CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
operation_serialization.cpp
Go to the documentation of this file.
1
11#include <algorithm>
12#include <cctype>
13#include <cstdlib>
14#include <spdlog/spdlog.h>
15
17
19{
20 // Use std::visit to handle each type in the variant
21 return std::visit([](auto&& arg) -> std::string {
22 using T = std::decay_t<decltype(arg)>;
23
24 if constexpr (std::is_same_v<T, bool>) {
25 return arg ? "true" : "false";
26 }
27 else if constexpr (std::is_same_v<T, int>) {
28 return std::to_string(arg);
29 }
30 else if constexpr (std::is_same_v<T, float>) {
31 return std::to_string(arg);
32 }
33 else if constexpr (std::is_same_v<T, std::string>) {
34 return arg;
35 }
36 else {
37 return ""; // Should not happen if all variant types are handled
38 }
39 }, value);
40}
41
43{
44 // 1. Check for Boolean (Case-insensitive)
45 std::string lower_val(value_str);
46 std::transform(lower_val.begin(), lower_val.end(), lower_val.begin(),
47 [](unsigned char c){ return std::tolower(c); });
48
49 if (lower_val == "true" || lower_val == "1") return true;
50 if (lower_val == "false" || lower_val == "0") return false;
51
52 // 2. Check for Integer (Int)
53 // We verify if the entire string is an integer to avoid confusing "10" with "10.5"
54 char* endptr = nullptr;
55 long long_val = std::strtol(value_str.data(), &endptr, 10);
56 // If we consumed the whole string and it wasn't empty
57 if (endptr != value_str.data() && *endptr == '\0' && !value_str.empty()) {
58 return static_cast<int>(long_val);
59 }
60
61 // 3. Check for Float (Float)
62 // std::strtof accepts "10" as 10.0f, so we use it if integer parsing failed
63 endptr = nullptr;
64 float float_val = std::strtof(value_str.data(), &endptr);
65 if (endptr != value_str.data()) {
66 return float_val;
67 }
68
69 // 4. Default to String (String)
70 return std::string(value_str);
71}
72
74{
75 // Simple format: "param1=value1;param2=value2"
76 std::string result;
77 for (const auto& [key, val] : descriptor.params) {
78 if (!result.empty()) {
79 result += ";";
80 }
81 result += key + "=" + serializeParameter(val);
82 }
83 return result;
84}
85
86bool deserializeOperationParameters(std::string_view params_str, Operations::OperationDescriptor& descriptor)
87{
88 // Reverse logic: Parse "key1=val1;key2=val2"
89 // Note: This is a basic implementation. For a robust format, JSON is preferred.
90 descriptor.params.clear();
91
92 size_t start = 0;
93 while (start < params_str.size()) {
94 size_t end = params_str.find(';', start);
95 std::string_view pair = (end == std::string_view::npos)
96 ? params_str.substr(start)
97 : params_str.substr(start, end - start);
98
99 size_t eq_pos = pair.find('=');
100 if (eq_pos != std::string_view::npos) {
101 std::string key(pair.substr(0, eq_pos));
102 std::string_view val_view(pair.substr(eq_pos + 1));
103
104 descriptor.params[key] = deserializeParameter(val_view);
105 }
106
107 if (end == std::string_view::npos) break;
108 start = end + 1;
109 }
110
111 return true;
112}
113
114} // namespace CaptureMoment::Core::Serialization
std::variant< float, int, bool, std::string > OperationValue
Supported types for operation parameters. Using variant instead of std::any for compile-time type saf...
bool deserializeOperationParameters(std::string_view params_str, Operations::OperationDescriptor &descriptor)
Deserializes a string representation back into an OperationDescriptor's parameters map....
Operations::OperationValue deserializeParameter(std::string_view value_str)
Deserializes a string representation back into an OperationValue. This function attempts to infer the...
std::string serializeOperationParameters(const Operations::OperationDescriptor &descriptor)
Serializes an entire OperationDescriptor's parameters map to a string representation....
std::string serializeParameter(const Operations::OperationValue &value)
Namespace containing utilities for serializing and deserializing OperationDescriptor parameters....
Descriptor of an operation with its parameters and unique identifier.
Declaration of OperationSerialization interface.
A universal container for operation settings with unique identifier.
std::unordered_map< std::string, OperationValue > params
Generic parameter storage using variant (Type Safe).
CaptureMoment — footer