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
18void set_warning_logger(const std::function<void(const std::string&, const hpp::source_location& loc)>& logger)
19{
20 get_warning_logger() = logger;
21}
22void log_warning(const std::string& log_msg, const hpp::source_location& loc)
23{
24 auto& logger = get_warning_logger();
25 if(logger)
26 logger(log_msg, loc);
27}
28
30{
31 return current_path_context;
32}
33
35{
36 current_path_context = ctx;
37}
38
39void path_context::push_segment(const std::string& segment, bool ignore_next)
40{
42 {
43 if(!ignore_next)
44 {
46 {
47 ignore_next_push = false;
48 }
49 else
50 {
51 path_segments.push_back(segment);
52 }
53 }
54 }
55}
56
58{
59 if (recording_enabled && !path_segments.empty())
60 {
61 path_segments.pop_back();
62 }
63}
64
65auto path_context::get_current_path() const -> std::string
66{
67 if (path_segments.empty())
68 return "";
69
70 std::stringstream ss;
71 for (size_t i = 0; i < path_segments.size(); ++i)
72 {
73 if (i > 0)
74 ss << "/";
75 ss << path_segments[i];
76 }
77 return ss.str();
78}
79
84
89
90auto path_context::is_recording() const -> bool
91{
92 return recording_enabled;
93}
94
96{
97 path_segments.clear();
98 recording_enabled = false;
99}
100
101path_segment_guard::path_segment_guard(const std::string& segment, bool ignore_next_push)
102{
103 auto* ctx = get_path_context();
104 if (ctx && ctx->is_recording())
105 {
106 ctx->push_segment(segment);
107 was_pushed_ = true;
108 }
109}
110
112{
113 if (was_pushed_)
114 {
115 auto* ctx = get_path_context();
116 if (ctx)
117 {
118 ctx->pop_segment();
119 }
120 }
121}
122
124{
125 auto* ctx = get_path_context();
126 if (ctx && ctx->is_recording())
127 {
128 return ctx->get_current_path();
129 }
130 return "";
131}
132
133} // namespace serialization
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)
auto get_path_context() -> path_context *
void set_warning_logger(const std::function< void(const std::string &, const hpp::source_location &loc)> &logger)
auto get_current_path() const -> std::string
void push_segment(const std::string &segment, bool ignore_next=false)
auto is_recording() const -> bool
std::vector< std::string > path_segments
path_segment_guard(const std::string &segment, bool ignore_next_push=false)