|
1 | 1 | --- |
2 | | -description: No non-portable header includes in public headers |
| 2 | +description: No non-portable header includes in type-erased public headers |
3 | 3 | alwaysApply: false |
4 | 4 | --- |
5 | 5 | # Portable Headers Rule |
6 | 6 |
|
7 | | -**All headers in `include/boost/corosio/` (including all subdirectories) MUST NOT include platform-specific headers.** |
| 7 | +**Headers in `include/boost/corosio/` and `include/boost/corosio/detail/` MUST NOT include platform-specific headers.** |
8 | 8 |
|
9 | | -## Scope |
10 | | - |
11 | | -**Public headers**: ANY header file in `include/boost/corosio/` or any of its subdirectories. |
| 9 | +The `native/` subtree (`include/boost/corosio/native/`) is exempt — it is the direct/native API that deliberately exposes platform types. |
12 | 10 |
|
13 | | -Examples: |
14 | | -- `include/boost/corosio/endpoint.hpp` |
15 | | -- `include/boost/corosio/detail/config.hpp` |
16 | | -- `include/boost/corosio/concept/...` |
| 11 | +## Scope |
17 | 12 |
|
18 | | -The entire `include/` tree is considered public API. This includes `detail/`, `concept/`, and any other subdirectories. |
| 13 | +**Type-erased headers** (platform includes FORBIDDEN): |
| 14 | +- `include/boost/corosio/*.hpp` |
| 15 | +- `include/boost/corosio/detail/*.hpp` |
| 16 | +- `include/boost/corosio/concept/*.hpp` |
19 | 17 |
|
20 | | -## Prohibited Headers |
| 18 | +**Native/direct headers** (platform includes ALLOWED): |
| 19 | +- `include/boost/corosio/native/*.hpp` |
| 20 | +- `include/boost/corosio/native/detail/**/*.hpp` |
21 | 21 |
|
22 | | -The following types of headers are **FORBIDDEN** in public headers: |
| 22 | +## Prohibited Headers (in type-erased scope) |
23 | 23 |
|
24 | 24 | ### Windows-specific |
25 | | -- `<windows.h>` |
26 | | -- `<WinSock2.h>` |
27 | | -- `<Ws2tcpip.h>` |
28 | | -- `<MSWSock.h>` |
| 25 | +- `<windows.h>`, `<WinSock2.h>`, `<Ws2tcpip.h>`, `<MSWSock.h>` |
29 | 26 | - Any other Windows SDK headers |
30 | 27 |
|
31 | 28 | ### Unix/POSIX-specific |
32 | | -- `<sys/socket.h>` |
33 | | -- `<netinet/in.h>` |
34 | | -- `<arpa/inet.h>` |
35 | | -- `<unistd.h>` |
36 | | -- `<sys/types.h>` |
| 29 | +- `<sys/socket.h>`, `<netinet/in.h>`, `<arpa/inet.h>`, `<unistd.h>`, `<sys/types.h>` |
| 30 | +- `<errno.h>` when used for platform-specific constants (e.g., `ECANCELED`) |
37 | 31 | - Any other POSIX/Unix-specific headers |
38 | 32 |
|
39 | 33 | ### Platform-specific macros and types |
40 | | -- Any macros or types that require platform-specific headers |
41 | | -- Platform-specific constants (e.g., `AF_INET`, `INADDR_ANY`) |
| 34 | +- Constants like `AF_INET`, `INADDR_ANY`, `ECANCELED`, `ERROR_OPERATION_ABORTED` |
| 35 | +- Types like `sockaddr_in`, `sockaddr_in6`, `sockaddr_storage`, `SOCKET` |
42 | 36 |
|
43 | | -## Allowed Locations |
| 37 | +## Allowed Locations for Platform-Specific Code |
44 | 38 |
|
45 | | -Platform-specific code is **ONLY** allowed in: |
46 | | - |
47 | | -- Implementation files in `src/` |
48 | | -- Platform abstraction layers in `src/` |
| 39 | +- `include/boost/corosio/native/` and `include/boost/corosio/native/detail/` (direct API) |
| 40 | +- Implementation files in `src/` (compilation firewall) |
49 | 41 |
|
50 | 42 | ## Rationale |
51 | 43 |
|
52 | | -1. **Portability**: Public headers should compile on any platform without platform-specific dependencies |
53 | | -2. **Maintainability**: Platform-specific code should be isolated in implementation files |
54 | | -3. **User Experience**: Users should not be exposed to platform-specific types or headers |
55 | | -4. **Clean API**: The public API should be platform-agnostic |
| 44 | +The library has two API layers: |
| 45 | +1. **Type-erased** (`corosio/` and `corosio/detail/`): portable, no platform headers leak to users |
| 46 | +2. **Native/direct** (`corosio/native/`): opt-in, exposes platform types for zero-overhead access |
56 | 47 |
|
57 | 48 | ## Examples |
58 | 49 |
|
59 | | -### ❌ Bad (in public header) |
| 50 | +### Bad (platform header in type-erased scope) |
60 | 51 | ```cpp |
61 | | -// include/boost/corosio/endpoint.hpp |
62 | | -#ifdef _WIN32 |
63 | | -#include <WinSock2.h> |
64 | | -#else |
| 52 | +// include/boost/corosio/detail/endpoint_convert.hpp ← WRONG location |
| 53 | +#include <sys/socket.h> |
65 | 54 | #include <netinet/in.h> |
66 | | -#endif |
| 55 | +``` |
67 | 56 |
|
68 | | -class endpoint { |
69 | | - sockaddr_in to_sockaddr_in() const; // Platform-specific type |
70 | | -}; |
| 57 | +### Good (platform header in native scope) |
| 58 | +```cpp |
| 59 | +// include/boost/corosio/native/detail/endpoint_convert.hpp ← correct |
| 60 | +#include <sys/socket.h> |
| 61 | +#include <netinet/in.h> |
71 | 62 | ``` |
72 | 63 |
|
73 | | -### ✅ Good (in public header) |
| 64 | +### Good (type-erased public header) |
74 | 65 | ```cpp |
75 | 66 | // include/boost/corosio/endpoint.hpp |
76 | | -#include <boost/url/ipv4_address.hpp> |
77 | 67 | #include <cstdint> |
78 | 68 |
|
79 | 69 | class endpoint { |
80 | | - urls::ipv4_address v4_address() const noexcept; |
| 70 | + ipv4_address v4_address() const noexcept; |
81 | 71 | std::uint16_t port() const noexcept; |
82 | 72 | }; |
83 | 73 | ``` |
84 | 74 |
|
85 | | -### ✅ Good (in implementation file) |
86 | | -```cpp |
87 | | -// src/src/detail/endpoint_convert.hpp |
88 | | -#ifdef _WIN32 |
89 | | -#include <WinSock2.h> |
90 | | -#else |
91 | | -#include <netinet/in.h> |
92 | | -#endif |
93 | | - |
94 | | -#include <boost/corosio/endpoint.hpp> |
95 | | - |
96 | | -namespace boost { |
97 | | -namespace corosio { |
98 | | -namespace detail { |
99 | | - |
100 | | -sockaddr_in to_sockaddr_in(endpoint const& ep) { |
101 | | - // Platform-specific conversion logic |
102 | | -} |
103 | | - |
104 | | -} // namespace detail |
105 | | -} // namespace corosio |
106 | | -} // namespace boost |
107 | | -``` |
108 | | - |
109 | 75 | ## Enforcement |
110 | 76 |
|
111 | | -When adding or modifying public headers: |
| 77 | +When adding or modifying headers in the type-erased scope: |
112 | 78 |
|
113 | | -1. Check all `#include` directives |
114 | | -2. Ensure no platform-specific headers are included |
115 | | -3. Move any platform-specific code to `src/` implementation files |
116 | | -4. Use platform abstraction utilities in `src/src/detail/` for conversions |
| 79 | +1. Check all `#include` directives for platform-specific headers |
| 80 | +2. If platform types are needed, the header belongs in `native/detail/` |
| 81 | +3. Type-erased code reaches platform APIs only through `src/` compiled translation units |
0 commit comments