CppWAMP
C++11 client library for the WAMP protocol
codec.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_CODEC_HPP
8 #define CPPWAMP_CODEC_HPP
9 
10 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
14 
15 #include <istream>
16 #include <ostream>
17 #include "api.hpp"
18 #include "config.hpp"
19 #include "messagebuffer.hpp"
20 #include "traits.hpp"
21 #include "variant.hpp"
22 
23 namespace wamp
24 {
25 
26 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
33 struct CPPWAMP_API KnownCodecIds
34 {
35  static constexpr int json() {return 1;}
36  static constexpr int msgpack() {return 2;}
37  static constexpr int cbor() {return 3;}
38 };
39 
40 
41 //******************************************************************************
42 // Encoder
43 //******************************************************************************
44 
48 
51 
54 
57 template <typename O, typename Enabled = void>
59 {
61 };
62 
63 template <typename O>
64 struct OutputCategory<O, EnableIf<std::is_base_of<std::ostream, O>::value,
65  void>>
66 {
67  using type = StreamOutputCategory;
68 };
69 
70 template <typename O>
71 struct OutputCategory<O, EnableIf<!std::is_base_of<std::ostream, O>::value,
72  void>>
73 {
74  using type = ByteContainerOutputCategory;
75 };
76 
78 template <typename O>
80 
81 //------------------------------------------------------------------------------
89 //------------------------------------------------------------------------------
90 template <typename F, typename O, typename C = OutputCategoryTypeOf<O>>
91 using Encoder = typename F::template Encoder<ValueTypeOf<O>, C>;
92 
93 //------------------------------------------------------------------------------
101 //------------------------------------------------------------------------------
102 template <typename TFormat, typename TOutput>
103 void encode(const Variant& variant, TOutput&& output)
104 {
106  encoder.encode(variant, std::forward<TOutput>(output));
107 }
108 
109 
110 //******************************************************************************
111 // Decoder
112 //******************************************************************************
113 
117 
120 
123 
126 template <typename I, typename Enabled = void>
128 {
129  using type = UnknownOutputCategory;
130 };
131 
132 template <typename I>
133 struct InputCategory<I, EnableIf<std::is_base_of<std::istream, I>::value,
134  void>>
135 {
136  using type = StreamInputCategory;
137 };
138 
139 template <typename I>
140 struct InputCategory<I, EnableIf<!std::is_base_of<std::istream, I>::value,
141  void>>
142 {
143  using type = ByteArrayInputCategory;
144 };
145 
147 template <typename I>
149 
150 //------------------------------------------------------------------------------
158 //------------------------------------------------------------------------------
159 template <typename F, typename I, typename C = InputCategoryTypeOf<I>>
160 using Decoder = typename F::template Decoder<ValueTypeOf<I>, C>;
161 
162 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 template <typename TFormat, typename TInput>
172 CPPWAMP_NODISCARD std::error_code decode(TInput&& input, Variant& variant)
173 {
174  Decoder<TFormat, TInput> decoder;
175  return decoder.decode(std::forward<TInput>(input), variant);
176 }
177 
178 } // namespace wamp
179 
180 #endif // CPPWAMP_CODEC_HPP
wamp::ByteArrayInputCategory
Input category for contiguous byte arrays which provide data and size member functions.
Definition: codec.hpp:116
wamp::StreamOutputCategory
Output category for output streams of bytes.
Definition: codec.hpp:50
wamp::Variant
Discriminated union container that represents a JSON value.
Definition: variant.hpp:134
messagebuffer.hpp
Contains the MessageBuffer definition.
api.hpp
Defines macros related to exporting/importing APIs.
wamp::InputCategory
Traits class that determines the category type that best matches the given input type.
Definition: codec.hpp:127
wamp::UnknownOutputCategory
Type used to indicate output category detection failed.
Definition: codec.hpp:53
wamp::encode
void encode(const Variant &variant, TOutput &&output)
Encodes the given variant to the given byte container or stream.
Definition: codec.hpp:103
wamp::StreamInputCategory
Input category for input streams of bytes.
Definition: codec.hpp:119
wamp
Definition: anyhandler.hpp:36
wamp::OutputCategory
Traits class that determines the category type that best matches the given output type.
Definition: codec.hpp:58
wamp::OutputCategoryTypeOf
typename OutputCategory< ValueTypeOf< O > >::type OutputCategoryTypeOf
Yields the category type that best matches the given output type.
Definition: codec.hpp:79
wamp::InputCategoryTypeOf
typename InputCategory< ValueTypeOf< I > >::type InputCategoryTypeOf
Yields the category type that best matches the given input type.
Definition: codec.hpp:148
wamp::ByteContainerOutputCategory
Output category for containers of bytes which provide push_back and insert member functions.
Definition: codec.hpp:47
wamp::EnableIf
typename std::enable_if< B, T >::type EnableIf
Metafunction used to enable overloads based on a boolean condition.
Definition: traits.hpp:37
variant.hpp
Contains the declaration of Variant and other closely related types/functions.
wamp::Encoder
typename F::template Encoder< ValueTypeOf< O >, C > Encoder
Yields the encoder type needed to encode a Variant to the given output type and output category.
Definition: codec.hpp:91
wamp::decode
std::error_code decode(TInput &&input, Variant &variant)
Decodes from the given byte sequence or stream to the given variant.
Definition: codec.hpp:172
wamp::KnownCodecIds
IDs used by rawsocket transports to negotiate the serializer.
Definition: codec.hpp:33
traits.hpp
Contains general-purpose type traits.
wamp::Decoder
typename F::template Decoder< ValueTypeOf< I >, C > Decoder
Yields the decoder type needed to decode a Variant from the given input type and input category.
Definition: codec.hpp:160
wamp::UnknownInputCategory
Type used to indicate input category detection failed.
Definition: codec.hpp:122