AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
mapper.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2020 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <algorithm>
23#include <string>
24#include <vector>
25#include <map>
26#include <ostream>
27#include <istream>
28#include <functional>
29#include <limits>
30
31#include "json/constraints.hpp"
32#include "mapping.hpp"
33#include "json/genericMapper.hpp"
34#include "json/mappingOptions.hpp"
35
36
37namespace anch::json {
38
39 // JSON mapper early declaration
40 template<typename T>
41 class ObjectMapper;
42
43 // JSON mapper early declaration
44 template<typename T>
45 class Factory;
46
47} // anch::json
48
49namespace anch::json::detail {
50
59 template<typename P>
60 constexpr auto
61 valueTypeOf() {
62 using V = std::remove_cvref_t<P>;
63 if constexpr (anch::val::isNullable<V>()) {
64 return valueTypeOf<anch::val::detail::Unwrap<V>>();
65 } else if constexpr (anch::val::MapLike<V>) {
66 return std::type_identity<std::remove_cvref_t<typename V::mapped_type>>{};
67 } else if constexpr (anch::val::Collection<V>) {
68 return std::type_identity<std::remove_cvref_t<std::ranges::range_value_t<V>>>{};
69 } else {
70 return std::type_identity<V>{};
71 }
72 }
73
80 template<typename P>
81 using MapperType = typename decltype(anch::json::detail::valueTypeOf<P>())::type;
82
83} // anch::json::detail
84
85namespace anch::json {
86
88 template<typename T>
89 using SerializeFn = std::function<void(const T&, std::ostream&, const anch::json::MappingOptions&)>;
90
92 template<typename T>
93 using DeserializeFn = std::function<bool(T&, anch::json::ReaderContext&)>;
94
115 template<typename T>
116 class ObjectMapper: public anch::json::GenericMapper<ObjectMapper<T>, T> {
117 friend class anch::json::Factory<T>;
118
119 // Attributes +
120 private:
122 static constexpr std::size_t NOT_REQUIRED = std::numeric_limits<std::size_t>::max();
123
125 struct FieldReader {
128
130 std::size_t required = NOT_REQUIRED;
131
133 bool rejectNull = false;
134 };
135
137 using WriteFn = std::function<bool(const T&, anch::json::WriterContext&)>;
138
140 struct FieldWriter {
142 std::string key;
143
145 WriteFn write;
146 };
147
149 std::vector<FieldWriter> _writers;
150
152 struct FieldLess {
161 [[gnu::always_inline]] bool operator()(const std::string& a, const std::string& b) const noexcept {
162 int res = std::char_traits<char>::compare(a.data(), b.data(), std::min(a.size(), b.size()));
163 return res < 0 || (res == 0 && a.size() < b.size());
164 }
165 };
166
168 std::map<std::string, FieldReader, FieldLess> _readers;
169
171 std::vector<std::string> _required;
172 // Attributes -
173
174 // Constructors +
175 private:
179 ObjectMapper();
180
181 public:
185 ObjectMapper(const ObjectMapper& other) = delete;
186
190 ObjectMapper(ObjectMapper&& other) = delete;
191 // Constructors -
192
193 private:
194 // Destructors +
198 virtual ~ObjectMapper();
199 // Destructors -
200
201 // Methods +
202 public:
209 void serializeValue(const T& value, anch::json::WriterContext& context) const;
210
219 bool deserializeValue(T& value, anch::json::ReaderContext& context) const;
220
221 private:
233 const FieldReader* readField(T& value, const std::string& field, anch::json::ReaderContext& context) const;
234
244 [[gnu::noinline]] void deserializeRequired(T& value, anch::json::ReaderContext& context) const;
245
259 template<typename G, typename W, typename... C>
260 void addWriter(const std::string& key, G get, W write, const C&... constraints);
261
270 void addReader(const std::string& key, anch::json::DeserializeFn<T>&& reader, bool required, bool nullable);
271
285 template<typename R, typename P, typename... C>
286 static anch::json::DeserializeFn<T> withConstraints(R& read, P T::* value, C&&... constraints);
287
302 template<typename MT, typename P, typename... C>
303 ObjectMapper<T>& registerMapped(const std::string& key, P T::* value, C&&... constraints);
304
305 public:
306 // Fields registration (public: the common description anch::Mapping<T> registers fields through the
307 // anch::ObjectMapper concept ; fields are registered once, when the mapper is built) +
320 template<typename P, typename... C>
321 requires (anch::val::FieldConstraint<C,P> && ...)
322 ObjectMapper<T>& registerField(const std::string& key, P T::* value, C&&... constraints);
323
336 template<typename P, typename... C>
337 requires (anch::val::FieldConstraint<C,P> && ...)
338 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter, const C&... constraints);
339
352 template<typename P, typename... C>
353 requires (anch::val::FieldConstraint<C,P> && ...)
354 ObjectMapper<T>& registerField(const std::string& key, std::function<void(T&, const P&)> setter, C&&... constraints);
355
369 template<typename P, typename... C>
370 requires (anch::val::FieldConstraint<C,P> && ...)
371 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter, std::function<void(T&, const P&)> setter, C&&... constraints);
372 // Fields registration -
373
374 // Methods -
375
376 };
377
378} // anch::json
379
380#include "json/impl/mapper.hpp"
JSON mapper factory.
Definition factory.hpp:54
Generic mapper.
Definition genericMapper.hpp:49
JSON complex object mapper.
Definition mapper.hpp:116
ObjectMapper< T > & registerField(const std::string &key, std::function< void(T &, const P &)> setter, C &&... constraints)
ObjectMapper< T > & registerField(const std::string &key, std::function< P(const T &)> getter, std::function< void(T &, const P &)> setter, C &&... constraints)
ObjectMapper< T > & registerField(const std::string &key, std::function< P(const T &)> getter, const C &... constraints)
ObjectMapper< T > & registerField(const std::string &key, P T::*value, C &&... constraints)
ObjectMapper(ObjectMapper &&other)=delete
void serializeValue(const T &value, anch::json::WriterContext &context) const
bool deserializeValue(T &value, anch::json::ReaderContext &context) const
ObjectMapper(const ObjectMapper &other)=delete
JSON reader context.
Definition readerContext.hpp:44
JSON writer context.
Definition writerContext.hpp:45
Mapper of a format on which anch::Mapping registers the fields of T.
Definition mapping.hpp:88
Definition constraints.hpp:1164
JSON namespace.
Definition constants.hpp:27
std::function< bool(T &, anch::json::ReaderContext &)> DeserializeFn
Definition mapper.hpp:93
std::function< void(const T &, std::ostream &, const anch::json::MappingOptions &)> SerializeFn
Definition mapper.hpp:89
constexpr bool isNullable()
Definition constraints.hpp:1205
JSON mapping options.
Definition mappingOptions.hpp:38