AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
codec.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 <cstddef>
26#include <istream>
27#include <limits>
28#include <ostream>
29#include <stdexcept>
30#include <string>
31#include <string_view>
32#include <type_traits>
33
34#include "codec/mediaType.hpp"
35
36namespace anch::codec {
37
48 enum class Side {
53 };
54
63 class ANCH_CODEC_EXPORT ContentTooLarge: public std::length_error {
64 public:
66 using std::length_error::length_error;
67 };
68
81 std::string readContent(std::istream& input, std::size_t maxSize);
82
95 ANCH_CODEC_EXPORT std::size_t& maxBodySize(anch::codec::Side side);
96
109 std::string readBody(std::istream& input, anch::codec::Side side);
110
139 template<typename C, typename T>
140 concept Serializer = requires {
141 { C::mediaType() } -> std::convertible_to<std::string_view>;
142 } && (requires(const T& value, std::ostream& out) { C::serialize(value, out); }
143 || requires(const T& value, std::ostream& out, anch::codec::Side side) { C::serialize(value, out, side); }
144 || requires(const T& value, std::ostream& out, const anch::codec::MediaType& contentType, anch::codec::Side side) {
145 C::serialize(value, out, contentType, side);
146 });
147
156 template<typename C, typename T>
157 concept Deserializer = requires {
158 { C::mediaType() } -> std::convertible_to<std::string_view>;
159 } && (requires(T& value, std::istream& input) { C::deserialize(value, input); }
160 || requires(T& value, std::istream& input, anch::codec::Side side) { C::deserialize(value, input, side); }
161 || requires(T& value, std::istream& input, const anch::codec::MediaType& contentType, anch::codec::Side side) {
162 C::deserialize(value, input, contentType, side);
163 });
164
180 template<typename C, typename T>
181 requires anch::codec::Serializer<C, T>
183
198 template<typename C, typename T>
200 void serialize(const T& value, std::ostream& out, const anch::codec::MediaType& contentType, anch::codec::Side side);
201
216 template<typename C, typename T>
218 void deserialize(T& value, std::istream& input, const anch::codec::MediaType& contentType, anch::codec::Side side);
219
220}
221
222#include "codec/impl/codec.hpp"
Definition codec.hpp:63
Definition mediaType.hpp:41
Definition codec.hpp:157
Definition codec.hpp:140
AnCH codec library namespace.
Definition csv.hpp:31
Side
Definition codec.hpp:48
@ SERVER
Definition codec.hpp:50
@ CLIENT
Definition codec.hpp:52
anch::codec::MediaType contentTypeOf(const T &value)
std::size_t & maxBodySize(anch::codec::Side side)
void deserialize(T &value, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)
std::string readContent(std::istream &input, std::size_t maxSize)
void serialize(const T &value, std::ostream &out, const anch::codec::MediaType &contentType, anch::codec::Side side)
std::string readBody(std::istream &input, anch::codec::Side side)