mqtt_cpp
variant.hpp
Go to the documentation of this file.
1 // Copyright Takatoshi Kondo 2018
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(MQTT_VARIANT_HPP)
8 #define MQTT_VARIANT_HPP
9 
10 #include <mqtt/config.hpp>
11 #include <mqtt/namespace.hpp>
12 
13 #if defined(MQTT_STD_VARIANT)
14 
15 #include <variant>
16 
17 namespace MQTT_NS {
18 
19 using std::variant;
20 
21 template<typename T, typename U>
22 decltype(auto) variant_get(U && arg)
23 {
24  return std::get<T>(std::forward<U>(arg));
25 }
26 
27 template<typename T>
28 decltype(auto) variant_idx(T const& arg)
29 {
30  return arg.index();
31 }
32 
33 using std::visit;
34 
35 } // namespace MQTT_NS
36 
37 #else // defined(MQTT_STD_VARIANT)
38 
39 #include <boost/variant.hpp>
40 #include <boost/variant/get.hpp>
41 #include <boost/variant/apply_visitor.hpp>
42 
43 namespace MQTT_NS {
44 
45 using boost::variant;
46 
47 template<typename T, typename U>
48 decltype(auto) variant_get(U && arg)
49 {
50  return boost::get<T>(std::forward<U>(arg));
51 }
52 
53 template<typename T>
54 decltype(auto) variant_idx(T const& arg)
55 {
56  return arg.which();
57 }
58 
59 template <typename Visitor, typename... Variants>
60 constexpr decltype(auto) visit(Visitor&& vis, Variants&&... vars)
61 {
62  return boost::apply_visitor(std::forward<Visitor>(vis), std::forward<Variants>(vars)...);
63 }
64 
65 } // namespace MQTT_NS
66 
67 #endif // defined(MQTT_STD_VARIANT)
68 
69 #endif // MQTT_VARIANT_HPP
Definition: any.hpp:27
constexpr decltype(auto) visit(Visitor &&vis, Variants &&... vars)
Definition: variant.hpp:60
decltype(auto) variant_get(U &&arg)
Definition: variant.hpp:48
decltype(auto) variant_idx(T const &arg)
Definition: variant.hpp:54