AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
fields.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 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 <functional>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <vector>
27
28#include "mapping.hpp"
29#include "sql/parameters.hpp"
30
31namespace anch::sql {
32
50 template<anch::Described T>
51 class FieldsMapper {
52
53 public:
57 struct Field {
59 std::string name;
60
62 std::function<anch::sql::BoundValue(const T&)> read;
63 };
64
65 // Attributes +
66 private:
68 std::vector<Field> _fields;
69 // Attributes -
70
71 // Constructors +
72 private:
76 FieldsMapper();
77 // Constructors -
78
79 // Methods +
80 public:
86 static const FieldsMapper<T>& instance();
87
95 const Field* find(std::string_view name) const noexcept;
96
102 const std::vector<Field>& fields() const noexcept;
103
104 // Fields registration (anch::ObjectMapper concept ; constraints are ignored) +
116 template<typename P, typename... C>
117 FieldsMapper<T>& registerField(const std::string& name, P T::* member, C&&...);
118
130 template<typename P, typename... C>
131 FieldsMapper<T>& registerField(const std::string& name, std::function<P(const T&)> getter, C&&...);
132
141 template<typename P, typename... C>
142 FieldsMapper<T>& registerField(const std::string&, std::function<void(T&, const P&)>, C&&...);
143
155 template<typename P, typename... C>
156 FieldsMapper<T>& registerField(const std::string& name, std::function<P(const T&)> getter, std::function<void(T&, const P&)>, C&&...);
157 // Fields registration -
158 // Methods -
159
160 };
161
169 template<anch::Described T>
170 class FieldsOf {
171
172 // Attributes +
173 private:
175 const T& _object;
176 // Attributes -
177
178 // Constructors +
179 public:
185 explicit FieldsOf(const T& object) noexcept;
186 // Constructors -
187
188 // Methods +
189 public:
195 const T& object() const noexcept;
196 // Methods -
197
198 };
199
214 template<anch::Described T>
215 anch::sql::FieldsOf<T> fieldsOf(const T& object) noexcept;
216
217}
218
219namespace anch::sql {
220
221 // Fields mapper +
222 template<anch::Described T>
223 FieldsMapper<T>::FieldsMapper(): _fields() {
224 anch::describe<T>(*this);
225 }
226
227 template<anch::Described T>
228 const FieldsMapper<T>&
230 static const FieldsMapper<T> mapper;
231 return mapper;
232 }
233
234 template<anch::Described T>
235 const typename FieldsMapper<T>::Field*
236 FieldsMapper<T>::find(std::string_view name) const noexcept {
237 for(const Field& field : _fields) {
238 if(field.name == name) {
239 return &field;
240 }
241 }
242 return nullptr;
243 }
244
245 template<anch::Described T>
246 const std::vector<typename FieldsMapper<T>::Field>&
247 FieldsMapper<T>::fields() const noexcept {
248 return _fields;
249 }
250
251 template<anch::Described T>
252 template<typename P, typename... C>
254 FieldsMapper<T>::registerField(const std::string& name, P T::* member, C&&...) {
255 static_assert(anch::sql::Bindable<std::remove_cvref_t<P>>, "SQL parameters are integers, floating point numbers, booleans, strings, dates,"
256 " UUID, std::optional and lists of them: this field of the description can not be bound");
257 _fields.push_back(Field{name, [member](const T& object) { return anch::sql::toBound(object.*member); }});
258 return *this;
259 }
260
261 template<anch::Described T>
262 template<typename P, typename... C>
264 FieldsMapper<T>::registerField(const std::string& name, std::function<P(const T&)> getter, C&&...) {
265 static_assert(anch::sql::Bindable<std::remove_cvref_t<P>>, "SQL parameters are integers, floating point numbers, booleans, strings, dates,"
266 " UUID, std::optional and lists of them: this field of the description can not be bound");
267 _fields.push_back(Field{name, [getter](const T& object) { return anch::sql::toBound(getter(object)); }});
268 return *this;
269 }
270
271 template<anch::Described T>
272 template<typename P, typename... C>
274 FieldsMapper<T>::registerField(const std::string&, std::function<void(T&, const P&)>, C&&...) {
275 return *this;
276 }
277
278 template<anch::Described T>
279 template<typename P, typename... C>
281 FieldsMapper<T>::registerField(const std::string& name, std::function<P(const T&)> getter, std::function<void(T&, const P&)>, C&&...) {
282 return registerField(name, getter);
283 }
284 // Fields mapper -
285
286 // Fields of an object +
287 template<anch::Described T>
288 FieldsOf<T>::FieldsOf(const T& object) noexcept: _object(object) {
289 // Nothing to do
290 }
291
292 template<anch::Described T>
293 const T&
294 FieldsOf<T>::object() const noexcept {
295 return _object;
296 }
297
298 template<anch::Described T>
300 fieldsOf(const T& object) noexcept {
301 return anch::sql::FieldsOf<T>(object);
302 }
303 // Fields of an object -
304
305}
Parameters of a described type: the common description of objects ( anch::Mapping<T> ,...
Definition fields.hpp:51
static const FieldsMapper< T > & instance()
Definition fields.hpp:229
const std::vector< Field > & fields() const noexcept
Definition fields.hpp:247
const Field * find(std::string_view name) const noexcept
Definition fields.hpp:236
FieldsMapper< T > & registerField(const std::string &name, P T::*member, C &&...)
Definition fields.hpp:254
Fields of an object, bound to the parameters of a query with the same names ( fieldsOf ).
Definition fields.hpp:170
const T & object() const noexcept
Definition fields.hpp:294
FieldsOf(const T &object) noexcept
Definition fields.hpp:288
Type described by an anch::Mapping specialization.
Definition mapping.hpp:66
Definition parameters.hpp:293
SQL namespace.
Definition clauses.hpp:28
anch::sql::BoundValue toBound(const T &value)
Definition parameters.hpp:474
anch::sql::FieldsOf< T > fieldsOf(const T &object) noexcept
Definition fields.hpp:300
AnCH framework base namespace.
Definition app.hpp:30
void describe(M &mapper)
Value bound to a parameter: value, or list of values.
Definition parameters.hpp:318
Field which has a value.
Definition fields.hpp:57
std::string name
Definition fields.hpp:59
std::function< anch::sql::BoundValue(const T &)> read
Definition fields.hpp:62