|
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 string with optional automatic trimming and thread-safe hash caching. More...
#include <jh/core/immutable_str.h>
Public Member Functions | |
| immutable_str (const char *str) | |
| Constructs an immutable string from a C-string. | |
| template<typename T> | |
| immutable_str (T)=delete | |
| Deleted constructor to prevent unintended conversions. | |
| template<jh::concepts::mutex_like M> | |
| immutable_str (std::string_view sv, M &mtx) | |
Constructs an immutable string from a std::string_view with mutex protection. | |
| immutable_str (const immutable_str &)=delete | |
| Deleted copy constructor. | |
| immutable_str & | operator= (const immutable_str &)=delete |
| Deleted copy assignment operator. | |
| immutable_str (immutable_str &&)=delete | |
| Deleted move constructor. | |
| immutable_str & | operator= (immutable_str &&)=delete |
| Deleted move assignment operator. | |
| const char * | c_str () const noexcept |
| Returns the raw C-style string pointer. | |
| std::string | str () const |
Converts the immutable content to a std::string. | |
| std::string_view | view () const noexcept |
Returns a lightweight std::string_view to the immutable data. | |
| pod::string_view | pod_view () const noexcept |
Returns a jh::pod::string_view representing this immutable string. | |
| std::uint64_t | size () const noexcept |
| Returns the length of the immutable string. | |
| bool | operator== (const immutable_str &other) const noexcept |
Compares two immutable_str instances for equality. | |
| std::uint64_t | hash () const noexcept |
| Computes the cached hash value of the immutable string. | |
Static Public Member Functions | |
| static bool | is_static_built () |
| Reports whether this header was built in static mode. | |
Static Public Attributes | |
| static constexpr bool | auto_trim = JH_IMMUTABLE_STR_AUTO_TRIM |
Global flag selecting whether immutable_str performs automatic leading/trailing whitespace trimming. | |
Immutable string with optional automatic trimming and thread-safe hash caching.
immutable_str represents a truly immutable and thread-safe string object. Once constructed, its internal data is fixed in memory and cannot be modified, reallocated, or replaced. It is designed for efficient read-only access in concurrent systems, configuration registries, and shared constant datasets.
unique_ptr<const char[]>. std::shared_ptr<immutable_str> (atomic_str_ptr). std::once_flag. const char* keys in hash tables. The class provides several view accessors for interoperability:
c_str() — Returns a null-terminated read-only C-string pointer. view() — Returns a std::string_view to the internal data (no copy). pod_view() — Returns a jh::pod::string_view for POD-style access. str() — Returns a full std::string copy of the immutable buffer. Note: Only str() performs data copying; other view functions are zero-copy.
const char* or std::string_view (with a lock). jh::make_atomic() for shared atomic usage. JH_INTERNAL_SHOULD_DEFINE. is_static_built(). c_str(), view(), hash(), etc.) are thread-safe. std::call_once. jh::observe_pool — object pool compatible with immutable_str. jh::atomic_str_ptr — alias for std::shared_ptr<immutable_str>.
|
explicit |
Constructs an immutable string from a C-string.
Creates an immutable copy of the provided null-terminated C-string. The constructor performs an internal strlen() to determine the source length and allocates a new immutable buffer.
| str | A null-terminated C-string. May be nullptr, which is treated as an empty string. |
The caller must ensure that the input pointer remains valid and unmodified during construction. This is only safe when the input is either:
Passing a pointer that can be modified by another thread results in undefined behavior.
explicit to prevent unintended implicit conversions. strlen() internally to determine the input length. JH_IMMUTABLE_STR_AUTO_TRIM. immutable_str(std::string_view, Mutex&) instead. extern "C").
|
explicitdelete |
Deleted constructor to prevent unintended conversions.
This template overload disables single-argument construction from non-C-string types, ensuring that only const char* inputs are accepted. This avoids unsafe or ambiguous conversions that could lead to undefined behavior.
const char* can be used for direct construction. immutable_str(std::string_view, Mutex&) instead.
|
inline |
Constructs an immutable string from a std::string_view with mutex protection.
Creates an immutable copy of the data referenced by sv while holding the provided mutex. This overload is intended for cases where the source memory may be transient, mutable, or shared between threads.
| M | Any type satisfying jh::concepts::mutex_like — such as std::mutex, std::shared_mutex, or custom types. |
| sv | A std::string_view representing the source data. It may or may not be null-terminated. |
| mtx | A reference to a mutex protecting the lifetime of the buffer referenced by sv. |
| std::logic_error | If sv contains embedded null ('\0') characters. |
::strnlen() to verify that no embedded nulls exist within sv.size() bytes. sv.size() bytes into an internal immutable buffer, even if not null-terminated. mtx must guard the same memory region as sv.data(); using an unrelated mutex leads to undefined behavior. jh::sync::const_lock. jh::typed::null_mutex (from <jh/typed>) as the mutex parameter. It is a zero-cost, concept-compatible dummy mutex, and all locking operations become no-ops.
|
delete |
Deleted copy constructor.
immutable_str manages its string data through std::unique_ptr<const char[]>, enforcing exclusive ownership. Copy construction would require duplicating the underlying data buffer, which is explicitly disallowed to maintain immutability.
|
delete |
Deleted move constructor.
Unlike typical movable types, immutable_str forbids move semantics. Moving would transfer ownership of the underlying buffer, violating the immutability principle.
std::shared_ptr<jh::immutable_str>.
|
nodiscardnoexcept |
Returns the raw C-style string pointer.
Provides direct access to the internal immutable buffer as a const char*. The returned pointer is guaranteed to remain valid for the lifetime of the object.
immutable_str; do not deallocate it.
|
nodiscardnoexcept |
Computes the cached hash value of the immutable string.
Returns a 64-bit hash derived from the string's contents. The computation is performed lazily — the first call initializes the cached value in a thread-safe manner, and all subsequent calls return the stored result without recomputation.
std::once_flag.
|
static |
Reports whether this header was built in static mode.
Indicates if the current immutable_str implementation was compiled as part of the static library target (jh-toolkit-static) or used in header-only mode (jh-toolkit).
true if compiled as part of jh-toolkit-static; otherwise false.false. true (macro JH_IS_STATIC_BUILD defined).
|
delete |
Deleted copy assignment operator.
Copy assignment is disabled to preserve the immutable property of immutable_str. Assigning one immutable instance to another would imply replacing its internal buffer, which contradicts its design.
std::shared_ptr<jh::immutable_str> if sharing semantics are required.
|
delete |
Deleted move assignment operator.
Move assignment is disabled because immutable_str must remain constant for its entire lifetime. Any form of reassignment or ownership transfer is considered a modification of its internal state.
std::shared_ptr, not reassigned.
|
noexcept |
Compares two immutable_str instances for equality.
Performs a deep, byte-wise comparison of the internal buffers. This operator guarantees that two instances are considered equal only if their contents are identical.
| other | Another immutable_str instance to compare with. |
true if both strings contain identical data; otherwise false.operator!= as its logical negation.
|
nodiscardnoexcept |
Returns a jh::pod::string_view representing this immutable string.
Provides a POD-compatible, read-only view over the internal buffer. The returned object has the same layout and semantics as jh::pod::string_view — that is, a pair of const char* and uint64_t describing a non-owning range of bytes.
jh::pod::string_view referencing the same data as this object.immutable_str exists. jh::pod::string_view to compute stable hashes via any algorithm provided by jh::meta::c_hash, unlike the non-deterministic nature of typical runtime hash functions. jh::pod::string_view.
|
nodiscardnoexcept |
Returns the length of the immutable string.
Provides the total number of characters contained in the string. The length is determined at construction time and remains constant throughout the object's lifetime.
|
nodiscard |
Converts the immutable content to a std::string.
Creates and returns a copy of the internal immutable data as a std::string. This is the only interface that performs a deep copy, preserving immutability while providing a mutable external representation.
std::string containing a copy of the immutable data.std::string.
|
nodiscardnoexcept |
Returns a lightweight std::string_view to the immutable data.
Provides a non-owning view of the internal string data without copying. This is the most efficient accessor for read-only operations and comparison.
std::string_view referencing the immutable string data.immutable_str instance.
|
staticconstexpr |
Global flag selecting whether immutable_str performs automatic leading/trailing whitespace trimming.
This value is controlled by the macro JH_IMMUTABLE_STR_AUTO_TRIM. The macro must be defined before including <jh/immutable_str>. If the macro is not defined beforehand, the default value is true.
Because auto_trim is an ordinary constexpr bool, it can be inspected in constexpr contexts and used inside concepts, templates, and other compile-time logic—avoiding the need for preprocessor conditionals (#if / #ifdef).
true (default): All constructed immutable_str instances remove leading and trailing ASCII whitespace. false: Whitespace is preserved exactly. To ensure consistent behavior across translation units — especially when dependencies may include <jh/immutable_str> indirectly — place:
before any code that might include the header.