hpp-core 6.0.0
Implement basic classes for canonical path planning for kinematic chains.
Loading...
Searching...
No Matches
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
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// 1. Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright
13// notice, this list of conditions and the following disclaimer in the
14// documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27// DAMAGE.
28
29#ifndef HPP_CORE_CONTAINER_HH
30#define HPP_CORE_CONTAINER_HH
31
32#include <hpp/core/config.hh>
33#include <hpp/util/pointer.hh>
34#include <map>
35#include <sstream>
36#include <stdexcept>
37#include <type_traits>
38
39namespace hpp {
40namespace core {
42namespace internal {
43template <typename T>
44struct is_pointer : std::is_pointer<T> {};
45template <typename T>
46struct is_pointer<shared_ptr<T> > : std::true_type {};
47template <typename T>
48struct remove_pointer : std::remove_pointer<T> {};
49template <typename T>
50struct remove_pointer<shared_ptr<T> > {
51 typedef T type;
52};
53template <typename T>
54struct remove_pointer<const shared_ptr<T> > {
55 typedef T type;
56};
57
58template <bool deref_ptr>
59struct deref {
60 template <typename T>
61 static inline T get(T t) {
62 return t;
63 }
64};
65template <>
66struct deref<true> {
67 template <typename T>
68 static inline typename remove_pointer<T>::type get(T t) {
69 return *t;
70 }
71};
72} // namespace internal
74
75template <typename Types, typename Key = std::string>
76struct Container {
77 typedef std::map<Key, Types> Map_t;
78 typedef typename Map_t::value_type value_type;
79 typedef typename Map_t::key_type key_type;
80 typedef typename Map_t::mapped_type mapped_type;
81 typedef typename Map_t::const_iterator const_iterator;
82 typedef typename Map_t::iterator iterator;
83
85
87 void erase(const Key& name) { map.erase(name); }
89 void clear() { map.clear(); }
91 void add(const key_type& name, const mapped_type& element) {
92 std::pair<iterator, bool> ret = map.insert(value_type(name, element));
93 if (!ret.second) ret.first->second = element;
94 }
96 bool has(const key_type& name) const { return (map.find(name) != map.end()); }
97
99 const mapped_type& get(const key_type& name) const {
100 const_iterator _e = map.find(name);
101 if (_e == map.end()) {
102 std::stringstream ss;
103 ss << "Invalid key: " << name;
104 throw std::invalid_argument(ss.str());
105 }
106 return _e->second;
107 }
108
110 const mapped_type& get(const key_type& name,
111 const mapped_type& defaultValue) const {
112 const_iterator _e = map.find(name);
113 if (_e == map.end()) return defaultValue;
114 return _e->second;
115 }
116
119 template <typename ReturnType>
120 ReturnType getAllAs() const {
121 ReturnType l;
122 for (const_iterator _e = map.begin(); _e != map.end(); ++_e)
123 l.push_back(_e->second);
124 return l;
125 }
126
127 template <typename ReturnType>
128 ReturnType getKeys() const {
129 ReturnType l;
130 for (const_iterator _e = map.begin(); _e != map.end(); ++_e)
131 l.push_back(_e->first);
132 return l;
133 }
134
136 std::ostream& print(std::ostream& os) const {
137 typedef internal::is_pointer<mapped_type> should_deref;
138 typedef internal::deref<should_deref::value> deref;
139 for (const_iterator _e = map.begin(); _e != map.end(); ++_e)
140 os << _e->first << ": "
141 << deref::template get<const mapped_type>(_e->second) << std::endl;
142 return os;
143 }
144};
145} // namespace core
146} // namespace hpp
147#endif // HPP_CORE_CONTAINER_HH
Definition bi-rrt-planner.hh:35
Definition container.hh:76
bool has(const key_type &name) const
Return the element named name.
Definition container.hh:96
std::ostream & print(std::ostream &os) const
Print object in a stream.
Definition container.hh:136
const mapped_type & get(const key_type &name, const mapped_type &defaultValue) const
Return the element named name.
Definition container.hh:110
std::map< Key, Types > Map_t
Definition container.hh:77
Map_t::const_iterator const_iterator
Definition container.hh:81
const mapped_type & get(const key_type &name) const
Return the element named name.
Definition container.hh:99
void add(const key_type &name, const mapped_type &element)
Add an element.
Definition container.hh:91
Map_t::key_type key_type
Definition container.hh:79
Map_t::value_type value_type
Definition container.hh:78
void erase(const Key &name)
Erase the element named name.
Definition container.hh:87
Map_t::mapped_type mapped_type
Definition container.hh:80
ReturnType getAllAs() const
Definition container.hh:120
void clear()
Clear content of container.
Definition container.hh:89
ReturnType getKeys() const
Definition container.hh:128
Map_t map
Definition container.hh:84
Map_t::iterator iterator
Definition container.hh:82