hpp-core  4.12.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 <stdexcept>
22 # include <type_traits>
23 
24 # include <hpp/util/pointer.hh>
25 # include <hpp/core/config.hh>
26 
27 namespace hpp {
28  namespace core {
30  namespace internal {
31  template <typename T> struct is_pointer : std::is_pointer<T> {};
32  template <typename T> struct is_pointer<shared_ptr<T> > : std::true_type {};
33  template <typename T> struct remove_pointer : std::remove_pointer<T> {};
34  template <typename T> struct remove_pointer<shared_ptr<T> > { typedef T type; };
35  template <typename T> struct remove_pointer<const shared_ptr<T> > { typedef T type; };
36 
37  template <bool deref_ptr> struct deref {
38  template <typename T> static inline T get (T t) { return t; }
39  };
40  template <> struct deref <true> {
41  template <typename T> static inline typename remove_pointer<T>::type get (T t) { return *t; }
42  };
43  }
45 
46  template <typename Types, typename Key = std::string > struct Container
47  {
48  typedef std::map <Key, Types> Map_t;
49  typedef typename Map_t::value_type value_type;
50  typedef typename Map_t::key_type key_type;
51  typedef typename Map_t::mapped_type mapped_type;
52  typedef typename Map_t::const_iterator const_iterator;
53  typedef typename Map_t:: iterator iterator;
54 
56 
58  void erase (const Key& name) { map.erase (name); }
60  void clear () { map.clear (); }
62  void add (const key_type& name, const mapped_type& element)
63  {
64  std::pair<iterator, bool> ret = map.insert( value_type(name, element));
65  if (!ret.second)
66  ret.first->second = element;
67  }
69  bool has (const key_type& name) const { return (map.find (name) != map.end ()); }
70 
72  const mapped_type& get (const key_type& name) const
73  {
74  const_iterator _e = map.find (name);
75  if (_e == map.end ()) {
76  std::stringstream ss; ss << "Invalid key: " << name;
77  throw std::invalid_argument (ss.str());
78  }
79  return _e->second;
80  }
81 
83  const mapped_type& get (const key_type& name, const mapped_type& defaultValue) const
84  {
85  const_iterator _e = map.find (name);
86  if (_e == map.end ()) return defaultValue;
87  return _e->second;
88  }
89 
92  template <typename ReturnType>
93  ReturnType getAllAs () const
94  {
95  ReturnType l;
96  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
97  l.push_back (_e->second);
98  return l;
99  }
100 
101  template <typename ReturnType>
102  ReturnType getKeys () const
103  {
104  ReturnType l;
105  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
106  l.push_back (_e->first);
107  return l;
108  }
109 
111  std::ostream& print (std::ostream& os) const
112  {
113  typedef internal::is_pointer<mapped_type> should_deref;
114  typedef internal::deref<should_deref::value> deref;
115  for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
116  os << _e->first << ": "
117  << deref::template get<const mapped_type> (_e->second)
118  << std::endl;
119  return os;
120  }
121  };
122  } // namespace core
123 } // namespace hpp
124 #endif // HPP_CORE_CONTAINER_HH
ReturnType getAllAs() const
Definition: container.hh:93
ReturnType getKeys() const
Definition: container.hh:102
Definition: bi-rrt-planner.hh:24
std::ostream & print(std::ostream &os) const
Print object in a stream.
Definition: container.hh:111
Map_t::key_type key_type
Definition: container.hh:50
bool has(const key_type &name) const
Return the element named name.
Definition: container.hh:69
Definition: container.hh:46
Map_t::iterator iterator
Definition: container.hh:53
std::map< Key, Types > Map_t
Definition: container.hh:48
Map_t::mapped_type mapped_type
Definition: container.hh:51
void clear()
Clear content of container.
Definition: container.hh:60
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:62
Map_t::const_iterator const_iterator
Definition: container.hh:52
Map_t map
Definition: container.hh:55
void erase(const Key &name)
Erase the element named name.
Definition: container.hh:58
Map_t::value_type value_type
Definition: container.hh:49