ORE Studio 0.0.4
Loading...
Searching...
No Matches
reflectors.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_UTILITY_RFL_REFLECTORS_HPP
21#define ORES_UTILITY_RFL_REFLECTORS_HPP
22
23#include <string>
24#include <chrono>
25#include <sstream>
26#include <cstdint>
27#include <stdexcept>
28#include <boost/uuid/uuid.hpp>
29#include <boost/uuid/uuid_io.hpp>
30#include <boost/lexical_cast.hpp>
31#include <boost/asio/ip/address.hpp>
32#include <rfl.hpp>
33
34// Forward declarations for enum types that need custom reflectors
35// to avoid GCC 15 std::min/max type deduction errors in rfl's
36// internal enum range detection.
37namespace ores::utility::serialization {
38 enum class error_code : std::uint16_t;
39}
40namespace ores::comms::messaging {
41 enum class compression_type : std::uint8_t;
42 enum class message_type;
43}
44
45namespace rfl {
46
52template<>
53struct Reflector<boost::uuids::uuid> {
54 using ReflType = std::string;
55
56 static boost::uuids::uuid to(const ReflType& str) {
57 return boost::lexical_cast<boost::uuids::uuid>(str);
58 }
59
60 static ReflType from(const boost::uuids::uuid& v) {
61 return boost::lexical_cast<std::string>(v);
62 }
63};
64
75template<>
76struct Reflector<std::chrono::system_clock::time_point> {
77 using ReflType = std::string;
78
87 static std::chrono::system_clock::time_point to(const ReflType& str) {
88 // Parse ISO 8601 format: "YYYY-MM-DD HH:MM:SS"
89 std::tm tm = {};
90 std::istringstream ss(str);
91 ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
92
93 auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
94 return tp;
95 }
96
104 static ReflType from(const std::chrono::system_clock::time_point& v) {
105 // The time_point is already in the "system clock" which is UTC.
106 // We format it directly. The format specifiers are:
107 // %F -> YYYY-MM-DD
108 // %T -> HH:MM:SS
109 // We add 'Z' to explicitly mark it as UTC, which is best practice.
110 return std::format("{:%F %T}Z", v);
111 }
112};
113
119template<>
120struct Reflector<boost::asio::ip::address> {
121 using ReflType = std::string;
122
123 static boost::asio::ip::address to(const ReflType& str) {
124 return boost::asio::ip::make_address(str);
125 }
126
127 static ReflType from(const boost::asio::ip::address& v) {
128 return v.to_string();
129 }
130};
131
138template<>
139struct Reflector<ores::comms::messaging::compression_type> {
140 using ReflType = std::uint8_t;
141
142 static ores::comms::messaging::compression_type to(const ReflType& v) {
143 // Valid values: none=0, zlib=1, gzip=2, bzip2=3
144 if (v > 3) {
145 throw std::runtime_error("Invalid value for compression_type enum: " +
146 std::to_string(v));
147 }
148 return static_cast<ores::comms::messaging::compression_type>(v);
149 }
150
151 static ReflType from(const ores::comms::messaging::compression_type& v) {
152 return static_cast<ReflType>(v);
153 }
154};
155
162template<>
163struct Reflector<ores::utility::serialization::error_code> {
164 using ReflType = std::uint16_t;
165
166 static ores::utility::serialization::error_code to(const ReflType& v) {
167 // Valid values: none=0x0000 through payload_incomplete=0x0019
168 if (v > 0x0019) {
169 throw std::runtime_error("Invalid value for error_code enum: " +
170 std::to_string(v));
171 }
172 return static_cast<ores::utility::serialization::error_code>(v);
173 }
174
175 static ReflType from(const ores::utility::serialization::error_code& v) {
176 return static_cast<ReflType>(v);
177 }
178};
179
186template<>
187struct Reflector<ores::comms::messaging::message_type> {
188 using ReflType = std::uint16_t;
189
190 static ores::comms::messaging::message_type to(const ReflType& v) {
191 // Valid values: 0x0001 to 0x5FFF (max subsystem range)
192 // More specific validation is done at the protocol layer
193 if (v == 0 || v > 0x5FFF) {
194 throw std::runtime_error("Invalid value for message_type enum: " +
195 std::to_string(v));
196 }
197 return static_cast<ores::comms::messaging::message_type>(v);
198 }
199
200 static ReflType from(const ores::comms::messaging::message_type& v) {
201 return static_cast<ReflType>(v);
202 }
203};
204
205}
206
207#endif
STL namespace.
ORE Studio - Graphical interface and data management for Open Source Risk Engine.
Definition image.hpp:27
Contains messaging related infrastructure in the comms library.
Definition assets_protocol.hpp:122
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:259
static std::chrono::system_clock::time_point to(const ReflType &str)
Parses a string into a time_point.
Definition reflectors.hpp:87
static ReflType from(const std::chrono::system_clock::time_point &v)
Formats a time_point into a string.
Definition reflectors.hpp:104