1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#ifndef RFL_PARSING_PARSER_DURATION_HPP_
#define RFL_PARSING_PARSER_DURATION_HPP_
#include <chrono>
#include <map>
#include <optional>
#include <type_traits>
#include "../Literal.hpp"
#include "../Variant.hpp"
#include "../always_false.hpp"
#include "Parent.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl ::parsing {
template <class R, class W, class Rep, class Period, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::chrono::duration<Rep, Period>>
struct Parser<R, W, std::chrono::duration<Rep, Period>, ProcessorsType> {
public:
using InputVarType = typename R::InputVarType;
using ParentType = Parent<W>;
using DurationType = std::chrono::duration<Rep, Period>;
using Unit = Literal<"nanoseconds", "microseconds", "milliseconds", "seconds",
"minutes", "hours", "days", "weeks", "months", "years">;
using SupportedTypes =
Variant<std::chrono::nanoseconds, std::chrono::microseconds,
std::chrono::milliseconds, std::chrono::seconds,
std::chrono::minutes, std::chrono::hours, std::chrono::days,
std::chrono::weeks, std::chrono::months, std::chrono::years>;
struct RType {
int64_t count;
Unit unit;
};
static Result<DurationType> read(const R& _r,
const InputVarType& _var) noexcept {
return Parser<R, W, RType, ProcessorsType>::read(_r, _var)
.and_then(to_duration)
.transform([](auto&& _duration) {
return _duration.visit([](auto&& _d) -> DurationType {
return std::chrono::duration_cast<DurationType>(std::move(_d));
});
});
}
template <class P>
static void write(const W& _w, const DurationType& _d,
const P& _parent) noexcept {
const auto r =
RType{.count = static_cast<int64_t>(_d.count()), .unit = make_unit()};
return Parser<R, W, RType, ProcessorsType>::write(_w, r, _parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return Parser<R, W, RType, ProcessorsType>::to_schema(_definitions);
}
private:
static Result<SupportedTypes> to_duration(const RType& _r) {
switch (_r.unit.value()) {
case Unit::value_of<"nanoseconds">():
return SupportedTypes(std::chrono::nanoseconds(_r.count));
case Unit::value_of<"microseconds">():
return SupportedTypes(std::chrono::microseconds(_r.count));
case Unit::value_of<"milliseconds">():
return SupportedTypes(std::chrono::milliseconds(_r.count));
case Unit::value_of<"seconds">():
return SupportedTypes(std::chrono::seconds(_r.count));
case Unit::value_of<"minutes">():
return SupportedTypes(std::chrono::minutes(_r.count));
case Unit::value_of<"days">():
return SupportedTypes(std::chrono::days(_r.count));
case Unit::value_of<"weeks">():
return SupportedTypes(std::chrono::weeks(_r.count));
case Unit::value_of<"months">():
return SupportedTypes(std::chrono::months(_r.count));
case Unit::value_of<"years">():
return SupportedTypes(std::chrono::years(_r.count));
default:
return error("Unsupported unit.");
}
}
static auto make_unit() noexcept {
if constexpr (std::is_same_v<DurationType, std::chrono::nanoseconds>) {
return Unit::make<"nanoseconds">();
} else if constexpr (std::is_same_v<DurationType,
std::chrono::microseconds>) {
return Unit::make<"microseconds">();
} else if constexpr (std::is_same_v<DurationType,
std::chrono::milliseconds>) {
return Unit::make<"milliseconds">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::seconds>) {
return Unit::make<"seconds">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::minutes>) {
return Unit::make<"minutes">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::hours>) {
return Unit::make<"hours">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::days>) {
return Unit::make<"days">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::weeks>) {
return Unit::make<"weeks">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::months>) {
return Unit::make<"months">();
} else if constexpr (std::is_same_v<DurationType, std::chrono::years>) {
return Unit::make<"years">();
} else {
static_assert(always_false_v<DurationType>, "Unsupported type.");
}
};
};
} // namespace rfl::parsing
#endif
|