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 <boost/uuid/uuid.hpp>
27#include <boost/uuid/uuid_io.hpp>
28#include <boost/lexical_cast.hpp>
29#include <boost/asio/ip/address.hpp>
30#include <rfl.hpp>
31
32namespace rfl {
33
39template<>
40struct Reflector<boost::uuids::uuid> {
41 using ReflType = std::string;
42
43 static boost::uuids::uuid to(const ReflType& str) {
44 return boost::lexical_cast<boost::uuids::uuid>(str);
45 }
46
47 static ReflType from(const boost::uuids::uuid& v) {
48 return boost::lexical_cast<std::string>(v);
49 }
50};
51
62template<>
63struct Reflector<std::chrono::system_clock::time_point> {
64 using ReflType = std::string;
65
74 static std::chrono::system_clock::time_point to(const ReflType& str) {
75 // Parse ISO 8601 format: "YYYY-MM-DD HH:MM:SS"
76 std::tm tm = {};
77 std::istringstream ss(str);
78 ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
79
80 auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
81 return tp;
82 }
83
91 static ReflType from(const std::chrono::system_clock::time_point& v) {
92 // The time_point is already in the "system clock" which is UTC.
93 // We format it directly. The format specifiers are:
94 // %F -> YYYY-MM-DD
95 // %T -> HH:MM:SS
96 // We add 'Z' to explicitly mark it as UTC, which is best practice.
97 return std::format("{:%F %T}Z", v);
98 }
99};
100
106template<>
107struct Reflector<boost::asio::ip::address> {
108 using ReflType = std::string;
109
110 static boost::asio::ip::address to(const ReflType& str) {
111 return boost::asio::ip::make_address(str);
112 }
113
114 static ReflType from(const boost::asio::ip::address& v) {
115 return v.to_string();
116 }
117};
118
119}
120
121#endif
STL namespace.
static std::chrono::system_clock::time_point to(const ReflType &str)
Parses a string into a time_point.
Definition reflectors.hpp:74
static ReflType from(const std::chrono::system_clock::time_point &v)
Formats a time_point into a string.
Definition reflectors.hpp:91