Skip to content

Commit 38168c2

Browse files
feat: better compile; material token access; gui color scheme galleries
1 parent ef509a7 commit 38168c2

25 files changed

Lines changed: 4343 additions & 8 deletions

base/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Base library - fundamental utilities and platform abstractions
22
cmake_minimum_required(VERSION 3.16)
33

4+
# ============================================================
5+
# Header-only library for base/include (utilities, hash, expected, etc.)
6+
# ============================================================
7+
add_library(cfbase_headers INTERFACE)
8+
target_include_directories(cfbase_headers INTERFACE
9+
${CMAKE_CURRENT_SOURCE_DIR}/include
10+
)
11+
add_library(CFDesktop::base_headers ALIAS cfbase_headers)
12+
413
# Include subdirectory CMakeLists (OBJECT libraries)
514
add_subdirectory(system/cpu)
615
add_subdirectory(system/memory)
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* @file constexpr_fnv1a.hpp
3+
* @brief Compile-time FNV-1a hash for string interning.
4+
*
5+
* Provides constexpr FNV-1a hashing algorithm for compile-time
6+
* string hash computation. Supports both 32-bit and 64-bit variants.
7+
*
8+
* @author Charliechen114514
9+
* @date 2026-02-25
10+
* @version 0.1
11+
* @since 0.1
12+
* @ingroup base_hash
13+
*/
14+
#pragma once
15+
16+
#include <cstdint>
17+
#include <string_view>
18+
19+
namespace cf::hash {
20+
21+
/**
22+
* @brief FNV-1a 64-bit offset basis.
23+
*
24+
* @since 0.1
25+
* @ingroup base_hash
26+
* @internal
27+
*/
28+
inline constexpr uint64_t fnv1a64_offset_basis = 14695981039346656037ULL;
29+
30+
/**
31+
* @brief FNV-1a 64-bit prime.
32+
*
33+
* @since 0.1
34+
* @ingroup base_hash
35+
* @internal
36+
*/
37+
inline constexpr uint64_t fnv1a64_prime = 1099511628211ULL;
38+
39+
/**
40+
* @brief FNV-1a 32-bit offset basis.
41+
*
42+
* @since 0.1
43+
* @ingroup base_hash
44+
* @internal
45+
*/
46+
inline constexpr uint32_t fnv1a32_offset_basis = 2166136261U;
47+
48+
/**
49+
* @brief FNV-1a 32-bit prime.
50+
*
51+
* @since 0.1
52+
* @ingroup base_hash
53+
* @internal
54+
*/
55+
inline constexpr uint32_t fnv1a32_prime = 16777619U;
56+
57+
/**
58+
* @brief FNV-1a 64-bit hash constexpr implementation.
59+
*
60+
* Computes FNV-1a hash at compile time for string literals.
61+
* The FNV-1a algorithm provides excellent distribution and
62+
* minimal collisions for string identifiers.
63+
*
64+
* @param str Null-terminated string to hash.
65+
* @param seed Starting hash value (default: FNV offset basis).
66+
*
67+
* @return 64-bit hash value.
68+
*
69+
* @throws None.
70+
*
71+
* @note The hash is computed at compile time for string literals.
72+
*
73+
* @warning Different inputs may produce the same hash (collision).
74+
* Use string comparison as fallback when hashes match.
75+
*
76+
* @since 0.1
77+
* @ingroup base_hash
78+
*
79+
* @code
80+
* constexpr uint64_t h1 = fnv1a64("TokenA"); // Compile-time
81+
* constexpr uint64_t h2 = fnv1a64("TokenB"); // Different value
82+
* static_assert(h1 != h2, "Hash collision!");
83+
* @endcode
84+
*/
85+
constexpr uint64_t fnv1a64(const char* str, uint64_t seed = fnv1a64_offset_basis) {
86+
return (*str == 0)
87+
? seed
88+
: fnv1a64(str + 1, (seed ^ static_cast<uint64_t>(*str)) * fnv1a64_prime);
89+
}
90+
91+
/**
92+
* @brief FNV-1a 64-bit hash for std::string_view (runtime/constexpr).
93+
*
94+
* @param sv String view to hash.
95+
* @param seed Starting hash value (default: FNV offset basis).
96+
*
97+
* @return 64-bit hash value.
98+
*
99+
* @since 0.1
100+
* @ingroup base_hash
101+
*/
102+
constexpr uint64_t fnv1a64(std::string_view sv, uint64_t seed = fnv1a64_offset_basis) {
103+
uint64_t result = seed;
104+
for (char c : sv) {
105+
result = (result ^ static_cast<uint64_t>(c)) * fnv1a64_prime;
106+
}
107+
return result;
108+
}
109+
110+
/**
111+
* @brief FNV-1a 32-bit hash constexpr implementation.
112+
*
113+
* @param str Null-terminated string to hash.
114+
* @param seed Starting hash value (default: FNV offset basis).
115+
*
116+
* @return 32-bit hash value.
117+
*
118+
* @since 0.1
119+
* @ingroup base_hash
120+
*/
121+
constexpr uint32_t fnv1a32(const char* str, uint32_t seed = fnv1a32_offset_basis) {
122+
return (*str == 0)
123+
? seed
124+
: fnv1a32(str + 1, (seed ^ static_cast<uint32_t>(*str)) * fnv1a32_prime);
125+
}
126+
127+
/**
128+
* @brief FNV-1a 32-bit hash for std::string_view.
129+
*
130+
* @param sv String view to hash.
131+
* @param seed Starting hash value (default: FNV offset basis).
132+
*
133+
* @return 32-bit hash value.
134+
*
135+
* @since 0.1
136+
* @ingroup base_hash
137+
*/
138+
constexpr uint32_t fnv1a32(std::string_view sv, uint32_t seed = fnv1a32_offset_basis) {
139+
uint32_t result = seed;
140+
for (char c : sv) {
141+
result = (result ^ static_cast<uint32_t>(c)) * fnv1a32_prime;
142+
}
143+
return result;
144+
}
145+
146+
/**
147+
* @brief User-defined literal for compile-time FNV-1a 64-bit hashing.
148+
*
149+
* Enables syntax: `"TokenName"_hash` for compile-time hash computation.
150+
*
151+
* @since 0.1
152+
* @ingroup base_hash
153+
*
154+
* @code
155+
* constexpr uint64_t h = "MyToken"_hash; // Compile-time hash
156+
* static_assert(h == fnv1a64("MyToken"), "Hash must match");
157+
* @endcode
158+
*/
159+
constexpr uint64_t operator""_hash(const char* str, size_t) {
160+
return fnv1a64(str);
161+
}
162+
163+
} // namespace cf::hash

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
log_info("Example" "Managing Example Components")
22

33
add_subdirectory(base)
4+
add_subdirectory(gui)
45

example/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
log_info("Example GUI" "Managing GUI Examples")
2+
3+
add_subdirectory(material_color_scheme)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Material Color Scheme Gallery Example
2+
log_info("Material Color Scheme" "Building Color Gallery Example")
3+
4+
add_executable(material_color_scheme_gallery
5+
main.cpp
6+
MaterialColorSchemeMainWindow.h
7+
MaterialColorSchemeMainWindow.cpp
8+
)
9+
10+
target_link_libraries(material_color_scheme_gallery PRIVATE
11+
Qt6::Widgets
12+
Qt6::Core
13+
Qt6::Gui
14+
cfui
15+
)
16+
17+
# Windows 下设置为 GUI 应用(不显示控制台窗口)
18+
if(WIN32)
19+
set_target_properties(material_color_scheme_gallery PROPERTIES
20+
WIN32_EXECUTABLE TRUE
21+
)
22+
# 自动部署 Qt DLL
23+
qt_add_windows_deploy(material_color_scheme_gallery AUTO_DEPLOY)
24+
endif()

0 commit comments

Comments
 (0)