Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::value_view Class Reference

Zero-copy value view for efficient read access. More...

#include <value_view.h>

Collaboration diagram for kcenon::container::value_view:
Collaboration graph

Public Member Functions

 value_view (const char *name_data, size_t name_length, const char *value_data, size_t value_length, value_types type) noexcept
 Construct a value view.
 
std::string_view name () const noexcept
 Get the value name as string_view (zero-copy)
 
value_types type () const noexcept
 Get the value type.
 
std::string_view as_string_view () const noexcept
 Get string value as string_view (zero-copy)
 
std::string as_string () const
 Get string value as owned copy.
 
template<typename T >
std::optional< T > as () const noexcept
 Type-safe value extraction.
 
bool is_null () const noexcept
 Check if this is a null value.
 
const char * data () const noexcept
 Get raw value data pointer.
 
size_t size () const noexcept
 Get value data length.
 

Private Member Functions

template<typename T >
std::optional< T > parse_integral () const noexcept
 Parse integral value from string data.
 
template<typename T >
std::optional< T > parse_floating () const noexcept
 Parse floating-point value from string data.
 

Static Private Member Functions

static bool is_integral_type (value_types t) noexcept
 Check if type is an integral type.
 

Private Attributes

const char * name_data_
 
size_t name_length_
 
const char * value_data_
 
size_t value_length_
 
value_types type_
 

Detailed Description

Zero-copy value view for efficient read access.

value_view provides a non-owning view into serialized data, enabling zero-copy access to string and numeric values. The underlying buffer must remain valid for the lifetime of the view.

Key features:

  • Zero-copy string access via string_view
  • Type-safe numeric extraction
  • Minimal memory overhead (just pointers and metadata)
  • No heap allocations for primitive types

Definition at line 55 of file value_view.h.

Constructor & Destructor Documentation

◆ value_view()

kcenon::container::value_view::value_view ( const char * name_data,
size_t name_length,
const char * value_data,
size_t value_length,
value_types type )
inlinenoexcept

Construct a value view.

Parameters
name_dataPointer to the name string data
name_lengthLength of the name string
value_dataPointer to the value data
value_lengthLength of the value data
typeThe value type

Definition at line 66 of file value_view.h.

69 : name_data_(name_data)
70 , name_length_(name_length)
71 , value_data_(value_data)
72 , value_length_(value_length)
73 , type_(type)
74 {}
value_types type() const noexcept
Get the value type.
Definition value_view.h:87

Member Function Documentation

◆ as()

template<typename T >
std::optional< T > kcenon::container::value_view::as ( ) const
inlinenodiscardnoexcept

Type-safe value extraction.

Template Parameters
TThe expected value type
Returns
std::optional containing the value if type matches

Definition at line 117 of file value_view.h.

118 {
119 if constexpr (std::is_same_v<T, std::string_view>) {
120 if (type_ == value_types::string_value ||
121 type_ == value_types::bytes_value) {
122 return as_string_view();
123 }
124 return std::nullopt;
125 }
126 else if constexpr (std::is_same_v<T, std::string>) {
127 if (type_ == value_types::string_value ||
128 type_ == value_types::bytes_value) {
129 return as_string();
130 }
131 return std::nullopt;
132 }
133 else if constexpr (std::is_same_v<T, bool>) {
134 if (type_ == value_types::bool_value && value_length_ > 0) {
135 return (value_data_[0] == 't' || value_data_[0] == 'T' ||
136 value_data_[0] == '1');
137 }
138 return std::nullopt;
139 }
140 else if constexpr (std::is_integral_v<T>) {
141 return parse_integral<T>();
142 }
143 else if constexpr (std::is_floating_point_v<T>) {
144 return parse_floating<T>();
145 }
146 else {
147 return std::nullopt;
148 }
149 }
std::optional< T > parse_integral() const noexcept
Parse integral value from string data.
Definition value_view.h:180
std::string_view as_string_view() const noexcept
Get string value as string_view (zero-copy)
Definition value_view.h:97
std::optional< T > parse_floating() const noexcept
Parse floating-point value from string data.
Definition value_view.h:213
std::string as_string() const
Get string value as owned copy.
Definition value_view.h:106

References as_string(), as_string_view(), parse_floating(), parse_integral(), type_, value_data_, and value_length_.

Here is the call graph for this function:

◆ as_string()

std::string kcenon::container::value_view::as_string ( ) const
inlinenodiscard

Get string value as owned copy.

Returns
Copied string

Definition at line 106 of file value_view.h.

107 {
108 return {value_data_, value_length_};
109 }

References value_data_, and value_length_.

Referenced by as().

Here is the caller graph for this function:

◆ as_string_view()

std::string_view kcenon::container::value_view::as_string_view ( ) const
inlinenodiscardnoexcept

Get string value as string_view (zero-copy)

Returns
string_view pointing into original buffer
Note
Only valid for string_value and bytes_value types

Definition at line 97 of file value_view.h.

98 {
99 return {value_data_, value_length_};
100 }

References value_data_, and value_length_.

Referenced by as().

Here is the caller graph for this function:

◆ data()

const char * kcenon::container::value_view::data ( ) const
inlinenodiscardnoexcept

Get raw value data pointer.

Definition at line 162 of file value_view.h.

163 {
164 return value_data_;
165 }

References value_data_.

◆ is_integral_type()

static bool kcenon::container::value_view::is_integral_type ( value_types t)
inlinestaticnodiscardprivatenoexcept

Check if type is an integral type.

Definition at line 245 of file value_view.h.

246 {
247 switch (t) {
248 case value_types::short_value:
249 case value_types::ushort_value:
250 case value_types::int_value:
251 case value_types::uint_value:
252 case value_types::long_value:
253 case value_types::ulong_value:
254 case value_types::llong_value:
255 case value_types::ullong_value:
256 return true;
257 default:
258 return false;
259 }
260 }

Referenced by parse_integral().

Here is the caller graph for this function:

◆ is_null()

bool kcenon::container::value_view::is_null ( ) const
inlinenodiscardnoexcept

Check if this is a null value.

Definition at line 154 of file value_view.h.

155 {
156 return type_ == value_types::null_value;
157 }

References type_.

◆ name()

std::string_view kcenon::container::value_view::name ( ) const
inlinenodiscardnoexcept

Get the value name as string_view (zero-copy)

Definition at line 79 of file value_view.h.

80 {
81 return {name_data_, name_length_};
82 }

References name_data_, and name_length_.

◆ parse_floating()

template<typename T >
std::optional< T > kcenon::container::value_view::parse_floating ( ) const
inlinenodiscardprivatenoexcept

Parse floating-point value from string data.

Definition at line 213 of file value_view.h.

214 {
215 if (type_ != value_types::float_value &&
216 type_ != value_types::double_value) {
217 return std::nullopt;
218 }
219
220 if (value_length_ == 0) {
221 return std::nullopt;
222 }
223
224 char* end = nullptr;
225 std::string temp(value_data_, value_length_);
226
227 if constexpr (std::is_same_v<T, float>) {
228 float val = std::strtof(temp.c_str(), &end);
229 if (end != temp.c_str() + temp.size()) {
230 return std::nullopt;
231 }
232 return val;
233 } else {
234 double val = std::strtod(temp.c_str(), &end);
235 if (end != temp.c_str() + temp.size()) {
236 return std::nullopt;
237 }
238 return static_cast<T>(val);
239 }
240 }

References type_, value_data_, and value_length_.

Referenced by as().

Here is the caller graph for this function:

◆ parse_integral()

template<typename T >
std::optional< T > kcenon::container::value_view::parse_integral ( ) const
inlinenodiscardprivatenoexcept

Parse integral value from string data.

Definition at line 180 of file value_view.h.

181 {
182 if (!is_integral_type(type_)) {
183 return std::nullopt;
184 }
185
186 if (value_length_ == 0) {
187 return std::nullopt;
188 }
189
190 // Use strtoll/strtoull for parsing
191 char* end = nullptr;
192 std::string temp(value_data_, value_length_);
193
194 if constexpr (std::is_signed_v<T>) {
195 long long val = std::strtoll(temp.c_str(), &end, 10);
196 if (end != temp.c_str() + temp.size()) {
197 return std::nullopt;
198 }
199 return static_cast<T>(val);
200 } else {
201 unsigned long long val = std::strtoull(temp.c_str(), &end, 10);
202 if (end != temp.c_str() + temp.size()) {
203 return std::nullopt;
204 }
205 return static_cast<T>(val);
206 }
207 }
static bool is_integral_type(value_types t) noexcept
Check if type is an integral type.
Definition value_view.h:245

References is_integral_type(), type_, value_data_, and value_length_.

Referenced by as().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_t kcenon::container::value_view::size ( ) const
inlinenodiscardnoexcept

Get value data length.

Definition at line 170 of file value_view.h.

171 {
172 return value_length_;
173 }

References value_length_.

◆ type()

value_types kcenon::container::value_view::type ( ) const
inlinenodiscardnoexcept

Get the value type.

Definition at line 87 of file value_view.h.

88 {
89 return type_;
90 }

References type_.

Member Data Documentation

◆ name_data_

const char* kcenon::container::value_view::name_data_
private

Definition at line 262 of file value_view.h.

Referenced by name().

◆ name_length_

size_t kcenon::container::value_view::name_length_
private

Definition at line 263 of file value_view.h.

Referenced by name().

◆ type_

value_types kcenon::container::value_view::type_
private

Definition at line 266 of file value_view.h.

Referenced by as(), is_null(), parse_floating(), parse_integral(), and type().

◆ value_data_

const char* kcenon::container::value_view::value_data_
private

Definition at line 264 of file value_view.h.

Referenced by as(), as_string(), as_string_view(), data(), parse_floating(), and parse_integral().

◆ value_length_

size_t kcenon::container::value_view::value_length_
private

Definition at line 265 of file value_view.h.

Referenced by as(), as_string(), as_string_view(), parse_floating(), parse_integral(), and size().


The documentation for this class was generated from the following file: