variant.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Raphael Grimm (raphael dot grimm at kit dot edu)
20 * @date 2019
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <variant>
28 #include <boost/variant.hpp>
29 
30 namespace boost
31 {
32  template<int N, class... Ts> using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
33 
34  template<int N, class... Ts>
35  auto& get_index(boost::variant<Ts...>& v)
36  {
37  using target = NthTypeOf<N, Ts...>;
38  return boost::get<target>(v);
39  }
40 
41  template<int N, class... Ts>
42  const auto& get_index(const boost::variant<Ts...>& v)
43  {
44  using target = NthTypeOf<N, Ts...>;
45  return boost::get<target>(v);
46  }
47 }
48 
49 namespace armarx
50 {
51  template<int N, class... Ts> using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
52 
53  template<int N, class... Ts>
54  auto* get_index_ptr(boost::variant<Ts...>& v)
55  {
56  using target = NthTypeOf<N, Ts...>;
57  return boost::get<target>(&v);
58  }
59 
60  template<int N, class... Ts>
61  const auto* get_index_ptr(const boost::variant<Ts...>& v)
62  {
63  using target = NthTypeOf<N, Ts...>;
64  return boost::get<const target>(&v);
65  }
66 
67 
68  template<int N, class... Ts>
69  auto* get_index_ptr(std::variant<Ts...>& v)
70  {
71  using target = NthTypeOf<N, Ts...>;
72  return std::holds_alternative<target>(v) ?
73  &std::get<target>(v) :
74  nullptr;
75  }
76 
77  template<int N, class... Ts>
78  const auto* get_index_ptr(const std::variant<Ts...>& v)
79  {
80  using target = NthTypeOf<N, Ts...>;
81  return std::holds_alternative<const target>(v) ?
82  &std::get<const target>(v) :
83  nullptr;
84  }
85 }
boost::get_index
auto & get_index(boost::variant< Ts... > &v)
Definition: variant.h:35
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
boost
Definition: ApplicationOptions.h:37
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::get_index_ptr
auto * get_index_ptr(boost::variant< Ts... > &v)
Definition: variant.h:54
armarx::NthTypeOf
typename std::tuple_element< N, std::tuple< Ts... > >::type NthTypeOf
Definition: variant.h:51
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
boost::NthTypeOf
typename std::tuple_element< N, std::tuple< Ts... > >::type NthTypeOf
Definition: variant.h:32