AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
mapper.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 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 <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <string>
23#include <vector>
24#include <map>
25#include <ostream>
26#include <istream>
27#include <functional>
28#include <list>
29#include <set>
30#include <map>
31
32#include "json/constants.hpp"
33#include "json/mappingOptions.hpp"
34#include "json/mappingFunctions.hpp"
35#include "json/readerContext.hpp"
36
37
38namespace anch::json {
39
40 // JSON mapper early declaration
41 template<typename T>
42 class ObjectMapper;
43
44 // JSON mapper early declaration
45 template<typename T>
46 class Factory;
47
56 template<typename T>
57 void registerObject(ObjectMapper<T>& mapper);
58
71 template<typename T>
72 class ObjectMapper {
74 template<typename U>
75 friend void anch::json::registerObject(ObjectMapper<U>&);
76
77 // Attributes +
78 private:
80 std::vector<std::function<bool((const T&, std::ostream&, const anch::json::MappingOptions&))>> _writers;
81
83 std::map<std::string, anch::json::DeserializeFn<T>> _readers;
84 // Attributes -
85
86 // Constructors +
87 private:
91 ObjectMapper();
92
93 public:
97 ObjectMapper(const ObjectMapper& other) = delete;
98
102 ObjectMapper(ObjectMapper&& other) = delete;
103 // Constructors -
104
105 private:
106 // Destructors +
110 virtual ~ObjectMapper();
111 // Destructors -
112
113 // Methods +
114 private:
125 template<typename P>
126 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
127
139 template<typename MT, typename P>
140 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
141
153 template<typename P, typename MT = P>
154 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter);
155
167 template<typename P, typename MT = P>
168 ObjectMapper<T>& registerField(const std::string& key, std::function<void(T&, const P&)> setter);
169
182 template<typename P, typename MT = P>
183 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter, std::function<void(T&, const P&)> setter);
184
185 public:
196 bool serialize(const T& value,
197 std::ostream& out,
198 const anch::json::MappingOptions& options,
199 const std::optional<std::string>& field = EMPTY_FIELD);
200
211 bool serialize(const T* const value,
212 std::ostream& out,
213 const anch::json::MappingOptions& options,
214 const std::optional<std::string>& field = EMPTY_FIELD);
215
226 bool serialize(const std::optional<T>& value,
227 std::ostream& out,
228 const anch::json::MappingOptions& options,
229 const std::optional<std::string>& field = EMPTY_FIELD);
230
241 bool serialize(const std::vector<T>& value,
242 std::ostream& out,
243 const anch::json::MappingOptions& options,
244 const std::optional<std::string>& field = EMPTY_FIELD);
245
256 bool serialize(const std::list<T>& value,
257 std::ostream& out,
258 const anch::json::MappingOptions& options,
259 const std::optional<std::string>& field = EMPTY_FIELD);
260
271 bool serialize(const std::set<T>& value,
272 std::ostream& out,
273 const anch::json::MappingOptions& options,
274 const std::optional<std::string>& field = EMPTY_FIELD);
275
286 bool serialize(const std::map<std::string,T>& value,
287 std::ostream& out,
288 const anch::json::MappingOptions& options,
289 const std::optional<std::string>& field = EMPTY_FIELD);
290
299 bool deserialize(T& value, anch::json::ReaderContext& context);
300
309 bool deserialize(std::optional<T>& value, anch::json::ReaderContext& context);
310
319 bool deserialize(T* value, anch::json::ReaderContext& context);
320
330 bool deserialize(std::vector<T>& value, anch::json::ReaderContext& context);
331
340 bool deserialize(std::list<T>& value, anch::json::ReaderContext& context);
341
350 bool deserialize(std::set<T>& value, anch::json::ReaderContext& context);
351
358 bool deserialize(std::map<std::string,T>& value, anch::json::ReaderContext& context);
359
360 private:
369 bool deserializeValue(T& value, anch::json::ReaderContext& context);
370
378 void serializeValue(const T& value, std::ostream& out, const anch::json::MappingOptions& options);
379 // Methods -
380
381 };
382
383} // anch::json
384
385#include "json/impl/mapper.hpp"
JSON mapper factory.
Definition factory.hpp:52
JSON complex object mapper.
Definition mapper.hpp:72
bool serialize(const std::optional< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool deserialize(std::set< T > &value, anch::json::ReaderContext &context)
bool serialize(const T &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool deserialize(std::optional< T > &value, anch::json::ReaderContext &context)
ObjectMapper(ObjectMapper &&other)=delete
bool serialize(const std::list< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool deserialize(std::list< T > &value, anch::json::ReaderContext &context)
bool deserialize(T *value, anch::json::ReaderContext &context)
bool deserialize(std::map< std::string, T > &value, anch::json::ReaderContext &context)
bool deserialize(std::vector< T > &value, anch::json::ReaderContext &context)
bool serialize(const T *const value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const std::map< std::string, T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const std::set< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const std::vector< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool deserialize(T &value, anch::json::ReaderContext &context)
ObjectMapper(const ObjectMapper &other)=delete
JSON mapper context.
Definition readerContext.hpp:42
JSON namespace.
Definition constants.hpp:25
const std::optional< std::string > EMPTY_FIELD
void registerObject(anch::json::ObjectMapper< T > &mapper)
JSON mapping options.
Definition mappingOptions.hpp:35