Unravel Engine C++ Reference
Loading...
Searching...
No Matches
serialization.cpp
Go to the documentation of this file.
1#include "serialization.h"
2#include <sstream>
3
4namespace serialization
5{
6namespace
7{
8auto get_warning_logger() -> log_callback_t&
9{
10 static log_callback_t logger;
11 return logger;
12}
13
14// Path tracking implementation
15thread_local path_context* current_path_context = nullptr;
16
17} // namespace
18
19void log_warning(const std::string& log_msg, const hpp::source_location& loc)
20{
21 auto& logger = get_warning_logger();
22 if(logger)
23 logger(log_msg, loc);
24}
25
27{
28 return current_path_context;
29}
30
32{
33 current_path_context = ctx;
34}
35
36auto path_context::push_segment(const std::string& segment) -> bool
37{
38
39 if(ignore_next_push)
40 {
41 ignore_next_push = false;
42 return false;
43 }
44
45 if (recording_enabled)
46 {
47 path_segments.push_back(segment);
48 return true;
49 }
50 return false;
51}
52
54{
55 if (recording_enabled && !path_segments.empty())
56 {
57 if(path_segments.back().ends_with("]"))
58 {
59 int a = 0;
60 a++;
61 }
62 path_segments.pop_back();
63 }
64}
65
66auto path_context::get_current_path() const -> std::string
67{
68 if (path_segments.empty())
69 return "";
70
71 std::stringstream ss;
72 for (size_t i = 0; i < path_segments.size(); ++i)
73 {
74 // For array indices, don't add a separator
75 if (i > 0 && path_segments[i][0] != '[')
76 ss << "/";
77 ss << path_segments[i];
78 }
79 return ss.str();
80}
81
86
91
92auto path_context::is_recording() const -> bool
93{
94 return recording_enabled;
95}
96
98{
99 path_segments.clear();
100 recording_enabled = false;
101}
102
103path_segment_guard::path_segment_guard(const std::string& segment)
104{
105 push_segment(segment);
106}
107
112
113
114void path_segment_guard::push_segment(const std::string& segment)
115{
116 auto* ctx = get_path_context();
117 if (ctx && ctx->is_recording())
118 {
119 was_pushed_ = ctx->push_segment(segment);
120 }
121}
123{
124 if(was_pushed_)
125 {
126 auto* ctx = get_path_context();
127 if (ctx && ctx->is_recording())
128 {
129 ctx->pop_segment();
130 }
131 }
132 was_pushed_ = false;
133}
134
135
137{
138 auto* ctx = get_path_context();
139 if (ctx && ctx->is_recording())
140 {
141 ctx->ignore_next_push = ignore_next_push;
142 }
143}
144
150{
151 auto* ctx = get_path_context();
152 if (ctx && ctx->is_recording())
153 {
154 return ctx->get_current_path();
155 }
156 return "";
157}
158
159
160
161void init(const init_data& data)
162{
163 if(data.warning_logger)
164 {
165 get_warning_logger() = data.warning_logger;
166 }
167
168 auto& context = ser20::get_vector_serialization_context();
169 context.on_element_serialization_begin = [](size_t index)
170 {
171 std::string index_segment = "[" + std::to_string(index) + "]";
172 // guard->push_segment(index_segment);
174 if (ctx && ctx->is_recording())
175 {
176 ctx->push_segment(index_segment);
177 }
178 };
179 context.on_element_serialization_end = [](size_t index)
180 {
182 if (ctx && ctx->is_recording())
183 {
184 ctx->pop_segment();
185 }
186 };
187 context.should_serialize_element = [](size_t index)
188 {
190 if (ctx && ctx->is_recording())
191 {
192 return ctx->should_serialize_property(ctx->get_current_path());
193 }
194 return true;
195 };
196}
197} // namespace serialization
entt::handle a
uint16_t index
void log_warning(const std::string &log_msg, const hpp::source_location &loc)
auto get_current_deserialization_path() -> std::string
std::function< void(const std::string &, const hpp::source_location &loc)> log_callback_t
void set_path_context(path_context *ctx)
void init(const init_data &data)
auto get_path_context() -> path_context *
Hash specialization for batch_key to enable use in std::unordered_map.
log_callback_t warning_logger
auto get_current_path() const -> std::string
auto push_segment(const std::string &segment) -> bool
auto is_recording() const -> bool
std::vector< std::string > path_segments
void push_segment(const std::string &segment)
path_skip_segment_guard(bool ignore_next_push=false)