collection.h
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // lists do no invalidate iterators on insert operations
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 p;
00055   }
00056 
00057   template <typename Derived> boost::shared_ptr<Derived> add(Derived *p) { return boost::shared_static_cast<Derived>(add(Ptr(p), p->getName())); }
00058   template <typename Derived> boost::shared_ptr<Derived> add(Derived *p, const key_type& key) { return boost::shared_static_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 //  Ptr get(std::size_t index) const {
00071 //    if (index >= size()) return Ptr();
00072 //    return list_[index];
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 //  template <typename Derived>
00081 //  boost::shared_ptr<Derived> getType(std::size_t index) const {
00082 //    return boost::shared_dynamic_cast<Derived>(get(index));
00083 //  }
00084 
00085   template <typename Derived>
00086   boost::shared_ptr<Derived> getType(const key_type& key) const {
00087     return boost::shared_dynamic_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


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 00:24:15