AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
formatCodec.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 "anch-codec-export.hpp"
23
24#include <concepts>
25#include <exception>
26#include <istream>
27#include <ostream>
28#include <string_view>
29
30#include "codec/codec.hpp"
31
32namespace anch::codec {
33
47 template<typename F>
48 concept DataFormat = requires {
49 typename F::Options;
50 { F::MEDIA_TYPE } -> std::convertible_to<std::string_view>;
51 { F::defaultOptions() } -> std::convertible_to<const typename F::Options&>;
52 };
53
62 template<typename F, typename T>
64 && requires(const T& value, std::ostream& out, const typename F::Options& options) { F::serialize(value, out, options); };
65
74 template<typename F, typename T>
76 && requires(T& value, std::istream& input, const typename F::Options& options) { F::deserialize(value, input, options); };
77
89 template<anch::codec::DataFormat F>
90 struct FormatPolicy {
97 static void
98 configure([[maybe_unused]] typename F::Options& options, [[maybe_unused]] anch::codec::Side side) {
99 // Default options for both sides
100 }
101
107 static void
108 translate([[maybe_unused]] const std::exception& error) {
109 // Error is rethrown
110 }
111 };
112
129 template<anch::codec::DataFormat F>
130 struct ANCH_CODEC_EXPORT FormatCodec {
131
133 using Options = typename F::Options;
134
140 static std::string_view mediaType() noexcept;
141
150 static Options& options(anch::codec::Side side);
151
158 template<typename T>
159 requires anch::codec::FormatSerializer<F, T>
160 static void serialize(const T& value, std::ostream& out);
161
169 template<typename T>
170 requires anch::codec::FormatSerializer<F, T>
171 static void serialize(const T& value, std::ostream& out, anch::codec::Side side);
172
179 template<typename T>
180 requires anch::codec::FormatDeserializer<F, T>
181 static void deserialize(T& value, std::istream& input);
182
192 template<typename T>
193 requires anch::codec::FormatDeserializer<F, T>
194 static void deserialize(T& value, std::istream& input, anch::codec::Side side);
195
196 private:
204 template<typename T>
205 static void read(T& value, std::istream& input, const Options& options);
206
207 };
208
209}
210
211#include "codec/impl/formatCodec.hpp"
Data format which serializes values with options (anch-json anch::json::JsonFormat ,...
Definition formatCodec.hpp:48
Format which deserializes T values.
Definition formatCodec.hpp:75
Format which serializes T values.
Definition formatCodec.hpp:63
AnCH codec library namespace.
Definition csv.hpp:31
Side
Definition codec.hpp:48
AnCH framework base namespace.
Definition app.hpp:30
Codec of a data format: media type, options of each side ( FormatPolicy ), serialization and deserial...
Definition formatCodec.hpp:130
static Options & options(anch::codec::Side side)
static void serialize(const T &value, std::ostream &out)
typename F::Options Options
Definition formatCodec.hpp:133
static void deserialize(T &value, std::istream &input)
static std::string_view mediaType() noexcept
Policy of a format codec: options of each side and errors translation.
Definition formatCodec.hpp:90
static void configure(typename F::Options &options, anch::codec::Side side)
Definition formatCodec.hpp:98
static void translate(const std::exception &error)
Definition formatCodec.hpp:108