LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+bd2ed33bd6,g1470d8bcf6+c6d6eb38e2,g14a832a312+9d12ad093c,g2079a07aa2+86d27d4dc4,g2305ad1205+91a32aca49,g295015adf3+88246b6574,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+c34e8be1fa,g487adcacf7+85dcfbcc36,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ea1711114f,g5a732f18d5+53520f316c,g64a986408d+bd2ed33bd6,g858d7b2824+bd2ed33bd6,g8a8a8dda67+585e252eca,g99cad8db69+016a06b37a,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+b6d7b42999,gc120e1dc64+f745648b3a,gc28159a63d+0e5473021a,gcf0d15dbbd+c34e8be1fa,gdaeeff99f8+f9a426f77a,ge6526c86ff+508d0e0a30,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+8d59551888,gf1cff7945b+bd2ed33bd6,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Types | List of all members
lsst::afw::typehandling::GenericMap< K > Class Template Referenceabstract

Interface for a heterogeneous map. More...

#include <GenericMap.h>

Inheritance diagram for lsst::afw::typehandling::GenericMap< K >:
lsst::afw::typehandling::MutableGenericMap< K > lsst::afw::typehandling::SimpleGenericMap< K >

Public Types

using key_type = K
 
using size_type = std::size_t
 
using difference_type = std::ptrdiff_t
 

Public Member Functions

virtual ~GenericMap () noexcept=default
 
virtual size_type size () const noexcept=0
 Return the number of key-value pairs in the map.
 
virtual bool empty () const noexcept=0
 Return true if this map contains no key-value pairs.
 
virtual size_type max_size () const noexcept=0
 Return the maximum number of elements the container is able to hold due to system or library implementation limitations.
 
template<typename T >
size_type count (Key< K, T > const &key) const
 Return the number of elements mapped to the specified key.
 
virtual bool contains (K const &key) const =0
 Return true if this map contains a mapping whose key has the specified label.
 
template<typename T >
bool contains (Key< K, T > const &key) const
 Return true if this map contains a mapping for the specified key.
 
virtual std::vector< K > const & keys () const noexcept=0
 Return the set of all keys, without type information.
 
template<class Visitor >
auto apply (Visitor &&visitor) const
 Apply an operation to each key-value pair in the map.
 
template<class Visitor >
auto apply (Visitor &&visitor)
 Apply a modifying operation to each key-value pair in the map.
 
template<typename T , typename std::enable_if_t<!detail::IS_SMART_PTR< T >, int > = 0>
T & at (Key< K, T > const &key)
 Return a reference to the mapped value of the element with key equal to key.
 
template<typename T , typename std::enable_if_t<!detail::IS_SMART_PTR< T >, int > = 0>
T const & at (Key< K, T > const &key) const
 Return a reference to the mapped value of the element with key equal to key.
 
template<typename T , typename std::enable_if_t< std::is_base_of< Storable, T >::value, int > = 0>
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.
 
virtual bool operator== (GenericMap const &other) const noexcept
 Test for map equality.
 
bool operator!= (GenericMap const &other) const
 Test for map equality.
 

Protected Types

using StorableType
 The types that can be stored in a map.
 
using ConstValueReference = decltype(_typeToConstRef(std::declval<StorableType>()))
 A type-agnostic reference to the value stored inside the map.
 
using ValueReference = decltype(_typeToRef(std::declval<StorableType>()))
 A type-agnostic reference to the value stored inside the map.
 

Protected Member Functions

virtual ConstValueReference unsafeLookup (K key) const =0
 Return a reference to the mapped value of the element with key equal to key.
 
ValueReference unsafeLookup (K key)
 Return a reference to the mapped value of the element with key equal to key.
 

Detailed Description

template<typename K>
class lsst::afw::typehandling::GenericMap< K >

Interface for a heterogeneous map.

Objects of type GenericMap cannot necessarily have keys added or removed, although mutable values can be modified as usual. In Python, a GenericMap behaves like a collections.abc.Mapping. See MutableGenericMap for a GenericMap that must allow insertions and deletions.

Template Parameters
Kthe key type of the map.

A Key for the map is parameterized by both the key type K and a corresponding value type V. The map is indexed uniquely by a value of type K; no two entries in the map may have identical values of Key::getId().

All operations are sensitive to the value type of the key: a contains call requesting an integer labeled "value", for example, will report no such integer if instead there is a string labeled "value". For Python compatibility, a GenericMap does not store type information internally, instead relying on RTTI for type checking.

All subclasses must guarantee, as a class invariant, that every value in the map is implicitly nothrow-convertible to the type indicated by its key. For example, MutableGenericMap ensures this by appropriately templating all operations that create new key-value pairs.

A GenericMap may contain primitive types, strings, Storable, and shared pointers to Storable as values. It does not support unique pointers to Storable because such pointers are read destructively. For safety reasons, it may not contain references, C-style pointers, or arrays to any type. Due to implementation restrictions, const types (except pointers to const Storable) are not currently supported.

Definition at line 79 of file GenericMap.h.

Member Typedef Documentation

◆ ConstValueReference

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::ConstValueReference = decltype(_typeToConstRef(std::declval<StorableType>()))
protected

A type-agnostic reference to the value stored inside the map.

These are the reference equivalents (T const& or T&) of StorableType.

Definition at line 369 of file GenericMap.h.

◆ difference_type

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::difference_type = std::ptrdiff_t

Definition at line 83 of file GenericMap.h.

◆ key_type

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::key_type = K

Definition at line 81 of file GenericMap.h.

◆ size_type

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::size_type = std::size_t

Definition at line 82 of file GenericMap.h.

◆ StorableType

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::StorableType
protected
Initial value:
std::variant<bool, int, long, long long, float, double, std::string,

The types that can be stored in a map.

Keys of any subclass of Storable are implemented using PolymorphicValue to preserve type.

Definition at line 359 of file GenericMap.h.

◆ ValueReference

template<typename K >
using lsst::afw::typehandling::GenericMap< K >::ValueReference = decltype(_typeToRef(std::declval<StorableType>()))
protected

A type-agnostic reference to the value stored inside the map.

These are the reference equivalents (T const& or T&) of StorableType.

Definition at line 370 of file GenericMap.h.

Constructor & Destructor Documentation

◆ ~GenericMap()

template<typename K >
virtual lsst::afw::typehandling::GenericMap< K >::~GenericMap ( )
virtualdefaultnoexcept

Member Function Documentation

◆ apply() [1/2]

template<typename K >
template<class Visitor >
auto lsst::afw::typehandling::GenericMap< K >::apply ( Visitor && visitor)
inline

Apply a modifying operation to each key-value pair in the map.

Template Parameters
Visitora callable that takes a key and a value. Requirements as for apply(Visitor&&) const, except that it may take non-const references to values.
Parameters
visitorthe visitor to apply
Returns
if Visitor has a return value, a std::vector of values returned from applying visitor to each key in keys, in that order. Otherwise, void.
Exception Safety
Provides basic exception safety if Visitor is exception-safe.
Note
This implementation calls keys, then calls unsafeLookup for each key before passing the result to visitor.

Definition at line 331 of file GenericMap.h.

331 {
332 // Delegate to private methods to hide special-casing of Visitor
333 return _apply(visitor);
334 }

◆ apply() [2/2]

template<typename K >
template<class Visitor >
auto lsst::afw::typehandling::GenericMap< K >::apply ( Visitor && visitor) const
inline

Apply an operation to each key-value pair in the map.

Template Parameters
Visitora callable that takes a key and a value. See below for exact requirements.
Parameters
visitorthe visitor to apply
Returns
if Visitor has a return value, a std::vector of values returned from applying visitor to each key in keys, in that order. Otherwise, void.
Exception Safety
Provides the same level of exception safety as Visitor, or strong exception safety if Visitor cannot throw.

A Visitor must define one or more operator() that take a weakly-typed key and a value. Each operator() must return the same type (which may be void). Through any combination of overloading or templates, the visitor must accept values of the following types:

  • either bool or bool const&
  • either int or int const&
  • either long or long const&
  • either long long or long long const&
  • either float or float const&
  • either double or double const&
  • std::string const&
  • Storable const&
  • std::shared_ptr<Storable const>
Note
This implementation calls keys, then calls unsafeLookup for each key before passing the result to visitor.

An example visitor that prints each key-value pair to standard output:

template <typename K>
class Printer {
public:
    template <typename V>
    void operator()(K const& key, V const& value) {
        std::cout << key << ": " << value << "," << std::endl;
    }

    void operator()(K const& key, Storable const& value) {
        std::cout << key << ": ";
        try {
            std::cout << value;
        } catch (UnsupportedOperationException const&) {
            std::cout << "[unprintable]";
        }
        std::cout << "," << std::endl;
    }

    void operator()(K const& key, std::shared_ptr<Storable const> value) {
        if (value != nullptr) {
            operator()(key, *value);
        } else {
            operator()(key, "null");
        }
    }
};

Definition at line 309 of file GenericMap.h.

309 {
310 // Delegate to private methods to hide special-casing of Visitor
311 return _apply(visitor);
312 }

◆ at() [1/3]

template<typename K >
template<typename T , typename std::enable_if_t< std::is_base_of< Storable, T >::value, int > = 0>
std::shared_ptr< T > lsst::afw::typehandling::GenericMap< K >::at ( Key< K, std::shared_ptr< T > > const & key) const
inline

Return a reference to the mapped value of the element with key equal to key.

Template Parameters
Tthe type of the element mapped to key. It may be the exact type of the element, if known, or any type to which its references or pointers can be implicitly converted (e.g., a superclass). For example, references to built-in types are not convertible, so you can't retrieve an int with T=long.
Parameters
keythe key of the element to find
Returns
a reference to the T mapped to key, if one exists
Exceptions
pex::exceptions::OutOfRangeErrorThrown if the map does not have a T with the specified key
Exception Safety
Provides strong exception safety.
Note
This implementation calls unsafeLookup once, then uses templates and RTTI to determine if the value is of the expected type.

Definition at line 121 of file GenericMap.h.

121 {
122 // Delegate to private methods to hide further special-casing of T
123 return _at(key);
124 }

◆ at() [2/3]

template<typename K >
template<typename T , typename std::enable_if_t<!detail::IS_SMART_PTR< T >, int > = 0>
T & lsst::afw::typehandling::GenericMap< K >::at ( Key< K, T > const & key)
inline

Return a reference to the mapped value of the element with key equal to key.

Template Parameters
Tthe type of the element mapped to key. It may be the exact type of the element, if known, or any type to which its references or pointers can be implicitly converted (e.g., a superclass). For example, references to built-in types are not convertible, so you can't retrieve an int with T=long.
Parameters
keythe key of the element to find
Returns
a reference to the T mapped to key, if one exists
Exceptions
pex::exceptions::OutOfRangeErrorThrown if the map does not have a T with the specified key
Exception Safety
Provides strong exception safety.
Note
This implementation calls unsafeLookup once, then uses templates and RTTI to determine if the value is of the expected type.

Definition at line 109 of file GenericMap.h.

109 {
110 // Both casts are safe; see Effective C++, Item 3
111 return const_cast<T&>(static_cast<const GenericMap&>(*this).at(key));
112 }

◆ at() [3/3]

template<typename K >
template<typename T , typename std::enable_if_t<!detail::IS_SMART_PTR< T >, int > = 0>
T const & lsst::afw::typehandling::GenericMap< K >::at ( Key< K, T > const & key) const
inline

Return a reference to the mapped value of the element with key equal to key.

Template Parameters
Tthe type of the element mapped to key. It may be the exact type of the element, if known, or any type to which its references or pointers can be implicitly converted (e.g., a superclass). For example, references to built-in types are not convertible, so you can't retrieve an int with T=long.
Parameters
keythe key of the element to find
Returns
a reference to the T mapped to key, if one exists
Exceptions
pex::exceptions::OutOfRangeErrorThrown if the map does not have a T with the specified key
Exception Safety
Provides strong exception safety.
Note
This implementation calls unsafeLookup once, then uses templates and RTTI to determine if the value is of the expected type.

Definition at line 115 of file GenericMap.h.

115 {
116 // Delegate to private methods to hide further special-casing of T
117 return _at(key);
118 }

◆ contains() [1/2]

template<typename K >
virtual bool lsst::afw::typehandling::GenericMap< K >::contains ( K const & key) const
pure virtual

Return true if this map contains a mapping whose key has the specified label.

More formally, this method returns true if and only if this map contains a mapping with a key k such that k.getId() == key. There can be at most one such mapping.

Parameters
keythe weakly-typed key to search for
Returns
true if this map contains a mapping for key, regardless of value type.
Exception Safety
Provides strong exception safety.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.

◆ contains() [2/2]

template<typename K >
template<typename T >
bool lsst::afw::typehandling::GenericMap< K >::contains ( Key< K, T > const & key) const
inline

Return true if this map contains a mapping for the specified key.

This is equivalent to testing whether at(key) would succeed.

Template Parameters
Tthe value corresponding to key
Parameters
keythe key to search for
Returns
true if this map contains a mapping from the specified key to a T
Exception Safety
Provides strong exception safety.
Note
This implementation calls contains(K const&) const. If the call returns true, it calls unsafeLookup, then uses templates and RTTI to determine if the value is of the expected type. The performance of this method depends strongly on the performance of contains(K const&).

Definition at line 194 of file GenericMap.h.

194 {
195 // Delegate to private methods to hide special-casing of T
196 return _contains(key);
197 }

◆ count()

template<typename K >
template<typename T >
size_type lsst::afw::typehandling::GenericMap< K >::count ( Key< K, T > const & key) const
inline

Return the number of elements mapped to the specified key.

Template Parameters
Tthe value corresponding to key
Parameters
keykey value of the elements to count
Returns
number of T with key key, that is, either 1 or 0.
Exception Safety
Provides strong exception safety.
Note
This implementation calls contains.

Definition at line 157 of file GenericMap.h.

157 {
158 return contains(key) ? 1 : 0;
159 }
virtual bool contains(K const &key) const =0
Return true if this map contains a mapping whose key has the specified label.

◆ empty()

template<typename K >
virtual bool lsst::afw::typehandling::GenericMap< K >::empty ( ) const
pure virtualnoexcept

Return true if this map contains no key-value pairs.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.

◆ keys()

template<typename K >
virtual std::vector< K > const & lsst::afw::typehandling::GenericMap< K >::keys ( ) const
pure virtualnoexcept

Return the set of all keys, without type information.

Returns
a read-only view of all keys currently in the map, in the same iteration order as this object. The view will be updated by changes to the underlying map.
Warning
Do not modify this map while iterating over its keys.
Do not store the returned reference in variables that outlive the map; destroying the map will invalidate the reference.
Note
The keys are returned as a list, rather than a set, so that subclasses can give them a well-defined iteration order.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.

◆ max_size()

template<typename K >
virtual size_type lsst::afw::typehandling::GenericMap< K >::max_size ( ) const
pure virtualnoexcept

Return the maximum number of elements the container is able to hold due to system or library implementation limitations.

Note
This value typically reflects the theoretical limit on the size of the container. At runtime, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.

◆ operator!=()

template<typename K >
bool lsst::afw::typehandling::GenericMap< K >::operator!= ( GenericMap< K > const & other) const
inline

Test for map equality.

Two GenericMap objects are considered equal if they map the same keys to the same values. The two objects do not need to have the same implementation class. If either class orders its keys, the order is ignored.

Note
This implementation calls keys on both objects and compares the results. If the two objects have the same keys, it calls unsafeLookup for each key and compares the values.

Definition at line 245 of file GenericMap.h.

245{ return !(*this == other); }

◆ operator==()

template<typename K >
virtual bool lsst::afw::typehandling::GenericMap< K >::operator== ( GenericMap< K > const & other) const
inlinevirtualnoexcept

Test for map equality.

Two GenericMap objects are considered equal if they map the same keys to the same values. The two objects do not need to have the same implementation class. If either class orders its keys, the order is ignored.

Note
This implementation calls keys on both objects and compares the results. If the two objects have the same keys, it calls unsafeLookup for each key and compares the values.

Definition at line 229 of file GenericMap.h.

229 {
230 auto keys1 = this->keys();
231 auto keys2 = other.keys();
232 if (!std::is_permutation(keys1.begin(), keys1.end(), keys2.begin(), keys2.end())) {
233 return false;
234 }
235 for (K const& key : keys1) {
236 // Can't use std::variant::operator== here because
237 // std::reference_wrapper isn't equality-comparable.
238 if (!detail::refwrap_equals()(this->unsafeLookup(key), other.unsafeLookup(key))) {
239 return false;
240 }
241 }
242 return true;
243 }
virtual ConstValueReference unsafeLookup(K key) const =0
Return a reference to the mapped value of the element with key equal to key.
virtual std::vector< K > const & keys() const noexcept=0
Return the set of all keys, without type information.
T is_permutation(T... args)

◆ size()

template<typename K >
virtual size_type lsst::afw::typehandling::GenericMap< K >::size ( ) const
pure virtualnoexcept

Return the number of key-value pairs in the map.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.

◆ unsafeLookup() [1/2]

template<typename K >
ValueReference lsst::afw::typehandling::GenericMap< K >::unsafeLookup ( K key)
inlineprotected

Return a reference to the mapped value of the element with key equal to key.

This method is the primary way to implement the GenericMap interface.

Parameters
keythe key of the element to find
Returns
the value mapped to key, if one exists
Exceptions
pex::exceptions::OutOfRangeErrorThrown if the map does not have a value with the specified key
Exception Safety
Must provide strong exception safety.

Definition at line 391 of file GenericMap.h.

391 {
392 ConstValueReference constRef = static_cast<const GenericMap&>(*this).unsafeLookup(key);
393 return std::visit(
394 [](auto const & v) { return ValueReference(detail::refwrap_const_cast(v)); },
395 constRef
396 );
397 }
decltype(_typeToRef(std::declval< StorableType >())) ValueReference
A type-agnostic reference to the value stored inside the map.
Definition GenericMap.h:370
decltype(_typeToConstRef(std::declval< StorableType >())) ConstValueReference
A type-agnostic reference to the value stored inside the map.
Definition GenericMap.h:369
std::reference_wrapper< T > refwrap_const_cast(std::reference_wrapper< T const > const &r)

◆ unsafeLookup() [2/2]

template<typename K >
virtual ConstValueReference lsst::afw::typehandling::GenericMap< K >::unsafeLookup ( K key) const
protectedpure virtual

Return a reference to the mapped value of the element with key equal to key.

This method is the primary way to implement the GenericMap interface.

Parameters
keythe key of the element to find
Returns
the value mapped to key, if one exists
Exceptions
pex::exceptions::OutOfRangeErrorThrown if the map does not have a value with the specified key
Exception Safety
Must provide strong exception safety.

Implemented in lsst::afw::typehandling::SimpleGenericMap< K >.


The documentation for this class was generated from the following file: