Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
messaging_integration.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7// Internal implementation file - suppress deprecation warnings
8#define CONTAINER_INTERNAL_INCLUDE
9#include "core/container.h"
10#undef CONTAINER_INTERNAL_INCLUDE
11#include "core/concepts.h"
12#include <functional>
13#include <memory>
14#include <concepts>
15
16#ifdef HAS_PERFORMANCE_METRICS
17#include <chrono>
18#include <atomic>
19#include <mutex>
20#include <unordered_map>
21#endif
22
24
32public:
36 static std::shared_ptr<value_container> create_optimized_container(
37 const std::string& message_type = "data_container"
38 );
39
43 static std::string serialize_for_messaging(
44 const std::shared_ptr<value_container>& container,
45 bool compress = false
46 );
47
51 static std::shared_ptr<value_container> deserialize_from_messaging(
52 const std::string& data,
53 bool decompress = false
54 );
55
56#ifdef HAS_PERFORMANCE_METRICS
60 struct metrics {
61 std::atomic<uint64_t> containers_created{0};
62 std::atomic<uint64_t> serializations_performed{0};
63 std::atomic<uint64_t> deserializations_performed{0};
64 std::atomic<uint64_t> total_serialize_time_us{0};
65 std::atomic<uint64_t> total_deserialize_time_us{0};
66 };
67
68 static metrics& get_metrics();
69 static void reset_metrics();
70 static std::string get_metrics_summary();
71#endif
72
73#ifdef HAS_EXTERNAL_INTEGRATION
77 using container_callback_t = std::function<void(const std::shared_ptr<value_container>&)>;
78
81 static void unregister_callbacks();
82#endif
83
84private:
85#ifdef HAS_PERFORMANCE_METRICS
87#endif
88
89#ifdef HAS_EXTERNAL_INTEGRATION
90 static std::vector<container_callback_t> creation_callbacks_;
91 static std::vector<container_callback_t> serialization_callbacks_;
92 static std::mutex callback_mutex_;
93#endif
94};
95
100public:
102
103 messaging_container_builder& source(const std::string& id, const std::string& sub_id = "");
104 messaging_container_builder& target(const std::string& id, const std::string& sub_id = "");
105 messaging_container_builder& message_type(const std::string& type);
106
113 template<typename T>
114 messaging_container_builder& set(const std::string& key, T&& value);
115
118
119 std::shared_ptr<value_container> build();
120
121private:
122 std::shared_ptr<value_container> container_;
123 bool size_optimized_ = false;
124 bool speed_optimized_ = false;
125};
126
127#ifdef HAS_PERFORMANCE_METRICS
132public:
133 explicit container_performance_monitor(const std::string& operation_name);
135
136 void set_container_size(size_t size);
137 void set_result_size(size_t size);
138
139private:
140 std::string operation_name_;
141 std::chrono::high_resolution_clock::time_point start_time_;
142 size_t container_size_ = 0;
143 size_t result_size_ = 0;
144};
145#endif // HAS_PERFORMANCE_METRICS
146
147// Template implementation for set (unified API)
148// Uses concepts to provide clearer error messages for unsupported types
149template<typename T>
151 using DecayedT = std::decay_t<T>;
152
153 if constexpr (std::is_same_v<DecayedT, std::shared_ptr<value_container>>) {
154 // Handle nested containers by serializing and storing as bytes
155 auto result = value->serialize_string(value_container::serialization_format::binary);
156 if (result.is_ok()) {
157 std::string serialized_data = result.value();
158 std::vector<uint8_t> bytes(serialized_data.begin(), serialized_data.end());
159 container_->set(key, bytes);
160 }
161 } else if constexpr (std::is_same_v<DecayedT, bool>) {
162 container_->set(key, value);
163 } else if constexpr (concepts::IntegralType<DecayedT>) {
164 if constexpr (sizeof(DecayedT) <= 4) {
165 container_->set(key, static_cast<int32_t>(value));
166 } else {
167 container_->set(key, static_cast<int64_t>(value));
168 }
169 } else if constexpr (concepts::FloatingPointType<DecayedT>) {
170 if constexpr (std::is_same_v<DecayedT, float>) {
171 container_->set(key, value);
172 } else {
173 container_->set(key, value);
174 }
175 } else if constexpr (std::is_same_v<DecayedT, std::string>) {
176 container_->set(key, std::string(value));
177 } else if constexpr (concepts::StringLike<DecayedT>) {
178 container_->set(key, std::string(value));
179 }
180
181 return *this;
182}
183
184} // namespace kcenon::container::integration
185
186// Convenience macros for performance monitoring
187#ifdef HAS_PERFORMANCE_METRICS
188#define CONTAINER_PERF_MONITOR(name) \
189 kcenon::container::integration::container_performance_monitor _monitor(name)
190
191#define CONTAINER_PERF_SET_SIZE(size) \
192 _monitor.set_container_size(size)
193
194#define CONTAINER_PERF_SET_RESULT(size) \
195 _monitor.set_result_size(size)
196#else
197#define CONTAINER_PERF_MONITOR(name)
198#define CONTAINER_PERF_SET_SIZE(size)
199#define CONTAINER_PERF_SET_RESULT(size)
200#endif
std::chrono::high_resolution_clock::time_point start_time_
messaging_container_builder & source(const std::string &id, const std::string &sub_id="")
messaging_container_builder & target(const std::string &id, const std::string &sub_id="")
messaging_container_builder & message_type(const std::string &type)
messaging_container_builder & set(const std::string &key, T &&value)
Set a value by key (unified API)
Container integration manager for messaging systems.
static void register_serialization_callback(container_callback_t callback)
static std::shared_ptr< value_container > create_optimized_container(const std::string &message_type="data_container")
Container creation with messaging optimization.
static std::shared_ptr< value_container > deserialize_from_messaging(const std::string &data, bool decompress=false)
Optimized deserialization for messaging.
static std::vector< container_callback_t > serialization_callbacks_
static void register_creation_callback(container_callback_t callback)
static std::vector< container_callback_t > creation_callbacks_
static std::string serialize_for_messaging(const std::shared_ptr< value_container > &container, bool compress=false)
High-performance serialization for messaging.
std::function< void(const std::shared_ptr< value_container > &)> container_callback_t
External system callback registration.
Enhanced type-safe value with perfect legacy compatibility.
Definition value.h:129