C++ Coding Standards
Relevant source files
The following files were used as context for generating this wiki page:
C++ Coding Standards define the structural and stylistic requirements for the HXLibs project. The project requires a minimum of C++20 and encourages modern programming practices to ensure cross-platform compatibility and maintainability.
Sources: documents/CodingStandards/CppStyle.md:1-5, CMakeLists.txt:1-10
Naming Conventions
The project enforces specific naming patterns for different code entities to maintain consistency across the codebase.
File and Namespace Naming
- File Names: Use CamelCase (e.g.,
IoUringLoop.h). - Extensions: Use
.hppfor files containing templates and.hfor files without templates. - Namespaces: Match the folder structure. For example, code in
/HXLibs/coroutine/resides in theHX::coroutinenamespace. Sub-modules use a nestedinternalnamespace for implementation details.
Sources: documents/CodingStandards/CppStyle.md:7-10, documents/CodingStandards/CppStyle.md:27-37
Identifier Naming
- Class Members: Prefix with an underscore (e.g.,
_name). - Variables: Use camelCase for almost all variables.
- Enums: Use PascalCase (e.g.,
HttpContentType::Html). - Constants: Use camelCase, optionally prefixed with
k(e.g.,kMaxRetry). - Macros: Use uppercase with underscores. Public library macros use the
HX_{NAME}pattern. Private internal macros use the_hx_MACRO_{NAME}__pattern.
Sources: documents/CodingStandards/CppStyle.md:39-47, include/HXLibs/net/protocol/http/MimeType.hpp:370-380
Code Structure and Formatting
Include Guards and Headers
The project uses #pragma once as the standard mechanism to prevent header re-definition. The include order follows a specific hierarchy:
- Paired
.hand.cppfiles. - System/Standard libraries (sorted by extensionless headers last).
- Third-party libraries (e.g., OpenSSL).
- Project platform-specific API headers.
- Project library files.
Sources: documents/CodingStandards/CppStyle.md:12-25, documents/CodingStandards/CppStyle.md:75-77
Indentation and Formatting
- Indentation: Use 4 spaces.
- Tabs: Convert all tab characters to 4 spaces.
Sources: documents/CodingStandards/CppStyle.md:73-73
Compilation and Reserved Identifiers
The build system uses Clang-specific flags to detect the use of reserved identifiers and macros.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
add_compile_options(
-Wpedantic
-Wreserved-id-macro
-Wreserved-identifier
)
endif()
Sources: documents/CodingStandards/CppStyle.md:53-62
Macro Management
The library provides specialized macros to manage compiler warnings across different platforms (MSVC, Clang, GCC).
| Macro | Description |
|---|---|
HX_NO_WARNINGS_BEGIN | Disables common warnings like -Wall, -Wextra, and -Wpedantic. |
HX_NO_WARNINGS_END | Restores previous warning levels using compiler-specific push/pop pragmas. |
HX_NO_MAYBE_UNINITIALIZED_WARNINGS_BEGIN | Specifically targets GCC's -Wmaybe-uninitialized noise. |
Sources: include/HXLibs/macro/NoWarring.hpp:18-58
Reflection Macros
Project-specific macros facilitate reflection of struct and class members, allowing access to private variables.
The HX_REFL macro exposes members using their actual names, while HX_REFL_AS allows mapping internal member names to public aliases for serialization.
Sources: tests/reflection/02_refl_macro.cpp:11-76
String and Resource Handling
The project standardizes the use of std::string_view for efficient string handling and case-insensitive operations.
Case-Insensitive Comparisons
Internal utilities provide transparent hash and equality comparators for std::unordered_map keys, enabling lookups via std::string_view without allocation.
MIME Type Mapping
MIME types are defined in a static map using std::string_view for both keys (file extensions) and values (content types).
Sources: include/HXLibs/net/protocol/http/MimeType.hpp:38-360
Conclusion
HXLibs adheres to strict C++20 standards, prioritizing modern language features and consistent naming. These standards ensure that the asynchronous network components and reflection systems remain performant and portable across various compilers and environments.