AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::codec Namespace Reference

AnCH codec library namespace. More...

Namespaces

namespace  media
 Common media types.

Classes

struct  Codecs
class  ContentTooLarge
struct  Format
struct  FormatCodec
 Codec of a data format: media type, options of each side ( FormatPolicy ), serialization and deserialization ( Serializer , Deserializer concepts). More...
struct  FormatPolicy
 Policy of a format codec: options of each side and errors translation. More...
struct  FormatPolicy< anch::csv::CsvFormat >
 Policy of the CSV codec (anch-codec-csv plugin, option ANCH_CODEC_CSV ). More...
struct  FormatPolicy< anch::json::JsonFormat >
 Policy of the JSON codec (anch-codec-json plugin, option ANCH_CODEC_JSON ). More...
class  MediaType
struct  Protobuf
struct  ProtobufJson
struct  Text
struct  ThriftCodec

Concepts

concept  DataFormat
 Data format which serializes values with options (anch-json anch::json::JsonFormat , anch-csv anch::csv::CsvFormat , ...).
concept  Deserializer
concept  FormatDeserializer
 Format which deserializes T values.
concept  FormatSerializer
 Format which serializes T values.
concept  ProtobufMessage
concept  Serializer
concept  ThriftStruct

Typedefs

using Csv = anch::codec::FormatCodec<anch::csv::CsvFormat>
using Json = anch::codec::FormatCodec<anch::json::JsonFormat>
using ThriftBinary = anch::codec::ThriftCodec<anch::codec::ThriftProtocol::BINARY>
using ThriftCompact = anch::codec::ThriftCodec<anch::codec::ThriftProtocol::COMPACT>
using ThriftJson = anch::codec::ThriftCodec<anch::codec::ThriftProtocol::JSON>

Enumerations

enum class  Side { SERVER , CLIENT }
enum class  ThriftProtocol { BINARY , COMPACT , JSON }

Functions

std::string readContent (std::istream &input, std::size_t maxSize)
std::size_t & maxBodySize (anch::codec::Side side)
std::string readBody (std::istream &input, anch::codec::Side side)
template<typename C, typename T>
requires anch::codec::Serializer<C, T>
anch::codec::MediaType contentTypeOf (const T &value)
template<typename C, typename T>
requires anch::codec::Serializer<C, T>
void serialize (const T &value, std::ostream &out, const anch::codec::MediaType &contentType, anch::codec::Side side)
template<typename C, typename T>
requires anch::codec::Deserializer<C, T>
void deserialize (T &value, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)
bool registerFormat (anch::codec::Format format)
std::vector< anch::codec::Format > formats ()
std::optional< std::size_t > negotiate (const std::vector< anch::codec::MediaType > &accept, const std::vector< anch::codec::MediaType > &available)
std::string_view thriftMediaType (anch::codec::ThriftProtocol protocol) noexcept
std::optional< anch::codec::ThriftProtocol > thriftProtocolOf (const anch::codec::MediaType &mediaType)
std::shared_ptr< apache::thrift::protocol::TProtocol > thriftProtocol (anch::codec::ThriftProtocol protocol, std::shared_ptr< apache::thrift::transport::TTransport > transport)
std::string thriftWrite (anch::codec::ThriftProtocol protocol, const std::function< void(apache::thrift::protocol::TProtocol *)> &write)
void thriftRead (anch::codec::ThriftProtocol protocol, std::string_view content, const std::function< void(apache::thrift::protocol::TProtocol *)> &read)

Detailed Description

AnCH codec library namespace.

It contains codecs concepts (Serializer, Deserializer), media types (MediaType) and their negotiation, the Text codec and the codecs of format plugins (Json, ...). See AnCH codec library documentation.

Typedef Documentation

◆ Csv

text/csv codec based on anch-csv (anch-codec-csv plugin, option ANCH_CODEC_CSV ): values are lists of described objects ( anch::Mapping , anch-utils), one record per object. Options of each side: see FormatPolicy.

Since
0.3

◆ Json

application/json codec based on anch-json (anch-codec-json plugin, option ANCH_CODEC_JSON ): values need a description ( anch::Mapping , anch-utils). Options of each side: see FormatPolicy.

Since
0.3 (anch::rest::Json in 0.2)

◆ ThriftBinary

application/vnd.apache.thrift.binary codec (see ThriftCodec )

◆ ThriftCompact

application/vnd.apache.thrift.compact codec (see ThriftCodec )

◆ ThriftJson

application/vnd.apache.thrift.json codec (see ThriftCodec )

Enumeration Type Documentation

◆ Side

enum class anch::codec::Side
strong

Trust policy of a codec user: what is checked and bounded when values are read and written.
SERVER reads data it does not control and trusts what it builds ; CLIENT checks what it sends and trusts what it reads. Codecs can behave according to it: anch::codec::Json checks constraints of values read with SERVER and of values written with CLIENT , and bounds what SERVER reads.
anch-rest servers use SERVER and clients CLIENT ; a message consumer reads with SERVER and a producer writes with CLIENT .

Since
0.2
Enumerator
SERVER 

Read values are checked and bounded, written values are trusted (HTTP server, message consumer)

CLIENT 

Written values are checked, read values are trusted (HTTP client, message producer)

◆ ThriftProtocol

enum class anch::codec::ThriftProtocol
strong

Thrift protocols

Since
0.2
Enumerator
BINARY 

Binary protocol: application/vnd.apache.thrift.binary ( application/x-thrift for RPC)

COMPACT 

Compact protocol: application/vnd.apache.thrift.compact

JSON 

JSON protocol: application/vnd.apache.thrift.json

Function Documentation

◆ contentTypeOf()

template<typename C, typename T>
requires anch::codec::Serializer<C, T>
anch::codec::MediaType anch::codec::contentTypeOf ( const T & value)

Get content type of a value serialized with codec: its contentType(value) function is used when it is defined, mediaType() otherwise

Template Parameters
Cthe codec type
Tthe value type
Parameters
valuethe value
Returns
the media type
Exceptions
std::invalid_argumentwhen codec media type is invalid
Since
0.2
Here is the call graph for this function:

◆ deserialize()

template<typename C, typename T>
requires anch::codec::Deserializer<C, T>
void anch::codec::deserialize ( T & value,
std::istream & input,
const anch::codec::MediaType & contentType,
anch::codec::Side side )

Deserialize value with codec: its deserialize(value, input, contentType, side) function is used when it is defined, then deserialize(value, input, side) , then deserialize(value, input)

Template Parameters
Cthe codec type
Tthe value type
Parameters
valuethe value to fill
inputthe input stream
contentTypethe content media type, with its parameters
sidethe side which deserializes value
Since
0.3 (anch::rest::deserializeBody in 0.2)
Here is the call graph for this function:

◆ formats()

std::vector< anch::codec::Format > anch::codec::formats ( )

Get registered formats (thread safe)

Returns
the formats sorted by name
Since
0.3
Here is the call graph for this function:

◆ maxBodySize()

std::size_t & anch::codec::maxBodySize ( anch::codec::Side side)

Maximum size of contents read in memory by codecs which have no limit of their own (Protobuf, Protobuf JSON, Thrift), to change before reading contents (0 for no limit).
Default: 2 MiB for SERVER , as JSON and form bodies ; no limit for CLIENT (anch-rest responses are bounded by transports: TransportConfig::maxResponseSize ).

Parameters
sidethe side
Returns
the maximum size in bytes
Since
0.3
Here is the call graph for this function:

◆ negotiate()

std::optional< std::size_t > anch::codec::negotiate ( const std::vector< anch::codec::MediaType > & accept,
const std::vector< anch::codec::MediaType > & available )

Select the best available media type for Accept media ranges (RFC 9110 proactive negotiation): the quality of an available media type is the quality of the most specific media range which includes it. On equal quality, the first available media type is selected.

Parameters
acceptthe Accept media ranges ; empty means that every media type is accepted
availablethe available media types
Returns
the selected media type index, empty when no media type is acceptable
Here is the call graph for this function:

◆ readBody()

std::string anch::codec::readBody ( std::istream & input,
anch::codec::Side side )

Read whole content with the limit of a side (maxBodySize )

Parameters
inputthe input stream
sidethe side
Returns
the content
Exceptions
ContentTooLargewhen content is larger than maxBodySize
Since
0.3
Here is the call graph for this function:

◆ readContent()

std::string anch::codec::readContent ( std::istream & input,
std::size_t maxSize )

Read whole content

Parameters
inputthe input stream
maxSizethe maximum content size in bytes
Returns
the content
Exceptions
ContentTooLargewhen content is larger than maxSize
Since
0.2
Here is the call graph for this function:

◆ registerFormat()

bool anch::codec::registerFormat ( anch::codec::Format format)

Register format. A format which is already registered (same name, from several shared libraries for example) is not registered twice.

Parameters
formatthe format
Returns
true when format has been registered, false when it was already registered
Since
0.3
Here is the call graph for this function:

◆ serialize()

template<typename C, typename T>
requires anch::codec::Serializer<C, T>
void anch::codec::serialize ( const T & value,
std::ostream & out,
const anch::codec::MediaType & contentType,
anch::codec::Side side )

Serialize value with codec: its serialize(value, out, contentType, side) function is used when it is defined, then serialize(value, out, side) , then serialize(value, out)

Template Parameters
Cthe codec type
Tthe value type
Parameters
valuethe value
outthe output stream
contentTypethe content media type (from contentTypeOf )
sidethe side which serializes value
Since
0.3 (anch::rest::serializeBody in 0.2)
Here is the call graph for this function:

◆ thriftMediaType()

std::string_view anch::codec::thriftMediaType ( anch::codec::ThriftProtocol protocol)
noexcept

Get protocol media type

Parameters
protocolthe protocol
Returns
the media type
Since
0.2
Here is the call graph for this function:

◆ thriftProtocol()

std::shared_ptr< apache::thrift::protocol::TProtocol > anch::codec::thriftProtocol ( anch::codec::ThriftProtocol protocol,
std::shared_ptr< apache::thrift::transport::TTransport > transport )

Create Thrift protocol over a transport

Parameters
protocolthe protocol
transportthe transport
Returns
the protocol
Since
0.2
Here is the call graph for this function:

◆ thriftProtocolOf()

std::optional< anch::codec::ThriftProtocol > anch::codec::thriftProtocolOf ( const anch::codec::MediaType & mediaType)

Get protocol of a media type ( application/x-thrift is the binary protocol, as Thrift HTTP transports)

Parameters
mediaTypethe media type
Returns
the protocol, empty when media type is not a Thrift media type
Since
0.2
Here is the call graph for this function:

◆ thriftRead()

void anch::codec::thriftRead ( anch::codec::ThriftProtocol protocol,
std::string_view content,
const std::function< void(apache::thrift::protocol::TProtocol *)> & read )

Read structure with protocol

Parameters
protocolthe protocol
contentthe encoded structure
readthe structure reader
Exceptions
std::invalid_argumentwhen content is not a valid structure (missing required field, ...)
Since
0.2
Here is the call graph for this function:

◆ thriftWrite()

std::string anch::codec::thriftWrite ( anch::codec::ThriftProtocol protocol,
const std::function< void(apache::thrift::protocol::TProtocol *)> & write )

Write structure with protocol

Parameters
protocolthe protocol
writethe structure writer
Returns
the encoded structure
Since
0.2
Here is the call graph for this function: