collection.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef HECTOR_POSE_ESTIMATION_COLLECTION_H
30 #define HECTOR_POSE_ESTIMATION_COLLECTION_H
31 
32 #include <list>
33 #include <map>
34 
35 #include <boost/shared_ptr.hpp>
36 #include <boost/weak_ptr.hpp>
37 #include <boost/make_shared.hpp>
38 
40 
41 template <typename T, typename key_type = std::string>
42 class Collection {
43 public:
45  typedef boost::weak_ptr<T> WPtr;
46  typedef std::list<Ptr> ListType; // lists do no invalidate iterators on insert operations
47  typedef std::map<key_type, WPtr> MapType;
48  typedef typename ListType::const_iterator iterator;
49  typedef typename ListType::const_iterator const_iterator;
50 
51  const Ptr& add(const Ptr& p, const key_type& key) {
52  list_.push_back(p);
53  map_[key] = p;
54  return list_.back();
55  }
56 
57  template <typename Derived> boost::shared_ptr<Derived> add(Derived *p) { return boost::static_pointer_cast<Derived>(add(Ptr(p), p->getName())); }
58  template <typename Derived> boost::shared_ptr<Derived> add(Derived *p, const key_type& key) { return boost::static_pointer_cast<Derived>(add(Ptr(p), key)); }
59 
60  template <typename Derived> const Ptr& create() {
61  Derived *p = new Derived();
62  return add(p);
63  }
64 
65  template <typename Derived> const Ptr& create(const key_type& key) {
66  Derived *p = new Derived();
67  return add(p, key);
68  }
69 
70 // Ptr get(std::size_t index) const {
71 // if (index >= size()) return Ptr();
72 // return list_[index];
73 // }
74 
75  Ptr get(const key_type& key) const {
76  if (!map_.count(key)) return Ptr();
77  return map_.at(key).lock();
78  }
79 
80 // template <typename Derived>
81 // boost::shared_ptr<Derived> getType(std::size_t index) const {
82 // return boost::dynamic_pointer_cast<Derived>(get(index));
83 // }
84 
85  template <typename Derived>
86  boost::shared_ptr<Derived> getType(const key_type& key) const {
87  return boost::dynamic_pointer_cast<Derived>(get(key));
88  }
89 
90  bool empty() const { return list_.empty(); }
91  std::size_t size() const { return list_.size(); }
92  const_iterator begin() const { return list_.begin(); }
93  const_iterator end() const { return list_.end(); }
94 
95  void clear() {
96  map_.clear();
97  list_.clear();
98  }
99 
100 private:
101  ListType list_;
102  MapType map_;
103 };
104 
105 }
106 
107 #endif // HECTOR_POSE_ESTIMATION_COLLECTION_H
boost::shared_ptr< T > Ptr
Definition: collection.h:44
boost::shared_ptr< Derived > add(Derived *p, const key_type &key)
Definition: collection.h:58
const Ptr & add(const Ptr &p, const key_type &key)
Definition: collection.h:51
const_iterator end() const
Definition: collection.h:93
const_iterator begin() const
Definition: collection.h:92
std::map< key_type, WPtr > MapType
Definition: collection.h:47
boost::shared_ptr< Derived > add(Derived *p)
Definition: collection.h:57
ListType::const_iterator iterator
Definition: collection.h:48
boost::weak_ptr< T > WPtr
Definition: collection.h:45
std::size_t size() const
Definition: collection.h:91
boost::shared_ptr< Derived > getType(const key_type &key) const
Definition: collection.h:86
ListType::const_iterator const_iterator
Definition: collection.h:49
const Ptr & create(const key_type &key)
Definition: collection.h:65


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Thu Feb 18 2021 03:29:30