Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
variant_value_factory.h
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, 🍀☀🌕🌥 🌊
5All rights reserved.
6*****************************************************************************/
7
8#pragma once
9
10#include "value.h"
11#include "core/value_types.h"
12#include "core/concepts.h"
13#include <string>
14#include <string_view>
15#include <vector>
16#include <memory>
17#include <type_traits>
18#include <concepts>
19
20namespace kcenon::container
21{
22 // ============================================================================
23 // Modern Factory API (Recommended)
24 // ============================================================================
25
45 namespace factory
46 {
58 template<typename T>
59 inline value make(std::string_view name, T&& val) {
60 return value(name, std::forward<T>(val));
61 }
62
71 inline value make_null(std::string_view name = "") {
72 return value(name);
73 }
74
78 inline value make_array(std::string_view name,
79 std::vector<std::shared_ptr<value>> values) {
80 array_variant arr;
81 arr.values = std::move(values);
82 return value(name, std::move(arr));
83 }
84
89 std::string_view name,
90 std::initializer_list<value> values)
91 {
92 array_variant arr;
93 arr.values.reserve(values.size());
94 for (const auto& v : values) {
95 arr.values.push_back(std::make_shared<value>(v));
96 }
97 return value(name, std::move(arr));
98 }
99
103 inline value make_empty_array(std::string_view name) {
104 return value(name, array_variant{});
105 }
106
110 inline value make_container(std::string_view name,
111 std::shared_ptr<thread_safe_container> container) {
112 return value(name, std::move(container));
113 }
114
118 inline value make_bytes(std::string_view name,
119 const uint8_t* data,
120 size_t size) {
121 return value(name, std::vector<uint8_t>(data, data + size));
122 }
123
128 inline value make_bytes_from_string(std::string_view name, std::string_view data) {
129 return value(name, std::vector<uint8_t>(
130 reinterpret_cast<const uint8_t*>(data.data()),
131 reinterpret_cast<const uint8_t*>(data.data() + data.size())
132 ));
133 }
134
135 } // namespace factory
136
137 // ============================================================================
138 // Type-based factory (runtime type selection)
139 // ============================================================================
140
153 inline value make_value_from_raw(std::string_view name,
154 value_types type,
155 const std::vector<uint8_t>& raw_data) {
156 return value(name, type, raw_data);
157 }
158
159 // ============================================================================
160 // Utility functions
161 // ============================================================================
162
166 inline value clone_with_name(const value& original,
167 std::string_view new_name) {
168 // Serialize and deserialize to create a deep copy, then change name
169 auto serialized = original.serialize();
170 auto cloned = value::deserialize(serialized);
171 if (!cloned) {
172 throw std::runtime_error("Failed to clone value");
173 }
174
175 // Create new value with same data but different name
176 return value(new_name, cloned->type(), serialized);
177 }
178
182 inline bool same_type(const value& a, const value& b) {
183 return a.type() == b.type();
184 }
185
189 inline std::string_view type_name(const value& value) {
190 switch (value.type()) {
191 case value_types::null_value: return "null";
192 case value_types::bool_value: return "bool";
193 case value_types::short_value: return "short";
194 case value_types::ushort_value: return "ushort";
195 case value_types::int_value: return "int";
196 case value_types::uint_value: return "uint";
197 case value_types::long_value: return "long";
198 case value_types::ulong_value: return "ulong";
199 case value_types::llong_value: return "llong";
200 case value_types::ullong_value: return "ullong";
201 case value_types::float_value: return "float";
202 case value_types::double_value: return "double";
203 case value_types::bytes_value: return "bytes";
204 case value_types::string_value: return "string";
205 case value_types::container_value: return "container";
206 case value_types::array_value: return "array";
207 default: return "unknown";
208 }
209 }
210
211} // namespace kcenon::container
Enhanced type-safe value with perfect legacy compatibility.
Definition value.h:129
value_types type() const
Get the value_types enum (NOT variant::index()!)
Definition value.cpp:138
static std::optional< value > deserialize(const std::vector< uint8_t > &data)
Deserialize from binary format (legacy compatible)
Definition value.cpp:555
std::vector< uint8_t > serialize() const
Serialize to binary format (legacy compatible)
Definition value.cpp:339
value make_empty_array(std::string_view name)
Create an empty array value.
value make_bytes(std::string_view name, const uint8_t *data, size_t size)
Create a bytes value from raw data pointer.
value make_bytes_from_string(std::string_view name, std::string_view data)
Create a bytes value from string (copy bytes) Note: This treats the string as binary data.
value make(std::string_view name, T &&val)
Generic factory template for creating typed values.
value make_array(std::string_view name, std::vector< std::shared_ptr< value > > values)
Create an array value from vector of shared_ptr<value>
value make_null(std::string_view name="")
Create a null value.
value make_container(std::string_view name, std::shared_ptr< thread_safe_container > container)
Create a container value.
value make_value_from_raw(std::string_view name, value_types type, const std::vector< uint8_t > &raw_data)
Create a value from value_types enum and raw data.
bool same_type(const value &a, const value &b)
Check if two values have the same type.
std::string_view type_name(const value &value)
Get human-readable type name.
value clone_with_name(const value &original, std::string_view new_name)
Create a copy of a value with a new name.
Recursive array type for variant.
Definition value.h:45
std::vector< std::shared_ptr< value > > values
Definition value.h:46