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
lvr2::VariantChannel::DataPtrVisitor
Definition: VariantChannel.hpp:105
lvr2::VariantChannel::types
std::tuple< T... > types
Definition: VariantChannel.hpp:27
lvr2::VariantChannel::dataPtr
boost::shared_array< type_of_index< N > > dataPtr() const
Definition: VariantChannel.hpp:52
lvr2::VariantChannel::WidthVisitor::operator()
size_t operator()(const Channel< U > &channel) const
Definition: VariantChannel.hpp:98
lvr2::VariantChannel::CloneVisitor
Definition: VariantChannel.hpp:123
lvr2::VariantChannel::NumElementsVisitor
Definition: VariantChannel.hpp:86
lvr2::VariantChannel::operator<<
friend std::ostream & operator<<(std::ostream &os, const VariantChannel< T... > &ch)
Definition: VariantChannel.hpp:76
lvr2::VariantChannel::type
int type() const
Get type index of a map entry.
lvr2::VariantChannel::NumElementsVisitor::operator()
size_t operator()(const Channel< U > &channel) const
Definition: VariantChannel.hpp:89
lvr2::VariantChannel::WidthVisitor
Definition: VariantChannel.hpp:95
lvr2::VariantChannel::clone
VariantChannel< T... > clone() const
lvr2::VariantChannel::type_of_index
typename std::tuple_element< N, types >::type type_of_index
Definition: VariantChannel.hpp:42
Channel.hpp
lvr2::VariantChannel::is_type
bool is_type() const
lvr2::Channel::dataPtr
const DataPtr dataPtr() const
lvr2::Channel::clone
Channel< T > clone() const
lvr2::VariantChannel::num_types
static constexpr std::size_t num_types
Definition: VariantChannel.hpp:39
lvr2::VariantChannel::numElements
size_t numElements() const
lvr2::VariantChannel::index_of_type::value
static constexpr std::size_t value
Definition: VariantChannel.hpp:36
lvr2::VariantChannel::base
boost::variant< Channel< T >... > base
Definition: VariantChannel.hpp:20
lvr2::Channel
Definition: Channel.hpp:42
std
Definition: HalfEdge.hpp:124
lvr2::VariantChannel::dataPtr
boost::shared_array< U > dataPtr() const
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::VariantChannel::CloneVisitor::operator()
VariantChannel< T... > operator()(const Channel< U > &channel) const
Definition: VariantChannel.hpp:126
lvr2::VariantChannel
Definition: VariantChannel.hpp:18
lvr2::VariantChannel::extract
Channel< U > extract() const
lvr2::Channel::width
size_t width() const
lvr2::VariantChannel::DataPtrVisitor::operator()
boost::shared_array< U > operator()(const Channel< V > &channel) const
Definition: VariantChannel.hpp:109
lvr2::VariantChannel::index_of_type
Access type index with type.
Definition: VariantChannel.hpp:35
lvr2::VariantChannel::TupleIndex
Definition: VariantChannel.hpp:24
lvr2::VariantChannelOptional
boost::optional< VariantChannel< Tp... > > VariantChannelOptional
Definition: VariantChannel.hpp:145
lvr2::VariantChannel::width
size_t width() const
lvr2::Channel::numElements
size_t numElements() const


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 Wed Mar 2 2022 00:37:25