CppWAMP
C++11 client library for the WAMP protocol
conversionaccess.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_CONVERSION_ACCESS_HPP
8 #define CPPWAMP_CONVERSION_ACCESS_HPP
9 
10 #include <sstream>
11 #include <string>
12 #include <utility>
13 #include "api.hpp"
14 #include "error.hpp"
15 #include "traits.hpp"
16 
17 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
21 
22 //------------------------------------------------------------------------------
23 /* Generates a metafunction that checks for the presence of a member.
24  Adapted from
25  http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector. */
26 //------------------------------------------------------------------------------
27 #define CPPWAMP_GENERATE_HAS_MEMBER(member) \
28  \
29 template <typename T> \
30 class CPPWAMP_API HasMember_##member \
31 { \
32 private: \
33  using Yes = char[2]; \
34  using No = char[1]; \
35  \
36  struct Fallback { int member; }; \
37  struct Derived : T, Fallback { }; \
38  \
39  template <typename U> static No& test ( decltype(U::member)* ); \
40  template <typename U> static Yes& test ( U* ); \
41  \
42 public: \
43  static constexpr bool result = \
44  sizeof(test<Derived>(nullptr)) == sizeof(Yes); \
45 }; \
46  \
47 template < class T > \
48 struct has_member_##member \
49 : public std::integral_constant<bool, HasMember_##member<T>::result> \
50 {};
51 
52 
53 namespace wamp
54 {
55 
56 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 class CPPWAMP_API ConversionAccess
63 {
64 public:
65  template <typename TConverter, typename TObject>
66  static void convert(TConverter& c, TObject& obj)
67  {
68  static_assert(
69  has_member_convert<TObject>(),
70  "The 'convert' function has not been specialized for this type. "
71  "Either provide a 'convert' free function specialization, or "
72  "a 'convert' member function.");
73  obj.convert(c);
74  }
75 
76  template <typename TConverter, typename TObject>
77  static void convertFrom(TConverter& c, TObject& obj)
78  {
79  static_assert(has_member_convertFrom<TObject>(),
80  "The 'convertFrom' member function has not been provided "
81  "for this type.");
82  obj.convertFrom(c);
83  }
84 
85  template <typename TConverter, typename TObject>
86  static void convertTo(TConverter& c, const TObject& obj)
87  {
88  static_assert(has_member_convertTo<TObject>(),
89  "The 'convertTo' member function has not been provided "
90  "for this type.");
91  obj.convertTo(c);
92  }
93 
94  template <typename TObject>
95  static TObject defaultConstruct() {return TObject();}
96 
97  template <typename TObject>
98  static TObject* defaultHeapConstruct() {return new TObject;}
99 
100 private:
101  CPPWAMP_GENERATE_HAS_MEMBER(convert)
102  CPPWAMP_GENERATE_HAS_MEMBER(convertFrom)
103  CPPWAMP_GENERATE_HAS_MEMBER(convertTo)
104 };
105 
106 } // namespace wamp
107 
108 #endif // CPPWAMP_CONVERSION_ACCESS_HPP
api.hpp
Defines macros related to exporting/importing APIs.
wamp::ConversionAccess
Helper class used to gain access to private conversion member functions.
Definition: conversionaccess.hpp:62
wamp
Definition: anyhandler.hpp:36
wamp::convert
void convert(TConverter &c, TValue &val)
General function for converting custom types to/from Variant.
Definition: variant.hpp:709
traits.hpp
Contains general-purpose type traits.
error.hpp
Contains facilities for reporting and describing errors.