Skip to content

Commit 8c02260

Browse files
committed
Add design rules and conventions for NativeAPI library
1 parent 550c9ce commit 8c02260

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed

.rules/design-rules.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# NativeAPI Design Rules and Conventions
2+
3+
This document outlines the design principles, architectural patterns, and coding conventions for the NativeAPI library. These rules should be followed by all contributors and AI agents working on this codebase.
4+
5+
## Project Overview
6+
7+
NativeAPI is a cross-platform native API library that provides access to system-level functionality (windows, displays, keyboard monitoring, tray icons, etc.) through both C++ and C interfaces.
8+
9+
## Architecture & Design Principles
10+
11+
### 1. Dual API Architecture
12+
13+
The library maintains two parallel APIs:
14+
15+
- **C++ API**: Primary object-oriented interface located in `src/` directory
16+
- **C API**: FFI-compatible wrapper located in `src/capi/` directory for language bindings
17+
18+
**Rules:**
19+
- All public C++ classes must have corresponding C API wrappers
20+
- C API functions must be prefixed with `native_`
21+
- All C API functions must use `FFI_PLUGIN_EXPORT` macro
22+
- C API must handle memory management explicitly with create/destroy pairs
23+
24+
### 2. Cross-Platform Implementation
25+
26+
**Structure:**
27+
```
28+
src/
29+
├── platform/
30+
│ ├── macos/ # macOS-specific implementations (.mm files)
31+
│ ├── windows/ # Windows-specific implementations (.cpp files)
32+
│ └── linux/ # Linux-specific implementations (.cpp files)
33+
├── *.h # Abstract interfaces and declarations
34+
└── *.cpp # Platform-agnostic implementations
35+
```
36+
37+
**Rules:**
38+
- Abstract interfaces go in main `src/` directory
39+
- Platform-specific code goes in respective `src/platform/{platform}/` directories
40+
- Use conditional compilation for platform detection
41+
- macOS implementations use Objective-C++ (`.mm` extension)
42+
43+
### 3. Event System Architecture
44+
45+
The library uses a sophisticated event system with the following components:
46+
47+
- `Event`: Base class with timestamp support
48+
- `TypedEvent<T>`: Template for type-safe events
49+
- `EventListener`: Generic observer interface
50+
- `TypedEventListener<T>`: Type-safe observer template
51+
- `CallbackEventListener<T>`: Lambda/function callback support
52+
53+
**Rules:**
54+
- All events must inherit from `Event` or `TypedEvent<T>`
55+
- Event classes must implement `GetTypeName()` and `GetType()` methods
56+
- Use observer pattern for event distribution
57+
- Events must be immutable after creation
58+
- Include timestamp in all events
59+
60+
### 4. Memory Management Strategy
61+
62+
**C++ API:**
63+
- Use RAII principles throughout
64+
- Prefer `std::shared_ptr` for shared ownership
65+
- Use PIMPL idiom for platform-specific implementations
66+
- Automatic cleanup through destructors
67+
68+
**C API:**
69+
- Manual memory management with explicit lifecycle
70+
- All create functions must have corresponding destroy functions
71+
- Null pointer checks required in all C API functions
72+
- Return boolean for success/failure indication
73+
74+
## Coding Conventions
75+
76+
### 5. Naming Conventions
77+
78+
**Namespaces:**
79+
- All C++ code in `nativeapi` namespace
80+
- No nested namespaces
81+
82+
**Classes and Types:**
83+
- PascalCase: `WindowManager`, `EventDispatcher`, `WindowCreatedEvent`
84+
- ID types: `typedef long WindowID` pattern
85+
86+
**Functions and Methods:**
87+
- PascalCase for public methods: `GetInstance()`, `Focus()`, `IsVisible()`
88+
- Private methods may use camelCase or snake_case
89+
90+
**Variables:**
91+
- snake_case with trailing underscore for members: `window_id_`, `new_position_`
92+
- camelCase for local variables and parameters
93+
- UPPER_SNAKE_CASE for constants
94+
95+
**C API Naming:**
96+
- snake_case with `native_` prefix: `native_window_create()`, `native_window_get_title()`
97+
- Type names end with `_t`: `native_window_t`, `native_size_t`
98+
99+
### 6. File Organization
100+
101+
**File Extensions:**
102+
- `.h`: C++ headers with `#pragma once`
103+
- `.cpp`: C++ implementation files
104+
- `.mm`: Objective-C++ for macOS platform code
105+
- `_c.h`: C API headers
106+
- `_c.cpp`: C API implementation
107+
108+
**File Naming:**
109+
- snake_case for all filenames
110+
- Descriptive names matching primary class/functionality
111+
- Event files grouped: `window_event.h`, `keyboard_event.h`, `display_event.h`
112+
113+
### 7. Type System
114+
115+
**Geometry Types:**
116+
```cpp
117+
struct Point { double x, y; };
118+
struct Size { double width, height; };
119+
struct Rectangle { double x, y, width, height; };
120+
```
121+
122+
**ID Types:**
123+
```cpp
124+
typedef long WindowID;
125+
typedef long DisplayID;
126+
```
127+
128+
**C API Types:**
129+
- Opaque handle pattern: `typedef struct native_window_handle* native_window_t;`
130+
- Consistent `native_` prefixing
131+
- Mirror C++ geometry types with `native_` prefix
132+
133+
### 8. Documentation Standards
134+
135+
**Required Documentation:**
136+
- Doxygen-style comments for all public APIs
137+
- Class documentation with purpose and usage
138+
- Method documentation with parameters and return values
139+
- Example usage in complex APIs
140+
141+
**Format:**
142+
```cpp
143+
/**
144+
* Brief description of the class or method
145+
*
146+
* Detailed description if needed, including usage notes
147+
* or important behavioral information.
148+
*
149+
* @param param_name Description of parameter
150+
* @return Description of return value
151+
*/
152+
```
153+
154+
### 9. Design Pattern Usage
155+
156+
**Singleton Pattern:**
157+
- Use for manager classes: `WindowManager::GetInstance()`
158+
- Thread-safe static initialization
159+
- No public constructors
160+
161+
**PIMPL Pattern:**
162+
- Use for platform-specific implementations
163+
- Private `Impl` class in implementation files
164+
- Forward declare in headers
165+
166+
**Observer Pattern:**
167+
- Event system implementation
168+
- Type-safe event listeners
169+
- Support for both class-based and callback-based observers
170+
171+
**Factory Pattern:**
172+
- Options structs for object creation
173+
- Separate create/destroy functions in C API
174+
175+
### 10. Error Handling
176+
177+
**C++ API:**
178+
- Use exceptions for exceptional conditions
179+
- Return boolean for simple success/failure
180+
- Check return values and handle errors appropriately
181+
182+
**C API:**
183+
- Boolean return values for success/failure
184+
- Null pointer checks at function entry
185+
- Set output parameters only on success
186+
- Provide error information through separate functions if needed
187+
188+
## Implementation Guidelines
189+
190+
### 11. Header Dependencies
191+
192+
- Minimize header dependencies
193+
- Use forward declarations when possible
194+
- Include only what you use
195+
- Platform-specific includes only in implementation files
196+
197+
### 12. Platform-Specific Code
198+
199+
**macOS:**
200+
- Use Objective-C++ (`.mm`) for Cocoa integration
201+
- Bridge between C++ and Objective-C properly
202+
- Handle memory management for both C++ and Objective-C objects
203+
204+
**Windows:**
205+
- Use Windows API directly
206+
- Handle Unicode properly (UTF-8 <-> UTF-16 conversion)
207+
- Proper COM initialization/cleanup
208+
209+
**Linux:**
210+
- Use X11 or Wayland as appropriate
211+
- Handle different desktop environments
212+
- Proper error checking for system calls
213+
214+
### 13. Testing and Examples
215+
216+
- Provide examples for each major feature in `examples/` directory
217+
- Examples should demonstrate both C++ and C API usage
218+
- Include CMakeLists.txt for each example
219+
- Examples should be simple and focused
220+
221+
### 14. Thread Safety
222+
223+
- Document thread safety guarantees for each class
224+
- Use appropriate synchronization primitives
225+
- Event system should be thread-safe
226+
- Manager classes should handle concurrent access
227+
228+
## File Structure Requirements
229+
230+
```
231+
src/
232+
├── *.h # Public C++ interfaces
233+
├── *.cpp # C++ implementations
234+
├── capi/
235+
│ ├── *_c.h # C API headers
236+
│ └── *_c.cpp # C API implementations
237+
└── platform/
238+
├── macos/*.mm # macOS implementations
239+
├── windows/*.cpp # Windows implementations
240+
└── linux/*.cpp # Linux implementations
241+
242+
examples/
243+
├── {feature}_example/
244+
│ ├── CMakeLists.txt
245+
│ └── main.cpp # C++ example
246+
└── {feature}_c_example/
247+
├── CMakeLists.txt
248+
└── main.c # C example
249+
250+
include/
251+
└── nativeapi.h # Main public header
252+
```
253+
254+
## Quality Assurance
255+
256+
### 15. Code Review Checklist
257+
258+
- [ ] Follows naming conventions
259+
- [ ] Proper documentation
260+
- [ ] Memory management handled correctly
261+
- [ ] Platform-specific code isolated
262+
- [ ] C API provides equivalent functionality
263+
- [ ] Error handling implemented
264+
- [ ] Thread safety considered
265+
- [ ] Examples updated if needed
266+
267+
### 16. Performance Considerations
268+
269+
- Minimize allocations in hot paths
270+
- Use efficient data structures
271+
- Cache expensive operations
272+
- Profile platform-specific code
273+
- Consider lazy initialization for expensive resources
274+
275+
This document should be referenced by all contributors and AI agents to maintain consistency and quality in the NativeAPI codebase.

0 commit comments

Comments
 (0)