25 #ifndef LSST_AFW_TYPEHANDLING_GENERICMAP_H 26 #define LSST_AFW_TYPEHANDLING_GENERICMAP_H 34 #include <type_traits> 35 #include <unordered_set> 38 #include "boost/core/demangle.hpp" 39 #include "boost/variant.hpp" 47 namespace typehandling {
61 template <
typename K,
typename V>
78 constexpr
Key(K
id) : id(id) {}
93 constexpr K
const&
getId() const noexcept {
return id; }
105 template <
typename U>
106 constexpr std::enable_if_t<!std::is_same<U, V>::value,
bool>
operator==(
Key<K, U> const&)
const noexcept {
110 template <
typename U>
112 return !(*
this ==
other);
130 template <
typename U>
131 constexpr
bool operator<(Key<K, U>
const&
other)
const noexcept {
133 return comparator(this->
getId(),
other.getId());
160 template <
typename V,
typename K>
183 template <
typename K,
typename V>
185 static const std::string typeStr = boost::core::demangle(
typeid(V).
name());
188 os <<
key.getId() <<
"<" << typeStr << constStr << volatileStr <<
">";
194 template <
typename,
typename =
void>
196 template <
typename T>
197 constexpr
bool IS_SMART_PTR<T, std::enable_if_t<std::is_object<typename T::element_type>::value>> =
true;
228 template <
typename K>
254 template <typename T, typename
std::enable_if_t<!IS_SMART_PTR<T>,
int> = 0>
257 return const_cast<T&
>(
static_cast<const GenericMap&
>(*this).
at(key));
261 template <
typename T,
262 typename std::enable_if_t<
266 "Due to implementation constraints, const keys are not supported.");
268 auto foo = unsafeLookup(key.
getId());
269 return boost::get<T const&>(foo);
270 }
catch (boost::bad_get
const&) {
272 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
277 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
280 "Due to implementation constraints, const keys are not supported.");
282 auto foo = unsafeLookup(key.
getId());
284 Storable const& value = boost::get<PolymorphicValue const&>(foo);
285 T
const* typedPointer =
dynamic_cast<T const*
>(&value);
286 if (typedPointer !=
nullptr) {
287 return *typedPointer;
290 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
293 }
catch (boost::bad_get
const&) {
295 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
300 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
303 "Due to implementation constraints, const keys are not supported.");
305 auto foo = unsafeLookup(
key.getId());
306 auto pointer = boost::get<std::shared_ptr<Storable>
const&>(foo);
310 if (typedPointer.use_count() > 0) {
314 message <<
"Key " <<
key <<
" not found, but a key labeled " <<
key.getId() <<
" is present.";
317 }
catch (boost::bad_get
const&) {
319 message <<
"Key " <<
key <<
" not found, but a key labeled " <<
key.getId() <<
" is present.";
326 virtual size_type size()
const noexcept = 0;
330 virtual bool empty()
const noexcept = 0;
339 virtual size_type max_size()
const noexcept = 0;
354 template <
typename T>
356 return contains(key) ? 1 : 0;
371 virtual bool contains(K
const&
key)
const = 0;
394 template <
typename T,
395 typename std::enable_if_t<
399 if (!contains(key.
getId())) {
403 auto foo = unsafeLookup(key.
getId());
406 boost::get<T const&>(foo);
408 }
catch (boost::bad_get
const&) {
413 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
416 if (!contains(key.
getId())) {
420 auto foo = unsafeLookup(key.
getId());
423 Storable const& value = boost::get<PolymorphicValue const&>(foo);
424 auto asT =
dynamic_cast<T const*
>(&value);
425 return asT !=
nullptr;
426 }
catch (boost::bad_get
const&) {
431 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
434 if (!contains(key.getId())) {
438 auto foo = unsafeLookup(key.getId());
440 auto pointer = boost::get<std::shared_ptr<Storable>
const&>(foo);
444 return typedPointer.use_count() > 0;
445 }
catch (boost::bad_get
const&) {
480 auto keys1 = this->keySet();
481 auto keys2 = other.keySet();
482 if (keys1 != keys2) {
485 for (K
const& key : keys1) {
486 if (this->unsafeLookup(key) != other.
unsafeLookup(key)) {
501 template <
typename... Types>
502 static boost::variant<std::decay_t<Types>...> _referenceToType(boost::variant<Types...>
const&) noexcept;
522 using StorableType = decltype(_referenceToType(std::declval<ValueReference>()));
546 auto rawKeys =
keys();
561 template <
typename K>
574 virtual void clear() noexcept = 0;
594 template <
typename T>
596 if (this->contains(key.
getId())) {
618 template <
typename T>
620 auto strongKey = makeKey<T>(
key);
622 auto result = make_pair(strongKey,
false);
623 result.second = insert(strongKey, value);
640 template <
typename T>
642 if (this->contains(key)) {
643 return unsafeErase(key.
getId());
662 virtual bool unsafeInsert(K key,
StorableType&& value) = 0;
673 virtual bool unsafeErase(K key) = 0;
681 template <
typename K,
typename V>
682 struct hash<typename
lsst::afw::typehandling::Key<K, V>> {
size_type count(Key< K, T > const &key) const
Return the number of elements mapped to the specified key.
bool operator!=(GenericMap const &other) const
Test for map equality.
virtual bool operator==(GenericMap const &other) const
Test for map equality.
bool erase(Key< K, T > const &key)
Remove the mapping for a key from this map, if it exists.
Container that passes Storable objects by value while preserving type.
std::shared_ptr< T > at(Key< K, std::shared_ptr< T >> const &key) const
Return a reference to the mapped value of the element with key equal to key.
T & at(Key< K, T > const &key)
Return a reference to the mapped value of the element with key equal to key.
Interface supporting iteration over heterogenous containers.
constexpr Key< K, V > makeKey(K const &id)
Factory function for Key, to enable type parameter inference.
size_t operator()(argument_type const &obj) const noexcept
constexpr bool operator==(Key< K, V > const &other) const noexcept
Test for key equality.
ItemVariant const * other
T const & at(Key< K, T > const &key) const
Return a reference to the mapped value of the element with key equal to key.
Key for type-safe lookup in a GenericMap.
bool contains(Key< K, T > const &key) const
Return true if this map contains a mapping for the specified 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.
A base class for image defects.
decltype(_referenceToType(std::declval< ValueReference >())) StorableType
The types that can be stored in a map.
virtual ValueReference unsafeLookup(K key) const =0
Return a reference to the mapped value of the element with key equal to key.
constexpr Key(K id)
Construct a new key.
typename lsst::afw::typehandling::Key< K, V > argument_type
T dynamic_pointer_cast(T... args)
constexpr bool operator!=(Key< K, U > const &other) const noexcept
Test for key equality.
std::unique_ptr< SchemaItem< U > > result
std::size_t hash_value() const noexcept
Return a hash of this object.
#define LSST_EXCEPT(type,...)
std::pair< Key< K, T >, bool > insert(K const &key, T const &value)
Insert an element into the map, if the map doesn't already contain a mapping with a conflicting key...
bool contains(Key< K, std::shared_ptr< T >> const &key) const
Return true if this map contains a mapping for the specified key.
constexpr bool IS_SMART_PTR
bool insert(Key< K, T > const &key, T const &value)
Insert an element into the map, if the map doesn't already contain a mapping with the same or a confl...
Interface for a GenericMap that allows element addition and removal.
constexpr K const & getId() const noexcept
Return the identifier of this field.
Interface for a heterogeneous map.
Key & operator=(Key const &)=delete