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 <map>
29
30#include "json/genericMapper.hpp"
31#include "json/mappingOptions.hpp"
32
33
34namespace anch::json {
35
36 // JSON mapper early declaration
37 template<typename T>
38 class ObjectMapper;
39
40 // JSON mapper early declaration
41 template<typename T>
42 class Factory;
43
52 template<typename T>
53 void registerObject(ObjectMapper<T>& mapper);
54
56 template<typename T>
57 using SerializeFn = std::function<void(const T&, std::ostream&, const anch::json::MappingOptions&)>;
58
60 template<typename T>
61 using DeserializeFn = std::function<bool(T&, anch::json::ReaderContext&)>;
62
75 template<typename T>
76 class ObjectMapper: public anch::json::GenericMapper<ObjectMapper<T>, T> {
78 template<typename U>
79 friend void anch::json::registerObject(ObjectMapper<U>&);
80
81 // Attributes +
82 private:
84 std::vector<std::function<bool((const T&, anch::json::WriterContext&))>> _writers;
85
87 std::map<std::string, anch::json::DeserializeFn<T>> _readers;
88 // Attributes -
89
90 // Constructors +
91 private:
95 ObjectMapper();
96
97 public:
101 ObjectMapper(const ObjectMapper& other) = delete;
102
106 ObjectMapper(ObjectMapper&& other) = delete;
107 // Constructors -
108
109 private:
110 // Destructors +
114 virtual ~ObjectMapper();
115 // Destructors -
116
117 // Methods +
118 public:
125 void serializeValue(const T& value, anch::json::WriterContext& context) const;
126
135 bool deserializeValue(T& value, anch::json::ReaderContext& context) const;
136
137 private:
148 template<typename P>
149 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
150
162 template<typename MT, typename P>
163 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
164
176 template<typename P, typename MT = P>
177 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter);
178
190 template<typename P, typename MT = P>
191 ObjectMapper<T>& registerField(const std::string& key, std::function<void(T&, const P&)> setter);
192
205 template<typename P, typename MT = P>
206 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter, std::function<void(T&, const P&)> setter);
207 // Methods -
208
209 };
210
211} // anch::json
212
213#include "json/impl/mapper.hpp"
JSON mapper factory.
Definition factory.hpp:52
Generic mapper.
Definition genericMapper.hpp:49
JSON complex object mapper.
Definition mapper.hpp:76
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:42
JSON writer context.
Definition writerContext.hpp:41
JSON namespace.
Definition constants.hpp:25
std::function< bool(T &, anch::json::ReaderContext &)> DeserializeFn
Definition mapper.hpp:61
std::function< void(const T &, std::ostream &, const anch::json::MappingOptions &)> SerializeFn
Definition mapper.hpp:57
void registerObject(anch::json::ObjectMapper< T > &mapper)
JSON mapping options.
Definition mappingOptions.hpp:36