| Directory: | ./ |
|---|---|
| File: | include/hpp/core/container.hh |
| Date: | 2025-03-10 11:18:21 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 18 | 21 | 85.7% |
| Branches: | 8 | 24 | 33.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 39 | namespace hpp { | ||
| 40 | namespace core { | ||
| 41 | /// @cond INTERNAL | ||
| 42 | namespace internal { | ||
| 43 | template <typename T> | ||
| 44 | struct is_pointer : std::is_pointer<T> {}; | ||
| 45 | template <typename T> | ||
| 46 | struct is_pointer<shared_ptr<T> > : std::true_type {}; | ||
| 47 | template <typename T> | ||
| 48 | struct remove_pointer : std::remove_pointer<T> {}; | ||
| 49 | template <typename T> | ||
| 50 | struct remove_pointer<shared_ptr<T> > { | ||
| 51 | typedef T type; | ||
| 52 | }; | ||
| 53 | template <typename T> | ||
| 54 | struct remove_pointer<const shared_ptr<T> > { | ||
| 55 | typedef T type; | ||
| 56 | }; | ||
| 57 | |||
| 58 | template <bool deref_ptr> | ||
| 59 | struct deref { | ||
| 60 | template <typename T> | ||
| 61 | 1 | static inline T get(T t) { | |
| 62 | 1 | return t; | |
| 63 | } | ||
| 64 | }; | ||
| 65 | template <> | ||
| 66 | struct deref<true> { | ||
| 67 | template <typename T> | ||
| 68 | 2 | static inline typename remove_pointer<T>::type get(T t) { | |
| 69 | 2 | return *t; | |
| 70 | } | ||
| 71 | }; | ||
| 72 | } // namespace internal | ||
| 73 | /// @endcond | ||
| 74 | |||
| 75 | template <typename Types, typename Key = std::string> | ||
| 76 | struct 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 | |||
| 84 | Map_t map; | ||
| 85 | |||
| 86 | /// Erase the element named name | ||
| 87 | void erase(const Key& name) { map.erase(name); } | ||
| 88 | /// Clear content of container | ||
| 89 | void clear() { map.clear(); } | ||
| 90 | /// Add an element | ||
| 91 | 930 | void add(const key_type& name, const mapped_type& element) { | |
| 92 |
2/4✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 486 times.
✗ Branch 5 not taken.
|
930 | std::pair<iterator, bool> ret = map.insert(value_type(name, element)); |
| 93 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 479 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
930 | if (!ret.second) ret.first->second = element; |
| 94 | 930 | } | |
| 95 | /// Return the element named name | ||
| 96 |
1/2✓ Branch 2 taken 668 times.
✗ Branch 3 not taken.
|
690 | bool has(const key_type& name) const { return (map.find(name) != map.end()); } |
| 97 | |||
| 98 | /// Return the element named name | ||
| 99 | 445 | const mapped_type& get(const key_type& name) const { | |
| 100 |
1/2✓ Branch 1 taken 370 times.
✗ Branch 2 not taken.
|
445 | const_iterator _e = map.find(name); |
| 101 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
|
445 | if (_e == map.end()) { |
| 102 | ✗ | std::stringstream ss; | |
| 103 | ✗ | ss << "Invalid key: " << name; | |
| 104 | ✗ | throw std::invalid_argument(ss.str()); | |
| 105 | } | ||
| 106 | 445 | return _e->second; | |
| 107 | } | ||
| 108 | |||
| 109 | /// Return the element named name | ||
| 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 | |||
| 117 | /// Return a list of all elements | ||
| 118 | /// \tparam ReturnType must have a push_back method. | ||
| 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 | |||
| 135 | /// Print object in a stream | ||
| 136 | 3 | 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 | 6 | for (const_iterator _e = map.begin(); _e != map.end(); ++_e) | |
| 140 | 3 | os << _e->first << ": " | |
| 141 | 3 | << deref::template get<const mapped_type>(_e->second) << std::endl; | |
| 142 | 3 | return os; | |
| 143 | } | ||
| 144 | }; | ||
| 145 | } // namespace core | ||
| 146 | } // namespace hpp | ||
| 147 | #endif // HPP_CORE_CONTAINER_HH | ||
| 148 |