|
JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
|
Immutable, thread-safe string with optional auto-trimming and dual-mode build support. More...
#include <algorithm>#include <memory>#include <unordered_map>#include <unordered_set>#include <string>#include <cstring>#include <string_view>#include <cstdint>#include <optional>#include <type_traits>#include <stdexcept>#include "jh/synchronous/const_lock.h"#include "jh/concurrent/observe_pool.h"#include "jh/pods/string_view.h"#include "jh/macros/header_begin.h"#include "jh/macros/header_end.h"Go to the source code of this file.
Classes | |
| class | jh::immutable_str |
| Immutable string with optional automatic trimming and thread-safe hash caching. More... | |
| struct | jh::atomic_str_hash |
Custom hash functor for atomic_str_ptr and compatible types. More... | |
| struct | jh::atomic_str_eq |
Custom equality functor for atomic_str_ptr and compatible types. More... | |
Concepts | |
| concept | jh::immutable_str_compatible |
Concept for types compatible with jh::immutable_str. | |
Macros | |
| #define | JH_IMMUTABLE_STR_AUTO_TRIM true |
Controls whether jh::immutable_str performs automatic leading/trailing ASCII whitespace trimming. | |
Typedefs | |
| using | jh::atomic_str_ptr = std::shared_ptr<immutable_str> |
| Atomically replaceable handle to an immutable string. | |
Functions | |
| template<typename T> | |
| atomic_str_ptr | jh::make_atomic (T str)=delete |
| atomic_str_ptr | jh::make_atomic (const char *str) |
Creates a shared pointer to an immutable_str. | |
| template<jh::concepts::mutex_like M> | |
| atomic_str_ptr | jh::safe_from (std::string_view sv, M &mtx) |
Creates a shared pointer to an immutable_str from a locked string view. | |
Immutable, thread-safe string with optional auto-trimming and dual-mode build support.
jh::immutable_str provides a true immutable string type in modern C++. It guarantees memory-level immutability and thread safety — once created, the string data can never be modified. This makes it ideal for concurrent environments, global configuration caches, or static metadata storage.
unique_ptr<const char[]>. std::shared_ptr<immutable_str> for safe sharing. In C++, std::string remains mutable even when declared const. This permits unintended modification via const_cast or aliasing, leading to race conditions and subtle data corruption. jh::immutable_str eliminates these risks by enforcing immutability both at the type level and memory level.
const std::string| Feature | jh::immutable_str | const std::string |
|---|---|---|
| Memory-level immutability | ✅ True | ❌ False |
| Thread safety | ✅ Safe by design | ❌ Mutable buffer |
| Reallocation risk | ❌ None | ✅ Possible |
| Hashing | ✅ Cached, thread-safe | ❌ Recomputed each time |
| Storage model | Compact (unique_ptr) | Dynamic capacity-managed |
unique_ptr<const char[]>, preventing mutation. std::once_flag to ensure safe caching. JH_IMMUTABLE_STR_AUTO_TRIM). jh::atomic_str_ptr (shared_ptr alias). std::string_view and C-string APIs. unordered_map lookup via const char*. From v1.3.x, jh::immutable_str supports the Dual-Mode Header system:
jh::jh-toolkit → acts as a header-only component. jh::jh-toolkit-static → compiled as a static implementation for performance and deterministic linking. JH_INTERNAL_SHOULD_DEFINE. The method bool jh::immutable_str::is_static_built() allows runtime mode verification:
jh-toolkit-static. jh-toolkit. If the program prints the expected message, it confirms that the correct linkage (header-only or static) is configured properly in your build environment.
This header automatically includes jh/core/pool.h, exposing the full pooling behavior of jh::immutable_str without requiring any additional include directives. Because jh::observe_pool performs duck-typed deduction (detecting hash() and operator==), immutable_str instances are automatically compatible with jh::observe_pool.
In addition to providing hash() and operator==, types used with jh::observe_pool must guarantee semantic immutability: once an object is inserted into the pool, its hash() and equality semantics must remain constant for its entire lifetime. This ensures that pooled instances remain stable and deduplicated.
jh::immutable_str naturally satisfies this requirement — its memory content and hash are fixed at construction time and can never change. Therefore, it represents the canonical example of a pool-safe immutable type.
This means you can directly acquire shared, deduplicated immutable strings:
The pool.acquire() call internally checks for an existing equivalent object (by hash() and operator==) and reuses it if found. This guarantees that semantically identical strings always share the same underlying immutable buffer.
jh/pool.h is included by default. immutable_str itself is immutable. jh::observe_pool automatically recognizes compatible types implementing hash() and operator==. jh::immutable_str shows performance essentially identical to std::string — sometimes slower by about 1%, sometimes faster by up to 2%, typically fluctuating within ±2%. This variation is within normal measurement noise. jh::observe_pool — efficient pooling system compatible with immutable_str. jh::atomic_str_ptr — shared-pointer alias for efficient immutable string sharing. 1.3.x
2025
| #define JH_IMMUTABLE_STR_AUTO_TRIM true |
Controls whether jh::immutable_str performs automatic leading/trailing ASCII whitespace trimming.
Default: true
If not defined manually, it defaults to:
#define JH_IMMUTABLE_STR_AUTO_TRIM true
If overriding manually, it must be defined before any inclusion of <jh/immutable_str> in the translation unit.
Configuration methods:
#define JH_IMMUTABLE_STR_AUTO_TRIM false
target_compile_definitions(target PRIVATE JH_IMMUTABLE_STR_AUTO_TRIM=false)
-DJH_IMMUTABLE_STR_AUTO_TRIM=false
Important:
true or false (not 1/0). static constexpr bool immutable_str::auto_trim. if constexpr, not preprocessor elimination. jh-toolkit-static), both code paths are compiled; user compilation activates the selected branch. Behavior:
true: remove leading/trailing ASCII whitespace at construction. false: preserve input exactly.