CppWAMP
C++11 client library for the WAMP protocol
set.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_TYPES_SET_HPP
8 #define CPPWAMP_TYPES_SET_HPP
9 
10 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
14 
15 #include <set>
16 #include "../api.hpp"
17 #include "../error.hpp"
18 #include "../variant.hpp"
19 
20 namespace wamp
21 {
22 
23 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
27 template <typename T>
28 void CPPWAMP_API convert(FromVariantConverter& conv, std::set<T>& set)
29 {
30  const auto& variant = conv.variant();
31  if (!variant.is<Array>())
32  {
33  throw error::Conversion("Attempting to convert non-array variant "
34  "to std::set");
35  }
36 
37  std::set<T> newSet;
38  const auto& array = variant.as<Array>();
39  for (Array::size_type i=0; i<array.size(); ++i)
40  {
41  try
42  {
43  newSet.emplace(array[i].to<T>());
44  }
45  catch (const error::Conversion& e)
46  {
47  std::string msg = e.what();
48  msg += " (for element #" + std::to_string(i) + ")";
49  throw error::Conversion(msg);
50  }
51  }
52  set = std::move(newSet);
53 }
54 
55 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
59 template <typename T>
60 void CPPWAMP_API convert(ToVariantConverter& conv, std::set<T>& set)
61 {
62  Array array;
63  for (const auto& elem: set)
64  {
65  array.emplace_back(Variant::from(elem));
66  }
67  conv.variant() = std::move(array);
68 }
69 
70 } // namespace wamp
71 
72 #endif // CPPWAMP_TYPES_SET_HPP
wamp::FromVariantConverter::variant
const Variant & variant() const
Returns a constant reference to the wrapped variant.
Definition: variant.hpp:1384
wamp::TypeId::array
@ array
For Variant::Array.
wamp::ToVariantConverter::variant
Variant & variant()
Returns a reference to the wrapped variant.
Definition: variant.hpp:1208
wamp
Definition: anyhandler.hpp:36
wamp::ToVariantConverter
Wrapper around a destination Variant, used for conversions.
Definition: variant.hpp:604
wamp::Array
std::vector< Variant > Array
Variant bound type for arrays of variants.
Definition: variantdefs.hpp:51
wamp::FromVariantConverter
Wrapper around a source Variant, used for conversions.
Definition: variant.hpp:650
wamp::convert
void convert(TConverter &c, TValue &val)
General function for converting custom types to/from Variant.
Definition: variant.hpp:709
wamp::error::Conversion
Exception type thrown when converting a Variant to an invalid type.
Definition: error.hpp:106
wamp::Variant::from
static Variant from(TValue &&value)
Constructs a variant from a custom type.
Definition: variant.hpp:744