Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
basic_container_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
24#include <iostream>
25#include <memory>
26#include <vector>
27#include <chrono>
28#include <iomanip>
29
30#include "container.h"
31
32using namespace kcenon::container;
33
35 std::cout << "=== Basic Container Usage ===" << std::endl;
36
37 // Create a new container
38 auto container = std::make_shared<value_container>();
39
40 // Set container metadata
41 container->set_source("example_client", "session_001");
42 container->set_target("example_server", "main_handler");
43 container->set_message_type("user_data");
44
45 std::cout << "Container created with:" << std::endl;
46 std::cout << " Source: " << container->source_id() << "/" << container->source_sub_id() << std::endl;
47 std::cout << " Target: " << container->target_id() << "/" << container->target_sub_id() << std::endl;
48 std::cout << " Type: " << container->message_type() << std::endl;
49}
50
52 std::cout << "\n=== Value Types Demonstration ===" << std::endl;
53
54 auto container = std::make_shared<value_container>();
55 container->set_message_type("value_types_demo");
56
57 // String value using new set_value API
58 container->set("username", std::string("john_doe"));
59 std::cout << "Added string value: username = john_doe" << std::endl;
60
61 // Integer value
62 container->set("user_id", static_cast<int32_t>(12345));
63 std::cout << "Added int value: user_id = 12345" << std::endl;
64
65 // Long value (timestamp)
66 auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(
67 std::chrono::system_clock::now().time_since_epoch()).count();
68 container->set("timestamp", static_cast<int64_t>(timestamp));
69 std::cout << "Added long value: timestamp = " << timestamp << std::endl;
70
71 // Float value
72 container->set("score", 98.5f);
73 std::cout << "Added float value: score = 98.5" << std::endl;
74
75 // Double value
76 container->set("account_balance", 1500.75);
77 std::cout << "Added double value: account_balance = 1500.75" << std::endl;
78
79 // Boolean value
80 container->set("is_active", true);
81 std::cout << "Added bool value: is_active = true" << std::endl;
82
83 std::cout << "Total values added: 6" << std::endl;
84}
85
87 std::cout << "\n=== Serialization Demonstration ===" << std::endl;
88
89 // Create container with various data
90 auto container = std::make_shared<value_container>();
91 container->set_source("serialize_test", "test_session");
92 container->set_target("deserialize_test", "test_handler");
93 container->set_message_type("serialization_test");
94
95 container->set("message", std::string("Hello, Serialization!"));
96 container->set("count", static_cast<int32_t>(42));
97 container->set("pi", 3.14159);
98 container->set("success", true);
99
100 // Serialize
101 std::cout << "Serializing container..." << std::endl;
102 std::string serialized_data = container->serialize_string(value_container::serialization_format::binary).value();
103 std::cout << "Serialized size: " << serialized_data.size() << " bytes" << std::endl;
104
105 // Deserialize
106 std::cout << "Deserializing container..." << std::endl;
107 auto new_container = std::make_shared<value_container>(serialized_data);
108
109 std::cout << "Deserialization successful!" << std::endl;
110 std::cout << "Deserialized container:" << std::endl;
111 std::cout << " Source: " << new_container->source_id() << "/" << new_container->source_sub_id() << std::endl;
112 std::cout << " Target: " << new_container->target_id() << "/" << new_container->target_sub_id() << std::endl;
113 std::cout << " Type: " << new_container->message_type() << std::endl;
114
115 // Verify specific values using get() API
116 if (auto message_value = new_container->get("message")) {
117 if (auto* str = std::get_if<std::string>(&message_value->data)) {
118 std::cout << " Message: " << *str << std::endl;
119 }
120 }
121
122 if (auto count_value = new_container->get("count")) {
123 if (auto* val = std::get_if<int32_t>(&count_value->data)) {
124 std::cout << " Count: " << *val << std::endl;
125 }
126 }
127}
128
130 std::cout << "\n=== Value Access Demonstration ===" << std::endl;
131
132 auto container = std::make_shared<value_container>();
133 container->set_message_type("value_access_test");
134
135 // Add sample data using set_value
136 container->set("product_name", std::string("Super Widget"));
137 container->set("price", 29.99);
138 container->set("quantity", static_cast<int32_t>(100));
139 container->set("in_stock", true);
140
141 std::cout << "Container contains 4 values" << std::endl;
142
143 // Access values by key using new API
144 std::cout << "\nAccessing values by key:" << std::endl;
145
146 if (auto product_name = container->get("product_name")) {
147 if (auto* str = std::get_if<std::string>(&product_name->data)) {
148 std::cout << " Product: " << *str << std::endl;
149 }
150 }
151
152 if (auto price = container->get("price")) {
153 if (auto* val = std::get_if<double>(&price->data)) {
154 std::cout << " Price: $" << *val << std::endl;
155 }
156 }
157
158 if (auto quantity = container->get("quantity")) {
159 if (auto* val = std::get_if<int32_t>(&quantity->data)) {
160 std::cout << " Quantity: " << *val << std::endl;
161 }
162 }
163
164 if (auto in_stock = container->get("in_stock")) {
165 if (auto* val = std::get_if<bool>(&in_stock->data)) {
166 std::cout << " In Stock: " << (*val ? "yes" : "no") << std::endl;
167 }
168 }
169}
170
172 std::cout << "\n=== Container Iteration ===" << std::endl;
173
174 auto container = std::make_shared<value_container>();
175 container->set_message_type("iteration_test");
176
177 // Add multiple items
178 container->set("item_1", std::string("first"));
179 container->set("item_2", std::string("second"));
180 container->set("item_3", std::string("third"));
181
182 std::cout << "Added 3 values with different names" << std::endl;
183
184 // Iterate over all values
185 std::cout << "Iterating over container values:" << std::endl;
186 for (const auto& val : *container) {
187 std::cout << " - " << val.name << " (type: " << static_cast<int>(val.type) << ")" << std::endl;
188 }
189}
190
192 std::cout << "\n=== Basic Performance Demonstration ===" << std::endl;
193
194 const int num_operations = 1000;
195
196 // Container creation performance
197 auto start_time = std::chrono::high_resolution_clock::now();
198
199 std::vector<std::shared_ptr<value_container>> containers;
200 containers.reserve(num_operations);
201
202 for (int i = 0; i < num_operations; ++i) {
203 auto container = std::make_shared<value_container>();
204 container->set_source("perf_client", "session_" + std::to_string(i));
205 container->set_target("perf_server", "handler");
206 container->set_message_type("performance_test");
207
208 container->set("index", static_cast<int32_t>(i));
209 container->set("data", std::string("test_data_" + std::to_string(i)));
210
211 containers.push_back(container);
212 }
213
214 auto end_time = std::chrono::high_resolution_clock::now();
215 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
216
217 double containers_per_second = (num_operations * 1000000.0) / duration.count();
218
219 std::cout << "Performance results:" << std::endl;
220 std::cout << " Created " << num_operations << " containers in "
221 << duration.count() << " microseconds" << std::endl;
222 std::cout << " Rate: " << std::fixed << std::setprecision(2)
223 << containers_per_second << " containers/second" << std::endl;
224
225 // Serialization performance
226 start_time = std::chrono::high_resolution_clock::now();
227
228 std::vector<std::string> serialized_data;
229 serialized_data.reserve(num_operations);
230
231 for (const auto& container : containers) {
232 serialized_data.push_back(container->serialize_string(value_container::serialization_format::binary).value());
233 }
234
235 end_time = std::chrono::high_resolution_clock::now();
236 duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
237
238 double serializations_per_second = (num_operations * 1000000.0) / duration.count();
239
240 std::cout << " Serialized " << num_operations << " containers in "
241 << duration.count() << " microseconds" << std::endl;
242 std::cout << " Rate: " << std::fixed << std::setprecision(2)
243 << serializations_per_second << " serializations/second" << std::endl;
244
245 // Calculate total data size
246 size_t total_size = 0;
247 for (const auto& data : serialized_data) {
248 total_size += data.size();
249 }
250
251 std::cout << " Total serialized data: " << total_size << " bytes" << std::endl;
252 std::cout << " Average per container: " << (total_size / num_operations) << " bytes" << std::endl;
253}
254
255int main() {
256 try {
257 std::cout << "Container System Basic Example" << std::endl;
258 std::cout << "==============================" << std::endl;
259
266
267 std::cout << "\n=== Basic Example Completed Successfully ===" << std::endl;
268 std::cout << "This example demonstrated:" << std::endl;
269 std::cout << " - Basic container creation and configuration" << std::endl;
270 std::cout << " - All supported value types using set_value() API" << std::endl;
271 std::cout << " - Serialization and deserialization" << std::endl;
272 std::cout << " - Value access patterns using get() API" << std::endl;
273 std::cout << " - Container iteration" << std::endl;
274 std::cout << " - Basic performance characteristics" << std::endl;
275
276 return 0;
277
278 } catch (const std::exception& e) {
279 std::cerr << "Error in basic example: " << e.what() << std::endl;
280 return 1;
281 }
282}
void demonstrate_performance_basics()
void demonstrate_basic_usage()
void demonstrate_iteration()
void demonstrate_value_access()
void demonstrate_value_types()
void demonstrate_serialization()