VariantChannel.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef LVR2_TYPES_VARIANTCHANNEL
4 #define LVR2_TYPES_VARIANTCHANNEL
5 
6 #include <boost/variant.hpp>
7 #include <tuple>
8 #include <memory>
9 #include <stdexcept>
10 #include <iostream>
11 
12 #include "Channel.hpp"
13 
14 namespace lvr2 {
15 
16 
17 template<typename... T>
18 class VariantChannel : public boost::variant<Channel<T>...>
19 {
20  using base = boost::variant<Channel<T>...>;
21  using base::base;
22 protected:
23  template <class T1, class Tuple>
24  struct TupleIndex;
25 
26 public:
27  using types = std::tuple<T...>;
28 
34  template<class U>
35  struct index_of_type {
36  static constexpr std::size_t value = TupleIndex<U, types>::value;
37  };
38 
39  static constexpr std::size_t num_types = std::tuple_size<types>::value;
40 
41  template <std::size_t N>
42  using type_of_index = typename std::tuple_element<N, types>::type;
43 
44  size_t numElements() const;
45 
46  size_t width() const;
47 
48  template<typename U>
49  boost::shared_array<U> dataPtr() const;
50 
51  template<std::size_t N>
52  boost::shared_array<type_of_index<N> > dataPtr() const
53  {
54  return boost::apply_visitor(DataPtrVisitor<type_of_index<N> >(), *this);
55  }
56 
61  int type() const;
62 
63  template<typename U>
64  Channel<U> extract() const;
65 
66  template<typename U>
68 
73  template<typename U>
74  bool is_type() const;
75 
76  friend std::ostream& operator<<(std::ostream& os, const VariantChannel<T...>& ch)
77  {
78  os << "type: " << ch.type() << ", " << static_cast <const base &>(ch);
79  return os;
80  }
81 
82  VariantChannel<T...> clone() const;
83 
84 // Visitor Implementations
85 protected:
86  struct NumElementsVisitor : public boost::static_visitor<size_t>
87  {
88  template<typename U>
89  size_t operator()(const Channel<U>& channel) const
90  {
91  return channel.numElements();
92  }
93  };
94 
95  struct WidthVisitor : public boost::static_visitor<size_t>
96  {
97  template<typename U>
98  size_t operator()(const Channel<U>& channel) const
99  {
100  return channel.width();
101  }
102  };
103 
104  template<typename U>
105  struct DataPtrVisitor : public boost::static_visitor<boost::shared_array<U> >
106  {
107  template<typename V,
108  std::enable_if_t<std::is_same<U, V>::value, int> = 0>
109  boost::shared_array<U> operator()(const Channel<V>& channel) const
110  {
111  return channel.dataPtr();
112  }
113 
114  template<typename V,
115  std::enable_if_t<!std::is_same<U, V>::value, int> = 0>
116  boost::shared_array<U> operator()(const Channel<V>& channel) const
117  {
118  throw std::invalid_argument("tried to get wrong type of channel");
119  return boost::shared_array<U>();
120  }
121  };
122 
123  struct CloneVisitor : public boost::static_visitor< VariantChannel<T...> >
124  {
125  template<typename U>
126  VariantChannel<T...> operator()(const Channel<U>& channel) const
127  {
128  return channel.clone();
129  }
130  };
131 
132  template <class T1, class... Types>
133  struct TupleIndex<T1, std::tuple<T1, Types...>> {
134  static constexpr std::size_t value = 0;
135  };
136 
137  template <class T1, class U, class... Types>
138  struct TupleIndex<T1, std::tuple<U, Types...>> {
139  static constexpr std::size_t value = 1 + TupleIndex<T1, std::tuple<Types...>>::value;
140  };
141 
142 };
143 
144 template<typename ...Tp>
145 using VariantChannelOptional = boost::optional<VariantChannel<Tp...> >;
146 
147 
148 } // namespace lvr2
149 
150 #include "VariantChannel.tcc"
151 
152 #endif // LVR2_TYPES_VARIANTCHANNEL
size_t operator()(const Channel< U > &channel) const
size_t width() const
Access type index with type.
boost::shared_array< type_of_index< N > > dataPtr() const
const DataPtr dataPtr() const
std::tuple< T... > types
size_t numElements() const
Channel< U > extract() const
VariantChannel< T... > clone() const
VariantChannel< T... > operator()(const Channel< U > &channel) const
int type() const
Get type index of a map entry.
boost::optional< VariantChannel< Tp... > > VariantChannelOptional
static constexpr std::size_t value
size_t operator()(const Channel< U > &channel) const
boost::variant< Channel< T >... > base
friend std::ostream & operator<<(std::ostream &os, const VariantChannel< T... > &ch)
bool is_type() const
boost::shared_array< U > operator()(const Channel< V > &channel) const
boost::shared_array< U > dataPtr() const
typename std::tuple_element< N, types >::type type_of_index
Channel< T > clone() const
size_t numElements() const
size_t width() const
static constexpr std::size_t num_types


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:09