CppWAMP
C++11 client library for the WAMP protocol
array.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2017, 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_ARRAY_HPP
8 #define CPPWAMP_TYPES_ARRAY_HPP
9 
10 //------------------------------------------------------------------------------
14 //------------------------------------------------------------------------------
15 
16 #include <array>
17 #include "../api.hpp"
18 #include "../error.hpp"
19 #include "../variant.hpp"
20 
21 namespace wamp
22 {
23 
24 //------------------------------------------------------------------------------
27 //------------------------------------------------------------------------------
28 template <typename T, std::size_t Size>
29 CPPWAMP_API void convert(FromVariantConverter& conv, std::array<T, Size>& array)
30 {
31  using namespace wamp;
32  const auto& variant = conv.variant();
33  if (variant.is<Array>() == false)
34  {
35  throw error::Conversion("Attempting to convert non-array variant "
36  "to std::array");
37  }
38 
39  std::array<T, Size> newArray;
40  const auto& variantArray = variant.as<Array>();
41  if (variantArray.size() != Size)
42  {
43  throw error::Conversion("Variant array size does not match that "
44  "of std::array<T," + std::to_string(Size) + ">");
45  }
46 
47  for (Array::size_type i=0; i<variantArray.size(); ++i)
48  {
49  try
50  {
51  newArray[i] = variantArray[i].to<T>();
52  }
53  catch (const error::Conversion& e)
54  {
55  std::string msg = e.what();
56  msg += " (for element #" + std::to_string(i) + ")";
57  throw error::Conversion(msg);
58  }
59  }
60  array = std::move(newArray);
61 }
62 
63 //------------------------------------------------------------------------------
66 //------------------------------------------------------------------------------
67 template <typename T, std::size_t Size>
68 CPPWAMP_API void convert(ToVariantConverter& conv, std::array<T, Size>& array)
69 {
70  using namespace wamp;
71  Array variantArray;
72  for (const auto& elem: array)
73  {
74  variantArray.emplace_back(Variant::from(elem));
75  }
76  conv.variant() = std::move(variantArray);
77 }
78 
79 } // namespace wamp
80 
81 #endif // CPPWAMP_TYPES_ARRAY_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