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>
258 template <typename T, typename
std::enable_if_t<!IS_SMART_PTR<T>,
int> = 0>
261 return const_cast<T&
>(
static_cast<const GenericMap&
>(*this).
at(key));
264 template <
typename T,
typename std::enable_if_t<!IS_SMART_PTR<T>,
int> = 0>
270 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
278 virtual size_type size()
const noexcept = 0;
282 virtual bool empty()
const noexcept = 0;
291 virtual size_type max_size()
const noexcept = 0;
306 template <
typename T>
308 return contains(key) ? 1 : 0;
323 virtual bool contains(K
const&
key)
const = 0;
343 template <
typename T>
346 return _contains(key);
380 auto keys1 = this->
keys();
381 auto keys2 =
other.keys();
385 for (K
const& key : keys1) {
386 if (this->unsafeLookup(key) !=
other.unsafeLookup(key)) {
456 template <
class Visitor>
457 auto apply(Visitor&& visitor)
const {
459 return _apply(visitor);
478 template <
class Visitor>
481 return _apply(visitor);
492 template <
typename... Types>
493 static boost::variant<std::add_lvalue_reference_t<Types>...> _typeToRef(
494 boost::variant<Types...>
const&) noexcept;
495 template <
typename... Types>
496 static boost::variant<std::add_lvalue_reference_t<std::add_const_t<Types>>...> _typeToConstRef(
497 boost::variant<Types...>
const&) noexcept;
543 using NonConstRef = std::add_lvalue_reference_t<
544 std::remove_const_t<std::remove_reference_t<decltype(value)>>>;
546 return const_cast<NonConstRef
>(value);
548 return boost::apply_visitor(removeConst, constRef);
555 template <
typename T,
556 typename std::enable_if_t<
558 T
const& _at(
Key<K, T> const& key)
const {
560 "Due to implementation constraints, const keys are not supported.");
562 auto foo = unsafeLookup(key.
getId());
563 return boost::get<T const&>(foo);
564 }
catch (boost::bad_get
const&) {
566 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
572 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
573 T
const& _at(
Key<K, T> const& key)
const {
575 "Due to implementation constraints, const keys are not supported.");
577 auto foo = unsafeLookup(key.
getId());
579 Storable const& value = boost::get<PolymorphicValue const&>(foo);
580 T
const* typedPointer =
dynamic_cast<T const*
>(&value);
581 if (typedPointer !=
nullptr) {
582 return *typedPointer;
585 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
588 }
catch (boost::bad_get
const&) {
590 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
596 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
599 "Due to implementation constraints, pointers to non-const are not supported.");
601 auto foo = unsafeLookup(key.
getId());
602 auto pointer = boost::get<std::shared_ptr<Storable const>
const&>(foo);
603 if (pointer ==
nullptr) {
610 if (typedPointer.use_count() > 0) {
614 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
617 }
catch (boost::bad_get
const&) {
619 message <<
"Key " << key <<
" not found, but a key labeled " << key.
getId() <<
" is present.";
625 template <
typename T,
626 typename std::enable_if_t<
628 bool _contains(
Key<K, T> const& key)
const {
630 if (!contains(key.
getId())) {
634 auto foo = unsafeLookup(key.
getId());
637 boost::get<T const&>(foo);
639 }
catch (boost::bad_get
const&) {
645 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
646 bool _contains(
Key<K, T> const& key)
const {
648 if (!contains(key.
getId())) {
652 auto foo = unsafeLookup(key.
getId());
655 Storable const& value = boost::get<PolymorphicValue const&>(foo);
656 auto asT =
dynamic_cast<T const*
>(&value);
657 return asT !=
nullptr;
658 }
catch (boost::bad_get
const&) {
664 template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value,
int> = 0>
667 "Due to implementation constraints, pointers to non-const are not supported.");
669 if (!contains(key.
getId())) {
673 auto foo = unsafeLookup(key.
getId());
675 auto pointer = boost::get<std::shared_ptr<Storable const>
const&>(foo);
676 if (pointer ==
nullptr) {
682 return typedPointer.use_count() > 0;
683 }
catch (boost::bad_get
const&) {
691 template <
class Visitor>
692 using _VisitorResult = std::result_of_t<Visitor && (K&&, bool&)>;
696 template <
class Visitor,
typename std::enable_if_t<std::is_
void<_VisitorResult<Visitor>>::value,
int> = 0>
697 void _apply(Visitor&& visitor)
const {
698 for (K
const& key :
keys()) {
699 boost::variant<K> varKey =
key;
700 boost::apply_visitor(visitor, varKey, unsafeLookup(key));
705 template <
class Visitor,
706 typename std::enable_if_t<!std::is_void<_VisitorResult<Visitor>>::value,
int> = 0>
707 auto _apply(Visitor&& visitor)
const {
711 for (K
const& key :
keys()) {
712 boost::variant<K> varKey =
key;
713 results.
emplace_back(boost::apply_visitor(visitor, varKey, unsafeLookup(key)));
719 template <
class Visitor,
typename std::enable_if_t<std::is_
void<_VisitorResult<Visitor>>::value,
int> = 0>
720 void _apply(Visitor&& visitor) {
721 for (K
const& key :
keys()) {
722 boost::variant<K> varKey =
key;
725 boost::apply_visitor(visitor, varKey, ref);
730 template <
class Visitor,
731 typename std::enable_if_t<!std::is_void<_VisitorResult<Visitor>>::value,
int> = 0>
732 auto _apply(Visitor&& visitor) {
736 for (K
const& key :
keys()) {
737 boost::variant<K> varKey =
key;
740 results.
emplace_back(boost::apply_visitor(visitor, varKey, ref));
756 template <
typename K>
769 virtual void clear() noexcept = 0;
789 template <
typename T>
791 if (this->contains(key.
getId())) {
813 template <
typename T>
815 auto strongKey = makeKey<T>(
key);
817 auto result = make_pair(strongKey,
false);
818 result.second = insert(strongKey, value);
835 template <
typename T>
837 if (this->contains(key)) {
838 return unsafeErase(key.
getId());
857 virtual bool unsafeInsert(K key,
StorableType&& value) = 0;
868 virtual bool unsafeErase(K key) = 0;
876 template <
typename K,
typename V>
877 struct hash<typename
lsst::afw::typehandling::Key<K, V>> {
T is_permutation(T... args)
auto apply(Visitor &&visitor)
Apply a modifying operation to each key-value pair in the map.
boost::variant< bool, int, long, long long, float, double, std::string, PolymorphicValue, std::shared_ptr< Storable const > > StorableType
The types that can be stored in a map.
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.
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
decltype(_typeToConstRef(std::declval< StorableType >())) ConstValueReference
A type-agnostic reference to the value stored inside the map.
constexpr bool operator==(Key< K, V > const &other) const noexcept
Test for key equality.
bool contains(Key< K, T > const &key) const
Return true if this map contains a mapping for the specified key.
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.
virtual ConstValueReference unsafeLookup(K key) const =0
Return a reference to the mapped value of the element with key equal to key.
A base class for image defects.
auto apply(Visitor &&visitor) const
Apply an operation to each key-value pair in the map.
constexpr Key(K id)
Construct a new key.
ValueReference unsafeLookup(K key)
Return a reference to the mapped value of the element with key equal to 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,...)
virtual bool operator==(GenericMap const &other) const noexcept
Test for map equality.
decltype(_typeToRef(std::declval< StorableType >())) ValueReference
A type-agnostic reference to the value stored inside the map.
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...
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
T emplace_back(T... args)