17 #ifndef HPP_CORE_CONTAINER_HH 18 # define HPP_CORE_CONTAINER_HH 24 # include <boost/smart_ptr/shared_ptr.hpp> 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> 34 # include <hpp/core/config.hh> 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; };
46 template <
bool deref_ptr>
struct deref {
47 template <
typename T>
static inline T
get (T
t) {
return t; }
49 template <>
struct deref <true> {
50 template <
typename T>
static inline typename remove_pointer<T>::type
get (T
t) {
return *
t; }
55 template <
typename Types,
typename Key = std::
string >
struct Container 57 typedef std::map <Key, Types>
Map_t;
67 void erase (
const Key& name) { map.erase (name); }
71 void add (
const key_type& name,
const mapped_type& element)
73 std::pair<iterator, bool> ret = map.insert(
value_type(name, element));
75 ret.first->second = element;
78 bool has (
const key_type& name)
const {
return (map.find (name) != map.end ()); }
81 const mapped_type&
get (
const key_type& name)
const 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());
92 const mapped_type&
get (
const key_type& name,
const mapped_type& defaultValue)
const 94 const_iterator _e = map.find (name);
95 if (_e == map.end ())
return defaultValue;
101 template <
typename ReturnType>
105 for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
106 l.push_back (_e->second);
110 template <
typename ReturnType>
114 for (const_iterator _e = map.begin (); _e != map.end (); ++_e)
115 l.push_back (_e->first);
120 std::ostream&
print (std::ostream& os)
const 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)
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
void erase(const Key &name)
Erase the element named name.
Definition: container.hh:67
Map_t::value_type value_type
Definition: container.hh:58