LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Protected Types | Protected Member Functions | List of all members
lsst::afw::typehandling::SimpleGenericMap< K > Class Template Referencefinal

A GenericMap that allows insertion and deletion of arbitrary values. More...

#include <SimpleGenericMap.h>

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

Public Types

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

Public Member Functions

 SimpleGenericMap ()=default
 
 SimpleGenericMap (SimpleGenericMap const &other)=default
 
 SimpleGenericMap (SimpleGenericMap &&) noexcept=default
 
 SimpleGenericMap (GenericMap< K > const &other)
 Convert another GenericMap into a SimpleGenericMap. More...
 
virtual ~SimpleGenericMap () noexcept=default
 
SimpleGenericMapoperator= (SimpleGenericMap const &other)
 
SimpleGenericMapoperator= (SimpleGenericMap &&) noexcept=default
 
SimpleGenericMapoperator= (GenericMap< K > const &other)
 
GenericMap< K >::size_type size () const noexcept override
 Return the number of key-value pairs in the map. More...
 
bool empty () const noexcept override
 Return true if this map contains no key-value pairs. More...
 
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 implementation limitations. More...
 
bool contains (K const &key) const override
 Return true if this map contains a mapping whose key has the specified label. More...
 
std::vector< K > const & keys () const noexcept override
 Return the set of all keys, without type information. More...
 
void clear () noexcept override
 Remove all of the mappings from this map. More...
 
template<typename T >
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 conflicting key. More...
 
template<typename T >
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. More...
 
template<typename T >
bool erase (Key< K, T > const &key)
 Remove the mapping for a key from this map, if it exists. More...
 
template<typename T >
size_type count (Key< K, T > const &key) const
 Return the number of elements mapped to the specified key. More...
 
template<typename T >
bool contains (Key< K, T > const &key) const
 Return true if this map contains a mapping for the specified key. More...
 
template<class Visitor >
auto apply (Visitor &&visitor) const
 Apply an operation to each key-value pair in the map. More...
 
template<class Visitor >
auto apply (Visitor &&visitor)
 Apply a modifying operation to each key-value pair in the map. More...
 
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. More...
 
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. More...
 
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. More...
 
virtual bool operator== (GenericMap const &other) const noexcept
 Test for map equality. More...
 
bool operator!= (GenericMap const &other) const
 Test for map equality. More...
 

Protected Types

using StorableType = boost::variant< bool, int, long, long long, float, double, std::string, PolymorphicValue, std::shared_ptr< Storable const > >
 The types that can be stored in a map. More...
 
using ConstValueReference = decltype(_typeToConstRef(std::declval< StorableType >()))
 A type-agnostic reference to the value stored inside the map. More...
 
using ValueReference = decltype(_typeToRef(std::declval< StorableType >()))
 A type-agnostic reference to the value stored inside the map. More...
 

Protected Member Functions

ConstValueReference unsafeLookup (K key) const override
 Return a reference to the mapped value of the element with key equal to key. More...
 
bool unsafeInsert (K key, StorableType &&value) override
 Create a new mapping with key equal to key and value equal to value. More...
 
bool unsafeErase (K key) override
 Remove the mapping for a key from this map, if it exists. More...
 
ValueReference unsafeLookup (K key)
 Return a reference to the mapped value of the element with key equal to key. More...
 

Detailed Description

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

A GenericMap that allows insertion and deletion of arbitrary values.

In Python, a SimpleGenericMap behaves like a dict. In particular, it will iterate over keys in the order they were added.

Template Parameters
Kthe key type of the map. Must be hashable.

Definition at line 50 of file SimpleGenericMap.h.

Member Typedef Documentation

◆ ConstValueReference

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

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 367 of file GenericMap.h.

◆ difference_type

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

Definition at line 82 of file GenericMap.h.

◆ key_type

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

Definition at line 80 of file GenericMap.h.

◆ size_type

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

Definition at line 81 of file GenericMap.h.

◆ StorableType

template<typename K>
using lsst::afw::typehandling::GenericMap< K >::StorableType = boost::variant<bool, int, long, long long, float, double, std::string, PolymorphicValue, std::shared_ptr<Storable const> >
protectedinherited

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 358 of file GenericMap.h.

◆ ValueReference

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

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 368 of file GenericMap.h.

Constructor & Destructor Documentation

◆ SimpleGenericMap() [1/4]

template<typename K >
lsst::afw::typehandling::SimpleGenericMap< K >::SimpleGenericMap ( )
default

◆ SimpleGenericMap() [2/4]

template<typename K >
lsst::afw::typehandling::SimpleGenericMap< K >::SimpleGenericMap ( SimpleGenericMap< K > const &  other)
default

◆ SimpleGenericMap() [3/4]

template<typename K >
lsst::afw::typehandling::SimpleGenericMap< K >::SimpleGenericMap ( SimpleGenericMap< K > &&  )
defaultnoexcept

◆ SimpleGenericMap() [4/4]

template<typename K >
lsst::afw::typehandling::SimpleGenericMap< K >::SimpleGenericMap ( GenericMap< K > const &  other)
inline

Convert another GenericMap into a SimpleGenericMap.

This constructor will insert key-value pairs following other's iteration order. This may not be the order in which they were inserted into the original map.

Definition at line 66 of file SimpleGenericMap.h.

66 : _storage(_convertStorage(other)), _keyView(other.keys()) {}
ItemVariant const * other
Definition: Schema.cc:56

◆ ~SimpleGenericMap()

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

Member Function Documentation

◆ apply() [1/2]

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

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 306 of file GenericMap.h.

306  {
307  // Delegate to private methods to hide special-casing of Visitor
308  return _apply(visitor);
309  }

◆ apply() [2/2]

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

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 328 of file GenericMap.h.

328  {
329  // Delegate to private methods to hide special-casing of Visitor
330  return _apply(visitor);
331  }

◆ at() [1/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)
inlineinherited

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 108 of file GenericMap.h.

108  {
109  // Both casts are safe; see Effective C++, Item 3
110  return const_cast<T&>(static_cast<const GenericMap&>(*this).at(key));
111  }
Key< U > key
Definition: Schema.cc:281

◆ at() [2/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
inlineinherited

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 114 of file GenericMap.h.

114  {
115  // Delegate to private methods to hide further special-casing of T
116  return _at(key);
117  }
Key< U > key
Definition: Schema.cc:281

◆ at() [3/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
inlineinherited

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 120 of file GenericMap.h.

120  {
121  // Delegate to private methods to hide further special-casing of T
122  return _at(key);
123  }
Key< U > key
Definition: Schema.cc:281

◆ clear()

template<typename K >
void lsst::afw::typehandling::SimpleGenericMap< K >::clear ( )
inlineoverridevirtualnoexcept

Remove all of the mappings from this map.

After this call, the map will be empty.

Implements lsst::afw::typehandling::MutableGenericMap< K >.

Definition at line 100 of file SimpleGenericMap.h.

100  {
101  _storage.clear();
102  _keyView.clear();
103  }
T clear(T... args)

◆ contains() [1/2]

template<typename K >
bool lsst::afw::typehandling::SimpleGenericMap< K >::contains ( K const &  key) const
inlineoverridevirtual

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.

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 96 of file SimpleGenericMap.h.

96 { return _storage.count(key) > 0; }
Key< U > key
Definition: Schema.cc:281
T count(T... args)

◆ contains() [2/2]

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

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 193 of file GenericMap.h.

193  {
194  // Delegate to private methods to hide special-casing of T
195  return _contains(key);
196  }
Key< U > key
Definition: Schema.cc:281

◆ count()

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

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 156 of file GenericMap.h.

156  {
157  return contains(key) ? 1 : 0;
158  }
Key< U > key
Definition: Schema.cc:281
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 >
bool lsst::afw::typehandling::SimpleGenericMap< K >::empty ( ) const
inlineoverridevirtualnoexcept

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

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 90 of file SimpleGenericMap.h.

90 { return _storage.empty(); }
T empty(T... args)

◆ erase()

template<typename K >
template<typename T >
bool lsst::afw::typehandling::MutableGenericMap< K >::erase ( Key< K, T > const &  key)
inlineinherited

Remove the mapping for a key from this map, if it exists.

Template Parameters
Tthe type of value the key maps to
Parameters
keythe key to remove
Returns
true if key was removed, false if it was not present
Exception Safety
Provides strong exception safety.
Note
This implementation calls contains, then calls unsafeErase if the key is present.

Definition at line 685 of file GenericMap.h.

685  {
686  if (this->contains(key)) {
687  return unsafeErase(key.getId());
688  } else {
689  return false;
690  }
691  }
Key< U > key
Definition: Schema.cc:281
virtual bool contains(K const &key) const =0
Return true if this map contains a mapping whose key has the specified label.
virtual bool unsafeErase(K key)=0
Remove the mapping for a key from this map, if it exists.

◆ insert() [1/2]

template<typename K >
template<typename T >
bool lsst::afw::typehandling::MutableGenericMap< K >::insert ( Key< K, T > const &  key,
T const &  value 
)
inlineinherited

Insert an element into the map, if the map doesn't already contain a mapping with the same or a conflicting key.

Template Parameters
Tthe type of value to insert
Parameters
keykey to insert
valuevalue to insert
Returns
true if the insertion took place, false otherwise
Exception Safety
Provides strong exception safety.
Note
It is possible for a key with a value type other than T to prevent insertion. Callers can safely assume this->contains(key.getId()) as a postcondition, but not this->contains(key).
This implementation calls contains, then calls unsafeInsert if there is no conflicting key.

Definition at line 639 of file GenericMap.h.

639  {
640  if (this->contains(key.getId())) {
641  return false;
642  }
643 
644  return unsafeInsert(key.getId(), StorableType(value));
645  }
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.
Definition: GenericMap.h:358
Key< U > key
Definition: Schema.cc:281
virtual bool contains(K const &key) const =0
Return true if this map contains a mapping whose key has the specified label.
virtual bool unsafeInsert(K key, StorableType &&value)=0
Create a new mapping with key equal to key and value equal to value.

◆ insert() [2/2]

template<typename K >
template<typename T >
std::pair<Key<K, T>, bool> lsst::afw::typehandling::MutableGenericMap< K >::insert ( K const &  key,
T const &  value 
)
inlineinherited

Insert an element into the map, if the map doesn't already contain a mapping with a conflicting key.

Template Parameters
Tthe type of value to insert
Parameters
keykey to insert
valuevalue to insert
Returns
a pair consisting of a strongly-typed key for the value and a flag that is true if the insertion took place and false otherwise
Exception Safety
Provides strong exception safety.
Warning
the type of the compiler-generated key may not always be what you expect. Callers should save the returned key if they wish to retrieve the value later.

Definition at line 663 of file GenericMap.h.

663  {
664  auto strongKey = makeKey<T>(key);
665  // Construct return value in advance, so that exception from copying/moving Key is atomic
666  auto result = make_pair(strongKey, false);
667  result.second = insert(strongKey, value);
668  return result;
669  }
Key< U > key
Definition: Schema.cc:281
T make_pair(T... args)
bool insert(Key< K, T > const &key, T const &value)
Insert an element into the map, if the map doesn&#39;t already contain a mapping with the same or a confl...
Definition: GenericMap.h:639
py::object result
Definition: _schema.cc:429

◆ keys()

template<typename K >
std::vector<K> const& lsst::afw::typehandling::SimpleGenericMap< K >::keys ( ) const
inlineoverridevirtualnoexcept

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.

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 98 of file SimpleGenericMap.h.

98 { return _keyView; }

◆ max_size()

template<typename K >
GenericMap<K>::size_type lsst::afw::typehandling::SimpleGenericMap< K >::max_size ( ) const
inlineoverridevirtualnoexcept

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.

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 92 of file SimpleGenericMap.h.

92  {
93  return std::min(_storage.max_size(), _keyView.max_size());
94  }
T min(T... args)
T max_size(T... args)

◆ operator!=()

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

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 242 of file GenericMap.h.

242 { return !(*this == other); }
ItemVariant const * other
Definition: Schema.cc:56

◆ operator=() [1/3]

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

Definition at line 69 of file SimpleGenericMap.h.

69  {
70  std::vector<K> newKeys = other._keyView;
71  _storage = other._storage;
72  // strong exception safety because no exceptions can occur past this point
73  using std::swap;
74  swap(_keyView, newKeys);
75  return *this;
76  }
T swap(T... args)
ItemVariant const * other
Definition: Schema.cc:56
void swap(PolymorphicValue &lhs, PolymorphicValue &rhs) noexcept
Swap specialization for PolymorphicValue.

◆ operator=() [2/3]

template<typename K >
SimpleGenericMap& lsst::afw::typehandling::SimpleGenericMap< K >::operator= ( SimpleGenericMap< K > &&  )
defaultnoexcept

◆ operator=() [3/3]

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

Definition at line 78 of file SimpleGenericMap.h.

78  {
79  std::vector<K> newKeys = other.keys();
80  // strong exception safety: unordered_map is nothrow move-assignable and
81  // vector is nothrow swappable, so no exceptions can occur after _convertStorage returns
82  _storage = _convertStorage(other);
83  using std::swap;
84  swap(_keyView, newKeys);
85  return *this;
86  }
T swap(T... args)
ItemVariant const * other
Definition: Schema.cc:56
void swap(PolymorphicValue &lhs, PolymorphicValue &rhs) noexcept
Swap specialization for PolymorphicValue.

◆ operator==()

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

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 228 of file GenericMap.h.

228  {
229  auto keys1 = this->keys();
230  auto keys2 = other.keys();
231  if (!std::is_permutation(keys1.begin(), keys1.end(), keys2.begin(), keys2.end())) {
232  return false;
233  }
234  for (K const& key : keys1) {
235  if (this->unsafeLookup(key) != other.unsafeLookup(key)) {
236  return false;
237  }
238  }
239  return true;
240  }
T is_permutation(T... args)
ItemVariant const * other
Definition: Schema.cc:56
virtual ConstValueReference unsafeLookup(K key) const =0
Return a reference to the mapped value of the element with key equal to key.
Key< U > key
Definition: Schema.cc:281
virtual std::vector< K > const & keys() const noexcept=0
Return the set of all keys, without type information.

◆ size()

template<typename K >
GenericMap<K>::size_type lsst::afw::typehandling::SimpleGenericMap< K >::size ( ) const
inlineoverridevirtualnoexcept

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

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 88 of file SimpleGenericMap.h.

88 { return _storage.size(); }
T size(T... args)

◆ unsafeErase()

template<typename K >
bool lsst::afw::typehandling::SimpleGenericMap< K >::unsafeErase ( key)
inlineoverrideprotectedvirtual

Remove the mapping for a key from this map, if it exists.

Parameters
keythe key to remove
Returns
true if key was removed, false if it was not present
Exception Safety
Must provide strong exception safety.

Implements lsst::afw::typehandling::MutableGenericMap< K >.

Definition at line 129 of file SimpleGenericMap.h.

129  {
130  std::vector<K> newKeys = _keyView;
131  for (auto it = newKeys.cbegin(); it != newKeys.cend();) {
132  if (*it == key) {
133  it = newKeys.erase(it);
134  } else {
135  ++it;
136  }
137  }
138  // strong exception safety because no exceptions can occur past this point
139  bool erased = _storage.erase(key) > 0;
140  if (erased) {
141  using std::swap;
142  swap(_keyView, newKeys);
143  }
144  return erased;
145  }
T swap(T... args)
T cend(T... args)
T erase(T... args)
Key< U > key
Definition: Schema.cc:281
T cbegin(T... args)
void swap(PolymorphicValue &lhs, PolymorphicValue &rhs) noexcept
Swap specialization for PolymorphicValue.

◆ unsafeInsert()

template<typename K >
bool lsst::afw::typehandling::SimpleGenericMap< K >::unsafeInsert ( key,
StorableType &&  value 
)
inlineoverrideprotectedvirtual

Create a new mapping with key equal to key and value equal to value.

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

Parameters
keythe key of the element to insert. The method may assume that the map does not contain key.
valuea reference to the value to insert.
Returns
true if the insertion took place, false otherwise
Exception Safety
Must provide strong exception safety.

Implements lsst::afw::typehandling::MutableGenericMap< K >.

Definition at line 116 of file SimpleGenericMap.h.

116  {
117  std::vector<K> newKeys = _keyView;
118  newKeys.emplace_back(key);
119  bool inserted = _storage.emplace(key, std::move(value)).second;
120  // strong exception safety because no exceptions can occur past this point
121  if (inserted) {
122  // _storage did not previously include key, so the key appended to newKeys is unique
123  using std::swap;
124  swap(_keyView, newKeys);
125  }
126  return inserted;
127  }
T swap(T... args)
Key< U > key
Definition: Schema.cc:281
T move(T... args)
T emplace(T... args)
void swap(PolymorphicValue &lhs, PolymorphicValue &rhs) noexcept
Swap specialization for PolymorphicValue.
T emplace_back(T... args)

◆ unsafeLookup() [1/2]

template<typename K >
ConstValueReference lsst::afw::typehandling::SimpleGenericMap< K >::unsafeLookup ( key) const
inlineoverrideprotectedvirtual

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.

Implements lsst::afw::typehandling::GenericMap< K >.

Definition at line 106 of file SimpleGenericMap.h.

106  {
107  try {
108  return _storage.at(key);
109  } catch (std::out_of_range& e) {
110  std::stringstream message;
111  message << "Key not found: " << key;
112  std::throw_with_nested(LSST_EXCEPT(pex::exceptions::OutOfRangeError, message.str()));
113  }
114  }
T at(T... args)
T throw_with_nested(T... args)
Key< U > key
Definition: Schema.cc:281
T str(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48

◆ unsafeLookup() [2/2]

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

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 389 of file GenericMap.h.

389  {
390  ConstValueReference constRef = static_cast<const GenericMap&>(*this).unsafeLookup(key);
391  auto removeConst = [](auto const& value) -> ValueReference {
392  using NonConstRef = std::add_lvalue_reference_t<
393  std::remove_const_t<std::remove_reference_t<decltype(value)>>>;
394  // This cast is safe; see Effective C++, Item 3
395  return const_cast<NonConstRef>(value);
396  };
397  return boost::apply_visitor(removeConst, constRef);
398  }
decltype(_typeToConstRef(std::declval< StorableType >())) ConstValueReference
A type-agnostic reference to the value stored inside the map.
Definition: GenericMap.h:367
Key< U > key
Definition: Schema.cc:281
decltype(_typeToRef(std::declval< StorableType >())) ValueReference
A type-agnostic reference to the value stored inside the map.
Definition: GenericMap.h:368

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