lsst.afw  17.0.1-26-ga1fda76f0
SimpleGenericMap.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of afw.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef LSST_AFW_TYPEHANDLING_SIMPLEGENERICMAP_H
26 #define LSST_AFW_TYPEHANDLING_SIMPLEGENERICMAP_H
27 
28 #include <exception>
29 #include <sstream>
30 #include <unordered_map>
31 #include <utility>
32 
33 #include "boost/variant.hpp"
34 
36 
37 namespace lsst {
38 namespace afw {
39 namespace typehandling {
40 
51 template <typename K>
52 class SimpleGenericMap final : public MutableGenericMap<K> {
53 protected:
54  using typename GenericMap<K>::StorableType;
55  using typename GenericMap<K>::ValueReference;
56 
57 public:
58  SimpleGenericMap() = default;
59  SimpleGenericMap(SimpleGenericMap const& other) = default;
61  virtual ~SimpleGenericMap() noexcept = default;
62 
63  SimpleGenericMap& operator=(SimpleGenericMap const& other) = default;
65 
66  typename GenericMap<K>::size_type size() const noexcept override { return _storage.size(); }
67 
68  bool empty() const noexcept override { return _storage.empty(); }
69 
70  typename GenericMap<K>::size_type max_size() const noexcept override { return _storage.max_size(); }
71 
72  bool contains(K const& key) const override { return _storage.count(key) > 0; }
73 
74  std::vector<K> keys() const override {
75  std::vector<K> keySnapshot;
76  keySnapshot.reserve(_storage.size());
77  for (auto const& pair : _storage) {
78  keySnapshot.push_back(pair.first);
79  }
80  return keySnapshot;
81  }
82 
83  void clear() noexcept override { _storage.clear(); }
84 
85 protected:
86  ValueReference unsafeLookup(K key) const override {
87  try {
88  return _storage.at(key);
89  } catch (std::out_of_range& e) {
90  std::stringstream message;
91  message << "Key not found: " << key;
93  }
94  }
95 
96  bool unsafeInsert(K key, StorableType&& value) override {
97  return _storage.insert(std::make_pair(key, std::move(value))).second;
98  }
99 
100  bool unsafeErase(K key) override { return _storage.erase(key); }
101 
102 private:
103  // StorableType is a value, so we might as well use it in the implementation
105 };
106 
107 } // namespace typehandling
108 } // namespace afw
109 } // namespace lsst
110 
111 #endif
T empty(T... args)
virtual ~SimpleGenericMap() noexcept=default
bool unsafeInsert(K key, StorableType &&value) override
Create a new mapping with key equal to key and value equal to value.
bool unsafeErase(K key) override
Remove the mapping for a key from this map, if it exists.
ItemVariant const * other
Definition: Schema.cc:56
bool empty() const noexcept override
Return true if this map contains no key-value pairs.
T at(T... args)
T push_back(T... args)
ValueReference unsafeLookup(K key) const override
Return a reference to the mapped value of the element with key equal to key.
boost::variant< bool const &, std::int32_t const &, std::int64_t const &, float const &, double const &, std::string const &, PolymorphicValue const &, std::shared_ptr< Storable > const & > ValueReference
A type-agnostic reference to the value stored inside the map.
Definition: GenericMap.h:514
A base class for image defects.
decltype(_referenceToType(std::declval< ValueReference >())) StorableType
The types that can be stored in a map.
Definition: GenericMap.h:522
A GenericMap that allows insertion and deletion of arbitrary values.
T erase(T... args)
T throw_with_nested(T... args)
Key< U > key
Definition: Schema.cc:281
T str(T... args)
T make_pair(T... args)
std::vector< K > keys() const override
Return the set of all keys, without type information.
T clear(T... args)
T move(T... args)
T count(T... args)
T insert(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
STL class.
GenericMap< K >::size_type size() const noexcept override
Return the number of key-value pairs in the map.
bool contains(K const &key) const override
Return true if this map contains a mapping whose key has the specified label.
T max_size(T... args)
SimpleGenericMap & operator=(SimpleGenericMap const &other)=default
void clear() noexcept override
Remove all of the mappings from this map.
GenericMap< K >::size_type max_size() const noexcept override
Return the maximum number of elements the container is able to hold due to system or library implemen...
Interface for a GenericMap that allows element addition and removal.
Definition: GenericMap.h:562
T reserve(T... args)