VariantChannelMap.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #ifndef LVR2_TYPES_VARIANTCHANNELMAP
31 #define LVR2_TYPES_VARIANTCHANNELMAP
32 
33 #include <unordered_map>
34 #include <iostream>
35 #include <utility>
36 #include <memory>
37 #include <vector>
38 #include "VariantChannel.hpp"
39 
40 namespace lvr2 {
41 
42 template<typename... T>
44 : public std::unordered_map<std::string, VariantChannel<T...> >
45 {
46 public:
47  using key_type = std::string;
48  using val_type = VariantChannel<T...>;
49  using elem_type = std::pair<const key_type, val_type>;
50 
51  using types = std::tuple<T...>;
52  using base = std::unordered_map<std::string, VariantChannel<T...> >;
53  using base::base;
54 
60  template<class U>
61  struct index_of_type {
62  static constexpr std::size_t value = val_type::template index_of_type<U>::value;
63  };
64 
65  template <std::size_t N>
66  using type_of_index = typename val_type::template type_of_index<N>;
67 
68  static constexpr std::size_t num_types = val_type::num_types;
69 
70  template<typename U>
71  struct iterator {
72 
73  using resolved_elem_type = std::pair<const key_type&, Channel<U>& >;
74  using pointer = std::shared_ptr<resolved_elem_type>;
75  // using pointer = elem_type*;
76  using reference = elem_type&;
77 
79  typename base::iterator base_it,
80  typename base::iterator end_it)
81  :m_base_it(base_it),
82  m_end_it(end_it)
83  {
84  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
85  {
86  m_base_it++;
87  }
88  }
89 
90  resolved_elem_type operator*() const noexcept
91  {
92  return {
93  m_base_it->first,
94  m_base_it->second.template extract<U>()
95  };
96  }
97 
98  pointer operator->() const noexcept
99  {
100  return pointer(
101  new resolved_elem_type({
102  m_base_it->first,
103  m_base_it->second.template extract<U>()
104  })
105  );
106  }
107 
109  {
110  m_base_it++;
111  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
112  {
113  m_base_it++;
114  }
115  return *this;
116  }
117 
118  iterator<U> operator++(int) noexcept
119  {
120  iterator<U> tmp(*this);
121  m_base_it++;
122  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
123  {
124  m_base_it++;
125  }
126  return tmp;
127  }
128 
129  inline bool operator==(const typename base::iterator& rhs) noexcept
130  {
131  return m_base_it == rhs;
132  }
133 
134  inline bool operator!=(const typename base::iterator& rhs) noexcept
135  {
136  return m_base_it != rhs;
137  }
138 
139  typename base::iterator operator()() const noexcept
140  {
141  return m_base_it;
142  }
143 
144  typename base::iterator m_base_it;
145  typename base::iterator m_end_it;
146  };
147 
148 
149  template<typename U>
150  struct const_iterator {
151 
152  using resolved_elem_type = std::pair<const key_type&, const Channel<U>& >;
153  using pointer = std::shared_ptr<resolved_elem_type>;
154  // using pointer = elem_type*;
156 
158  typename base::const_iterator base_it,
159  typename base::const_iterator end_it)
160  :m_base_it(base_it),
161  m_end_it(end_it)
162  {
163  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
164  {
165  m_base_it++;
166  }
167  }
168 
169  resolved_elem_type operator*() const noexcept
170  {
171  return {
172  m_base_it->first,
173  m_base_it->second.template extract<U>()
174  };
175  }
176 
177  pointer operator->() const noexcept
178  {
179  return pointer(
180  new resolved_elem_type({
181  m_base_it->first,
182  m_base_it->second.template extract<U>()
183  })
184  );
185  }
186 
188  {
189  m_base_it++;
190  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
191  {
192  m_base_it++;
193  }
194  return *this;
195  }
196 
198  {
199  iterator<U> tmp(*this);
200  m_base_it++;
201  while(m_base_it != m_end_it && m_base_it->second.which() != index_of_type<U>::value)
202  {
203  m_base_it++;
204  }
205  return tmp;
206  }
207 
208  inline bool operator==(const typename base::const_iterator& rhs) noexcept
209  {
210  return m_base_it == rhs;
211  }
212 
213  inline bool operator!=(const typename base::const_iterator& rhs) noexcept
214  {
215  return m_base_it != rhs;
216  }
217 
218  typename base::iterator operator()() const noexcept
219  {
220  return m_base_it;
221  }
222 
223  typename base::const_iterator m_base_it;
224  typename base::const_iterator m_end_it;
225  };
226 
234  template<typename U>
235  void add(const std::string& name, Channel<U> channel);
236 
237 
243  template<typename U>
244  void add(const std::string& name);
245 
253  template<typename U>
254  void add(const std::string& name, size_t numElements, size_t width);
255 
261  template<typename U>
262  Channel<U>& get(const std::string& name);
263 
270  template<typename U>
271  const Channel<U>& get(const std::string& name) const;
272 
273 
274  template<typename U>
275  typename Channel<U>::Optional getOptional(const std::string& name);
276 
277  template<typename U>
278  const typename Channel<U>::Optional getOptional(const std::string& name) const;
279 
286  int type(const std::string& name) const;
287 
297  template<typename U>
298  bool is_type(const std::string& name) const;
299 
307  template<typename U>
308  std::vector<std::string> keys();
309 
318  template<typename U>
319  size_t numChannels();
320 
321  template<typename U>
323  {
324  typename base::iterator it_base = this->begin();
325  typename base::iterator it_end = this->end();
326  return iterator<U>(it_base, it_end);
327  }
328 
329  template<typename U>
331  {
332  typename base::const_iterator it_base = this->begin();
333  typename base::const_iterator it_end = this->end();
334  return const_iterator<U>(it_base, it_end);
335  }
336 
337  using base::erase;
338 
339  template<typename U>
341  {
342  typename base::iterator it_base = erase(it());
343  typename base::iterator it_end = this->end();
344  return iterator<U>(it_base, it_end);
345  }
346 
347  template<typename V>
348  VariantChannelMap<T...> manipulate(V visitor)
349  {
350  VariantChannelMap<T...> cm;
351  for(auto vchannel: *this)
352  {
353  cm.insert({vchannel.first, boost::apply_visitor(visitor, vchannel.second)});
354  }
355  return cm;
356  }
357 
358  VariantChannelMap<T...> clone() const;
359 
364  friend std::ostream& operator<<(std::ostream& os, const VariantChannelMap<T...>& cm)
365  {
366  std::cout << "[ VariantChannelMap ]\n";
367  for(auto it : cm)
368  {
369  std::cout << it.first << ": " << it.second << "\n";
370  }
371  return os;
372  }
373 };
374 
375 } // namespace lvr2
376 
377 #include "VariantChannelMap.tcc"
378 
379 #endif // LVR2_TYPES_VARIANTCHANNELMAP
380 
VariantChannel.hpp
lvr2::VariantChannelMap::const_iterator::operator++
const_iterator< U > & operator++() noexcept
Definition: VariantChannelMap.hpp:187
lvr2::Channel::Optional
boost::optional< Channel< T > > Optional
Definition: Channel.hpp:45
lvr2::VariantChannelMap::index_of_type::value
static constexpr std::size_t value
Definition: VariantChannelMap.hpp:62
lvr2::VariantChannelMap::keys
std::vector< std::string > keys()
Gets the available keys by a specific type.
lvr2::VariantChannelMap::iterator::operator++
iterator< U > & operator++() noexcept
Definition: VariantChannelMap.hpp:108
lvr2::VariantChannelMap::iterator
Definition: VariantChannelMap.hpp:71
lvr2::VariantChannelMap::types
std::tuple< T... > types
Definition: VariantChannelMap.hpp:51
lvr2::VariantChannelMap::iterator::reference
elem_type & reference
Definition: VariantChannelMap.hpp:76
lvr2::VariantChannelMap::const_iterator::pointer
std::shared_ptr< resolved_elem_type > pointer
Definition: VariantChannelMap.hpp:153
lvr2::VariantChannelMap::const_iterator::const_iterator
const_iterator(typename base::const_iterator base_it, typename base::const_iterator end_it)
Definition: VariantChannelMap.hpp:157
lvr2::VariantChannelMap::const_iterator::operator!=
bool operator!=(const typename base::const_iterator &rhs) noexcept
Definition: VariantChannelMap.hpp:213
lvr2::VariantChannelMap::const_iterator::resolved_elem_type
std::pair< const key_type &, const Channel< U > & > resolved_elem_type
Definition: VariantChannelMap.hpp:152
lvr2::VariantChannelMap::iterator::operator!=
bool operator!=(const typename base::iterator &rhs) noexcept
Definition: VariantChannelMap.hpp:134
lvr2::VariantChannelMap::const_iterator::operator->
pointer operator->() const noexcept
Definition: VariantChannelMap.hpp:177
lvr2::VariantChannelMap::iterator::resolved_elem_type
std::pair< const key_type &, Channel< U > & > resolved_elem_type
Definition: VariantChannelMap.hpp:73
lvr2::VariantChannelMap::numChannels
size_t numChannels()
Counts the number of channels by a specific type. @detail For total number of channels use "size()".
lvr2::VariantChannelMap::is_type
bool is_type(const std::string &name) const
Checks if key has specific type U.
lvr2::VariantChannelMap::const_iterator::operator()
base::iterator operator()() const noexcept
Definition: VariantChannelMap.hpp:218
lvr2::VariantChannelMap::operator<<
friend std::ostream & operator<<(std::ostream &os, const VariantChannelMap< T... > &cm)
Output cout.
Definition: VariantChannelMap.hpp:364
lvr2::VariantChannelMap::const_iterator::reference
elem_type & reference
Definition: VariantChannelMap.hpp:155
lvr2::VariantChannelMap::iterator::pointer
std::shared_ptr< resolved_elem_type > pointer
Definition: VariantChannelMap.hpp:74
lvr2::VariantChannelMap::typedBegin
const_iterator< U > typedBegin() const
Definition: VariantChannelMap.hpp:330
lvr2::VariantChannelMap::iterator::operator->
pointer operator->() const noexcept
Definition: VariantChannelMap.hpp:98
lvr2::VariantChannelMap::const_iterator::operator*
resolved_elem_type operator*() const noexcept
Definition: VariantChannelMap.hpp:169
lvr2::VariantChannelMap::getOptional
Channel< U >::Optional getOptional(const std::string &name)
lvr2::VariantChannelMap::type_of_index
typename val_type::template type_of_index< N > type_of_index
Definition: VariantChannelMap.hpp:66
lvr2::VariantChannelMap::typedBegin
iterator< U > typedBegin()
Definition: VariantChannelMap.hpp:322
lvr2::VariantChannelMap
Definition: VariantChannelMap.hpp:43
lvr2::VariantChannel::num_types
static constexpr std::size_t num_types
Definition: VariantChannel.hpp:39
lvr2::VariantChannelMap::get
Channel< U > & get(const std::string &name)
Gets AttributeChannel with type U from map as reference.
lvr2::VariantChannelMap::clone
VariantChannelMap< T... > clone() const
lvr2::VariantChannelMap::const_iterator::m_end_it
base::const_iterator m_end_it
Definition: VariantChannelMap.hpp:224
lvr2::VariantChannelMap::iterator::operator()
base::iterator operator()() const noexcept
Definition: VariantChannelMap.hpp:139
lvr2::VariantChannelMap::num_types
static constexpr std::size_t num_types
Definition: VariantChannelMap.hpp:68
lvr2::VariantChannelMap::iterator::m_base_it
base::iterator m_base_it
Definition: VariantChannelMap.hpp:144
lvr2::Channel
Definition: Channel.hpp:42
lvr2::VariantChannelMap::iterator::operator++
iterator< U > operator++(int) noexcept
Definition: VariantChannelMap.hpp:118
lvr2::VariantChannelMap::const_iterator
Definition: VariantChannelMap.hpp:150
lvr2::VariantChannelMap::iterator::m_end_it
base::iterator m_end_it
Definition: VariantChannelMap.hpp:145
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::VariantChannel
Definition: VariantChannel.hpp:18
lvr2::VariantChannelMap::const_iterator::m_base_it
base::const_iterator m_base_it
Definition: VariantChannelMap.hpp:223
lvr2::VariantChannelMap::manipulate
VariantChannelMap< T... > manipulate(V visitor)
Definition: VariantChannelMap.hpp:348
lvr2::VariantChannelMap::add
void add(const std::string &name, Channel< U > channel)
Adds an Key + AttributeChannel to the map.
lvr2::VariantChannelMap::elem_type
std::pair< const key_type, val_type > elem_type
Definition: VariantChannelMap.hpp:49
lvr2::VariantChannelMap::iterator::operator==
bool operator==(const typename base::iterator &rhs) noexcept
Definition: VariantChannelMap.hpp:129
lvr2::VariantChannelMap::iterator::operator*
resolved_elem_type operator*() const noexcept
Definition: VariantChannelMap.hpp:90
lvr2::VariantChannelMap::const_iterator::operator++
const_iterator< U > operator++(int) noexcept
Definition: VariantChannelMap.hpp:197
lvr2::VariantChannelMap::const_iterator::operator==
bool operator==(const typename base::const_iterator &rhs) noexcept
Definition: VariantChannelMap.hpp:208
lvr2::VariantChannelMap::iterator::iterator
iterator(typename base::iterator base_it, typename base::iterator end_it)
Definition: VariantChannelMap.hpp:78
lvr2::VariantChannelMap::erase
iterator< U > erase(iterator< U > it)
Definition: VariantChannelMap.hpp:340
lvr2::VariantChannelMap::type
int type(const std::string &name) const
Gets type index of a map entry.
lvr2::VariantChannelMap::base
std::unordered_map< std::string, VariantChannel< T... > > base
Definition: VariantChannelMap.hpp:52
lvr2::VariantChannelMap::index_of_type
Access type index by type.
Definition: VariantChannelMap.hpp:61
lvr2::VariantChannelMap::key_type
std::string key_type
Definition: VariantChannelMap.hpp:47


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