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) 2026 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 <chrono>
23#include <cstddef>
24#include <functional>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <type_traits>
29#include <vector>
30
31#include "mapping.hpp"
32
33#include "csv/csvError.hpp"
34#include "csv/options.hpp"
35#include "csv/reader.hpp"
36#include "csv/writer.hpp"
37#include "val/constraints.hpp"
38
39#ifdef ANCH_UUID
40#include "uuid.hpp"
41#endif
42#ifdef ANCH_DATE
43#include "date/dateTime.hpp"
44#endif
45
46namespace anch::csv {
47
59 template<typename V>
60 concept Scalar = std::same_as<V, std::string> || std::same_as<V, bool>
61 || (std::integral<V> && !std::same_as<V, char> && !std::same_as<V, wchar_t> && !std::same_as<V, char8_t>
62 && !std::same_as<V, char16_t> && !std::same_as<V, char32_t>)
63 || std::floating_point<V>
64#ifdef ANCH_UUID
65 || std::same_as<V, anch::UUID>
66#endif
67#ifdef ANCH_DATE
68 || std::same_as<V, anch::date::DateTime> || std::same_as<V, anch::date::Date> || std::same_as<V, anch::date::Time>
69#endif
70 ;
71
79 template<typename P>
81 || (anch::val::isNullable<P>() && std::same_as<P, std::optional<anch::val::detail::Unwrap<P>>>
83
84}
85
86namespace anch::csv::detail {
87
97 template<anch::csv::Scalar V>
98 bool parse(std::string_view text, V& value, const anch::csv::Options& options);
99
108 template<anch::csv::Scalar V>
109 std::string format(const V& value, const anch::csv::Options& options);
110
114 struct ReadContext {
116 bool enabled;
117
119 const std::string& name;
120
122 std::size_t line;
123
125 std::size_t column;
126 };
127
131 struct WriteContext {
133 bool enabled;
134
136 const std::string& name;
137
139 std::size_t line;
140
142 std::size_t column;
143 };
144
146 using FieldText = std::optional<std::string>;
147
148}
149
150namespace anch::val {
151
155 template<>
156 struct CheckContext<anch::csv::detail::ReadContext> {
158 static bool
159 enabled(const anch::csv::detail::ReadContext& context) noexcept {
160 return context.enabled;
161 }
162
169 [[noreturn]] static void
170 violation(const anch::csv::detail::ReadContext& context, const std::string& message) {
171 throw anch::csv::CsvError(anch::csv::ErrorCode::CONSTRAINT_VIOLATION, context.name + ": " + message, context.line, context.column);
172 }
173
175 template<typename F>
176 static void
177 item(anch::csv::detail::ReadContext&, std::string_view, F&& check) {
178 std::forward<F>(check)();
179 }
180
182 template<typename F>
183 static void
184 item(anch::csv::detail::ReadContext&, std::size_t, F&& check) {
185 std::forward<F>(check)();
186 }
187 };
188
192 template<>
193 struct CheckContext<anch::csv::detail::WriteContext> {
195 static bool
196 enabled(const anch::csv::detail::WriteContext& context) noexcept {
197 return context.enabled;
198 }
199
206 [[noreturn]] static void
207 violation(const anch::csv::detail::WriteContext& context, const std::string& message) {
208 throw anch::csv::CsvError(anch::csv::ErrorCode::CONSTRAINT_VIOLATION, context.name + ": " + message, context.line, context.column);
209 }
210
212 template<typename F>
213 static void
214 item(anch::csv::detail::WriteContext&, std::string_view, F&& check) {
215 std::forward<F>(check)();
216 }
217
219 template<typename F>
220 static void
221 item(anch::csv::detail::WriteContext&, std::size_t, F&& check) {
222 std::forward<F>(check)();
223 }
224 };
225
226}
227
228namespace anch::csv {
229
244 template<typename T>
246
247 public:
249 struct Field {
251 std::string name;
252
255
257 std::function<void(T&, std::string_view, bool, anch::csv::detail::ReadContext&, const anch::csv::Options&)> read;
258
260 std::function<anch::csv::detail::FieldText(const T&, anch::csv::detail::WriteContext&, const anch::csv::Options&)> write;
261 };
262
263 // Attributes +
264 private:
266 std::vector<Field> _fields;
267 // Attributes -
268
269 // Constructors +
270 public:
275 // Constructors -
276
277 // Methods +
278 public:
284 static const ObjectMapper<T>& instance();
285
291 const std::vector<Field>& fields() const noexcept;
292
302 template<typename P, typename... C>
303 requires (anch::val::FieldConstraint<C, P> && ...)
304 ObjectMapper<T>& registerField(const std::string& name, P T::* member, C&&... constraints);
305
315 template<typename P, typename... C>
316 requires (anch::val::FieldConstraint<C, P> && ...)
317 ObjectMapper<T>& registerField(const std::string& name, std::function<P(const T&)> getter, C&&... constraints);
318
328 template<typename P, typename... C>
329 requires (anch::val::FieldConstraint<C, P> && ...)
330 ObjectMapper<T>& registerField(const std::string& name, std::function<void(T&, const P&)> setter, C&&... constraints);
331
342 template<typename P, typename... C>
343 requires (anch::val::FieldConstraint<C, P> && ...)
344 ObjectMapper<T>& registerField(const std::string& name, std::function<P(const T&)> getter,
345 std::function<void(T&, const P&)> setter, C&&... constraints);
346 // Methods -
347
348 private:
357 template<typename P, typename S, typename... C>
358 static auto reader(S set, C&&... constraints);
359
368 template<typename P, typename G, typename... C>
369 static auto writer(G get, C&&... constraints);
370
371 };
372
390 template<typename T>
392
393 // Attributes +
394 private:
396 anch::csv::Reader& _reader;
397
399 const anch::csv::ObjectMapper<T>& _mapper;
400
402 std::vector<std::size_t> _columns;
403 // Attributes -
404
405 public:
407 static constexpr std::size_t NONE = static_cast<std::size_t>(-1);
408
409 // Constructors +
410 public:
419 // Constructors -
420
421 // Methods +
422 public:
432 bool read(T& value);
433 // Methods -
434
435 };
436
452 template<typename T>
454
455 // Attributes +
456 private:
458 anch::csv::Writer& _writer;
459
461 const anch::csv::ObjectMapper<T>& _mapper;
462
464 bool _header;
465
467 bool _positional;
468
470 std::size_t _records;
471 // Attributes -
472
473 // Constructors +
474 public:
481 explicit ObjectWriter(anch::csv::Writer& writer, bool header = true);
482 // Constructors -
483
484 // Methods +
485 public:
493 void write(const T& value);
494
500 // Methods -
501
502 };
503
504}
505
506#include "csv/impl/mapper.hpp"
Definition csvError.hpp:62
CSV mapper of a described type ( anch::Mapping , anch-utils): fields are columns.
Definition mapper.hpp:245
ObjectMapper< T > & registerField(const std::string &name, P T::*member, C &&... constraints)
static const ObjectMapper< T > & instance()
const std::vector< Field > & fields() const noexcept
static constexpr std::size_t NONE
Definition mapper.hpp:407
ObjectReader(anch::csv::Reader &reader)
void write(const T &value)
ObjectWriter(anch::csv::Writer &writer, bool header=true)
Definition reader.hpp:50
Definition writer.hpp:111
Mapper of a format on which anch::Mapping registers the fields of T.
Definition mapping.hpp:88
Type of a CSV field: Scalar or std::optional of a scalar.
Definition mapper.hpp:80
Type of a CSV field value: string, boolean, integer, floating point number, anch::UUID (with ANCH_UUI...
Definition mapper.hpp:60
AnCH CSV library namespace.
@ CONSTRAINT_VIOLATION
Definition csvError.hpp:51
AnCH validation library namespace.
Definition mapper.hpp:150
constexpr bool isNullable()
Definition constraints.hpp:1205
AnCH framework base namespace.
Definition app.hpp:30
Field mapping.
Definition mapper.hpp:249
bool required
Definition mapper.hpp:254
std::string name
Definition mapper.hpp:251
std::function< void(T &, std::string_view, bool, anch::csv::detail::ReadContext &, const anch::csv::Options &)> read
Definition mapper.hpp:257
std::function< anch::csv::detail::FieldText(const T &, anch::csv::detail::WriteContext &, const anch::csv::Options &)> write
Definition mapper.hpp:260
Definition options.hpp:35
Context in which constraints are checked.
Definition constraints.hpp:331