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 
const_iterator(typename base::const_iterator base_it, typename base::const_iterator end_it)
int type(const std::string &name) const
Gets type index of a map entry.
std::tuple< T... > types
iterator(typename base::iterator base_it, typename base::iterator end_it)
static constexpr std::size_t num_types
pointer operator->() const noexcept
bool operator!=(const typename base::iterator &rhs) noexcept
std::pair< const key_type &, const Channel< U > &> resolved_elem_type
std::unordered_map< std::string, VariantChannel< T... > > base
typename val_type::template type_of_index< N > type_of_index
base::iterator operator()() const noexcept
std::pair< const key_type, val_type > elem_type
Channel< U >::Optional getOptional(const std::string &name)
resolved_elem_type operator*() const noexcept
friend std::ostream & operator<<(std::ostream &os, const VariantChannelMap< T... > &cm)
Output cout.
iterator< U > operator++(int) noexcept
const_iterator< U > typedBegin() const
void add(const std::string &name, Channel< U > channel)
Adds an Key + AttributeChannel to the map.
std::shared_ptr< resolved_elem_type > pointer
iterator< U > & operator++() noexcept
iterator< U > erase(iterator< U > it)
const_iterator< U > & operator++() noexcept
base::iterator operator()() const noexcept
boost::optional< Channel< T > > Optional
Definition: Channel.hpp:45
resolved_elem_type operator*() const noexcept
std::pair< const key_type &, Channel< U > &> resolved_elem_type
bool is_type(const std::string &name) const
Checks if key has specific type U.
VariantChannelMap< T... > manipulate(V visitor)
std::shared_ptr< resolved_elem_type > pointer
std::vector< std::string > keys()
Gets the available keys by a specific type.
bool operator==(const typename base::iterator &rhs) noexcept
bool operator==(const typename base::const_iterator &rhs) noexcept
VariantChannelMap< T... > clone() const
static constexpr std::size_t num_types
const_iterator< U > operator++(int) noexcept
size_t numChannels()
Counts the number of channels by a specific type. For total number of channels use "size()"...
static constexpr std::size_t value
bool operator!=(const typename base::const_iterator &rhs) noexcept


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