Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef HECTOR_POSE_ESTIMATION_COLLECTION_H
00030 #define HECTOR_POSE_ESTIMATION_COLLECTION_H
00031
00032 #include <list>
00033 #include <map>
00034
00035 #include <boost/shared_ptr.hpp>
00036 #include <boost/weak_ptr.hpp>
00037 #include <boost/make_shared.hpp>
00038
00039 namespace hector_pose_estimation {
00040
00041 template <typename T, typename key_type = std::string>
00042 class Collection {
00043 public:
00044 typedef boost::shared_ptr<T> Ptr;
00045 typedef boost::weak_ptr<T> WPtr;
00046 typedef std::list<Ptr> ListType;
00047 typedef std::map<key_type, WPtr> MapType;
00048 typedef typename ListType::const_iterator iterator;
00049 typedef typename ListType::const_iterator const_iterator;
00050
00051 const Ptr& add(const Ptr& p, const key_type& key) {
00052 list_.push_back(p);
00053 map_[key] = p;
00054 return list_.back();
00055 }
00056
00057 template <typename Derived> boost::shared_ptr<Derived> add(Derived *p) { return boost::static_pointer_cast<Derived>(add(Ptr(p), p->getName())); }
00058 template <typename Derived> boost::shared_ptr<Derived> add(Derived *p, const key_type& key) { return boost::static_pointer_cast<Derived>(add(Ptr(p), key)); }
00059
00060 template <typename Derived> const Ptr& create() {
00061 Derived *p = new Derived();
00062 return add(p);
00063 }
00064
00065 template <typename Derived> const Ptr& create(const key_type& key) {
00066 Derived *p = new Derived();
00067 return add(p, key);
00068 }
00069
00070
00071
00072
00073
00074
00075 Ptr get(const key_type& key) const {
00076 if (!map_.count(key)) return Ptr();
00077 return map_.at(key).lock();
00078 }
00079
00080
00081
00082
00083
00084
00085 template <typename Derived>
00086 boost::shared_ptr<Derived> getType(const key_type& key) const {
00087 return boost::dynamic_pointer_cast<Derived>(get(key));
00088 }
00089
00090 bool empty() const { return list_.empty(); }
00091 std::size_t size() const { return list_.size(); }
00092 const_iterator begin() const { return list_.begin(); }
00093 const_iterator end() const { return list_.end(); }
00094
00095 void clear() {
00096 map_.clear();
00097 list_.clear();
00098 }
00099
00100 private:
00101 ListType list_;
00102 MapType map_;
00103 };
00104
00105 }
00106
00107 #endif // HECTOR_POSE_ESTIMATION_COLLECTION_H