CppWAMP
C++11 client library for the WAMP protocol
variantencoding.hpp
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 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_INTERNAL_VARIANTENCODING_HPP
8 #define CPPWAMP_INTERNAL_VARIANTENCODING_HPP
9 
10 #include <jsoncons/byte_string.hpp>
11 #include <jsoncons/sink.hpp>
12 #include "../codec.hpp"
13 #include "../variant.hpp"
14 
15 namespace wamp
16 {
17 
18 namespace internal
19 {
20 
21 //------------------------------------------------------------------------------
22 template <typename TEncoder>
23 class VariantEncodingVisitor : public Visitor<>
24 {
25 public:
26  explicit VariantEncodingVisitor(TEncoder& encoder)
27  : encoder_(encoder)
28  {}
29 
30  void operator()(Null)
31  {
32  encoder_.null_value();
33  }
34 
35  void operator()(Bool b)
36  {
37  encoder_.bool_value(b);
38  }
39 
40  void operator()(Int n)
41  {
42  encoder_.int64_value(n);
43  }
44 
45  void operator()(UInt n)
46  {
47  encoder_.uint64_value(n);
48  }
49 
50  void operator()(Real x)
51  {
52  encoder_.double_value(x);
53  }
54 
55  void operator()(const String& s)
56  {
57  encoder_.string_value({s.data(), s.size()});
58  }
59 
60  void operator()(const Blob& b)
61  {
62  jsoncons::byte_string_view bsv(b.data().data(), b.data().size());
63  encoder_.byte_string_value(bsv);
64  }
65 
66  void operator()(const Array& a)
67  {
68  encoder_.begin_array(a.size());
69  for (const auto& v: a)
70  wamp::apply(*this, v);
71  encoder_.end_array();
72  }
73 
74  void operator()(const Object& o)
75  {
76  encoder_.begin_object(o.size());
77  for (const auto& kv: o)
78  {
79  const auto& key = kv.first;
80  encoder_.key({key.data(), key.size()});
81  wamp::apply(*this, kv.second);
82  }
83  encoder_.end_object();
84  }
85 
86 private:
87  TEncoder& encoder_;
88 };
89 
90 } // namespace internal
91 
92 } // namespace wamp
93 
94 #endif // CPPWAMP_INTERNAL_VARIANTENCODING_HPP
wamp::UInt
std::uint64_t UInt
Variant bound type for unsigned integers.
Definition: variantdefs.hpp:48
wamp::Object
std::map< String, Variant > Object
Variant bound type for maps of variants.
Definition: variantdefs.hpp:52
wamp
Definition: anyhandler.hpp:36
wamp::Array
std::vector< Variant > Array
Variant bound type for arrays of variants.
Definition: variantdefs.hpp:51
wamp::Int
std::int64_t Int
Variant bound type for signed integers.
Definition: variantdefs.hpp:47
wamp::Bool
bool Bool
Variant bound type for boolean values.
Definition: variantdefs.hpp:46
wamp::Real
double Real
Variant bound type for floating-point numbers.
Definition: variantdefs.hpp:49
wamp::String
std::string String
Variant bound type for text strings.
Definition: variantdefs.hpp:50