hpp-core  4.9.0
Implement basic classes for canonical path planning for kinematic chains.
container.hh
Go to the documentation of this file.
1 // Copyright (c) 2015, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-core.
5 // hpp-core is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-core is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-core. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HPP_CORE_CONTAINER_HH
18 # define HPP_CORE_CONTAINER_HH
19 
20 # include <map>
21 # include <list>
22 # include <stdexcept>
23 
24 # include <boost/smart_ptr/shared_ptr.hpp>
25 
26 # include <boost/mpl/inherit_linearly.hpp>
27 # include <boost/mpl/fold.hpp>
28 # include <boost/mpl/inherit.hpp>
29 # include <boost/mpl/vector.hpp>
30 # include <boost/mpl/for_each.hpp>
31 # include <boost/type_traits/is_pointer.hpp>
32 # include <boost/type_traits/remove_pointer.hpp>
33 
34 # include <hpp/core/config.hh>
35 
36 namespace hpp {
37  namespace core {
39  namespace internal {
40  template <typename T> struct is_pointer : boost::is_pointer<T> {};
41  template <typename T> struct is_pointer<boost::shared_ptr<T> > : boost::true_type {};
42  template <typename T> struct remove_pointer : boost::remove_pointer<T> {};
43  template <typename T> struct remove_pointer<boost::shared_ptr<T> > { typedef T type; };
44  template <typename T> struct remove_pointer<const boost::shared_ptr<T> > { typedef T type; };
45 
46  template <bool deref_ptr> struct deref {
47  template <typename T> static inline T get (T t) { return t; }
48  };
49  template <> struct deref <true> {
50  template <typename T> static inline typename remove_pointer<T>::type get (T t) { return *t; }
51  };
52  }
54 
55  template <typename Types, typename Key = std::string > struct Container
56  {
57  typedef std::map <Key, Types> Map_t;
58  typedef typename Map_t::value_type value_type;
59  typedef typename Map_t::key_type key_type;
60  typedef typename Map_t::mapped_type mapped_type;
61  typedef typename Map_t::const_iterator const_iterator;
62  typedef typename Map_t:: iterator iterator;
63 
64  Map_t map;
65 
67  void erase (const Key& name) { map.erase (name); }
69  void clear () { map.clear (); }
71  void add (const key_type& name, const mapped_type& element)
72  {
73  std::pair<iterator, bool> ret = map.insert( value_type(name, element));
74  if (!ret.second)
75  ret.first->second = element;
76  }
78  bool has (const key_type& name) const { return (map.find (name) != map.end ()); }
79 
81  const mapped_type& get (const key_type& name) const
82  {
83  const_iterator _e = map.find (name);
84  if (_e == map.end ()) {
85  std::stringstream ss; ss << "Invalid key: " << name;
86  throw std::invalid_argument (ss.str());
87  }
88  return _e->second;
89  }
90 
92  const mapped_type& get (const key_type& name, const mapped_type& defaultValue) const
93  {
94  const_iterator _e = map.find (name);
95  if (_e == map.end ()) return defaultValue;
96  return _e->second;
97  }
98 
101  template <typename ReturnType>
102  ReturnType getAllAs () const
103  {
104  ReturnType l;
105  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
106  l.push_back (_e->second);
107  return l;
108  }
109 
110  template <typename ReturnType>
111  ReturnType getKeys () const
112  {
113  ReturnType l;
114  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
115  l.push_back (_e->first);
116  return l;
117  }
118 
120  std::ostream& print (std::ostream& os) const
121  {
122  typedef internal::is_pointer<mapped_type> should_deref;
123  typedef internal::deref<should_deref::value> deref;
124  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
125  os << _e->first << ": "
126  << deref::template get<const mapped_type> (_e->second)
127  << std::endl;
128  return os;
129  }
130  };
131  } // namespace core
132 } // namespace hpp
133 #endif // HPP_CORE_CONTAINER_HH
ReturnType getAllAs() const
Definition: container.hh:102
ReturnType getKeys() const
Definition: container.hh:111
std::ostream & print(std::ostream &os) const
Print object in a stream.
Definition: container.hh:120
Map_t::key_type key_type
Definition: container.hh:59
bool has(const key_type &name) const
Return the element named name.
Definition: container.hh:78
Definition: container.hh:55
Map_t::iterator iterator
Definition: container.hh:62
std::map< Key, Types > Map_t
Definition: container.hh:57
Map_t::mapped_type mapped_type
Definition: container.hh:60
void clear()
Clear content of container.
Definition: container.hh:69
pinocchio::value_type value_type
Definition: fwd.hh:157
void add(const key_type &name, const mapped_type &element)
Add an element.
Definition: container.hh:71
Map_t::const_iterator const_iterator
Definition: container.hh:61
Map_t map
Definition: container.hh:64
Transform3f t
void erase(const Key &name)
Erase the element named name.
Definition: container.hh:67
Map_t::value_type value_type
Definition: container.hh:58