LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
GenericMap.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_GENERICMAP_H
26 #define LSST_AFW_TYPEHANDLING_GENERICMAP_H
27 
28 #include <algorithm>
29 #include <memory>
30 #include <sstream>
31 #include <string>
32 #include <type_traits>
33 #include <utility>
34 #include <vector>
35 
36 #include "boost/variant.hpp"
37 
38 #include "lsst/pex/exceptions.h"
43 
44 namespace lsst {
45 namespace afw {
46 namespace typehandling {
47 
76 // TODO: correctly handling const vs. non-const keys should be possible in C++17 with std::variant
77 template <typename K>
78 class GenericMap {
79 public:
80  using key_type = K;
83 
84  virtual ~GenericMap() noexcept = default;
85 
107  template <typename T, typename std::enable_if_t<!detail::IS_SMART_PTR<T>, int> = 0>
108  T& at(Key<K, T> const& key) {
109  // Both casts are safe; see Effective C++, Item 3
110  return const_cast<T&>(static_cast<const GenericMap&>(*this).at(key));
111  }
112 
113  template <typename T, typename std::enable_if_t<!detail::IS_SMART_PTR<T>, int> = 0>
114  T const& at(Key<K, T> const& key) const {
115  // Delegate to private methods to hide further special-casing of T
116  return _at(key);
117  }
118 
119  template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value, int> = 0>
121  // Delegate to private methods to hide further special-casing of T
122  return _at(key);
123  }
124 
127  virtual size_type size() const noexcept = 0;
129 
131  virtual bool empty() const noexcept = 0;
132 
140  virtual size_type max_size() const noexcept = 0;
141 
155  template <typename T>
156  size_type count(Key<K, T> const& key) const {
157  return contains(key) ? 1 : 0;
158  }
159 
172  virtual bool contains(K const& key) const = 0;
173 
192  template <typename T>
193  bool contains(Key<K, T> const& key) const {
194  // Delegate to private methods to hide special-casing of T
195  return _contains(key);
196  }
197 
212  virtual std::vector<K> const& keys() const noexcept = 0;
213 
228  virtual bool operator==(GenericMap const& other) const noexcept {
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  }
241 
242  bool operator!=(GenericMap const& other) const { return !(*this == other); }
243 
305  template <class Visitor>
306  auto apply(Visitor&& visitor) const {
307  // Delegate to private methods to hide special-casing of Visitor
308  return _apply(visitor);
309  }
310 
327  template <class Visitor>
328  auto apply(Visitor&& visitor) {
329  // Delegate to private methods to hide special-casing of Visitor
330  return _apply(visitor);
331  }
332 
333 private:
334  // Transformations between value/reference/ref-to-const versions of internal variant type, to let
335  // the set of value types supported by GenericMap be defined only once
336  // Icky TMP, but I can't find another way to get at the template arguments for variant :(
337  // Methods have no definition but can't be deleted without breaking type definitions below
338 
340  // may need to use std::reference_wrapper when migrating to std::variant, but it confuses Boost
341  template <typename... Types>
342  static boost::variant<std::add_lvalue_reference_t<Types>...> _typeToRef(
343  boost::variant<Types...> const&) noexcept;
344  template <typename... Types>
345  static boost::variant<std::add_lvalue_reference_t<std::add_const_t<Types>>...> _typeToConstRef(
346  boost::variant<Types...> const&) noexcept;
348 
349 protected:
355  // Use int, long, long long instead of int32_t, int64_t because C++ doesn't
356  // consider signed integers of the same length but different names equivalent
357  using StorableType = boost::variant<bool, int, long, long long, float, double, std::string,
359 
366  // this mouthful is shorter than the equivalent expression with result_of
367  using ConstValueReference = decltype(_typeToConstRef(std::declval<StorableType>()));
368  using ValueReference = decltype(_typeToRef(std::declval<StorableType>()));
369 
387  virtual ConstValueReference unsafeLookup(K key) const = 0;
388 
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  }
399 
402 private:
403  // Neither Storable nor shared_ptr<Storable>
404  template <typename T,
405  typename std::enable_if_t<
407  T const& _at(Key<K, T> const& key) const {
408  static_assert(!std::is_const<T>::value,
409  "Due to implementation constraints, const keys are not supported.");
410  try {
411  auto foo = unsafeLookup(key.getId());
412  return boost::get<T const&>(foo);
413  } catch (boost::bad_get const&) {
414  std::stringstream message;
415  message << "Key " << key << " not found, but a key labeled " << key.getId() << " is present.";
417  }
418  }
419 
420  // Storable and its subclasses
421  template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value, int> = 0>
422  T const& _at(Key<K, T> const& key) const {
423  static_assert(!std::is_const<T>::value,
424  "Due to implementation constraints, const keys are not supported.");
425  try {
426  auto foo = unsafeLookup(key.getId());
427  // Don't use pointer-based get, because it won't work after migrating to std::variant
428  Storable const& value = boost::get<PolymorphicValue const&>(foo);
429  T const* typedPointer = dynamic_cast<T const*>(&value);
430  if (typedPointer != nullptr) {
431  return *typedPointer;
432  } else {
433  std::stringstream message;
434  message << "Key " << key << " not found, but a key labeled " << key.getId() << " is present.";
435  throw LSST_EXCEPT(pex::exceptions::OutOfRangeError, message.str());
436  }
437  } catch (boost::bad_get const&) {
438  std::stringstream message;
439  message << "Key " << key << " not found, but a key labeled " << key.getId() << " is present.";
440  throw LSST_EXCEPT(pex::exceptions::OutOfRangeError, message.str());
441  }
442  }
443 
444  // shared_ptr<Storable>
445  template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value, int> = 0>
446  std::shared_ptr<T> _at(Key<K, std::shared_ptr<T>> const& key) const {
447  static_assert(std::is_const<T>::value,
448  "Due to implementation constraints, pointers to non-const are not supported.");
449  try {
450  auto foo = unsafeLookup(key.getId());
451  auto pointer = boost::get<std::shared_ptr<Storable const> const&>(foo);
452  if (pointer == nullptr) { // dynamic_cast not helpful
453  return nullptr;
454  }
455 
456  std::shared_ptr<T> typedPointer = std::dynamic_pointer_cast<T>(pointer);
457  // shared_ptr can be empty without being null. dynamic_pointer_cast
458  // only promises result of failed cast is empty, so test for that
459  if (typedPointer.use_count() > 0) {
460  return typedPointer;
461  } else {
462  std::stringstream message;
463  message << "Key " << key << " not found, but a key labeled " << key.getId() << " is present.";
464  throw LSST_EXCEPT(pex::exceptions::OutOfRangeError, message.str());
465  }
466  } catch (boost::bad_get const&) {
467  std::stringstream message;
468  message << "Key " << key << " not found, but a key labeled " << key.getId() << " is present.";
469  throw LSST_EXCEPT(pex::exceptions::OutOfRangeError, message.str());
470  }
471  }
472 
473  // Neither Storable nor shared_ptr<Storable>
474  template <typename T,
475  typename std::enable_if_t<
477  bool _contains(Key<K, T> const& key) const {
478  // Avoid actually getting and casting an object, if at all possible
479  if (!contains(key.getId())) {
480  return false;
481  }
482 
483  auto foo = unsafeLookup(key.getId());
484  // boost::variant has no equivalent to std::holds_alternative
485  try {
486  boost::get<T const&>(foo);
487  return true;
488  } catch (boost::bad_get const&) {
489  return false;
490  }
491  }
492 
493  // Storable and its subclasses
494  template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value, int> = 0>
495  bool _contains(Key<K, T> const& key) const {
496  // Avoid actually getting and casting an object, if at all possible
497  if (!contains(key.getId())) {
498  return false;
499  }
500 
501  auto foo = unsafeLookup(key.getId());
502  try {
503  // Don't use pointer-based get, because it won't work after migrating to std::variant
504  Storable const& value = boost::get<PolymorphicValue const&>(foo);
505  auto asT = dynamic_cast<T const*>(&value);
506  return asT != nullptr;
507  } catch (boost::bad_get const&) {
508  return false;
509  }
510  }
511 
512  // shared_ptr<Storable>
513  template <typename T, typename std::enable_if_t<std::is_base_of<Storable, T>::value, int> = 0>
514  bool _contains(Key<K, std::shared_ptr<T>> const& key) const {
515  static_assert(std::is_const<T>::value,
516  "Due to implementation constraints, pointers to non-const are not supported.");
517  // Avoid actually getting and casting an object, if at all possible
518  if (!contains(key.getId())) {
519  return false;
520  }
521 
522  auto foo = unsafeLookup(key.getId());
523  try {
524  auto pointer = boost::get<std::shared_ptr<Storable const> const&>(foo);
525  if (pointer == nullptr) { // Can't confirm type with dynamic_cast
526  return true;
527  }
528  std::shared_ptr<T> typedPointer = std::dynamic_pointer_cast<T>(pointer);
529  // shared_ptr can be empty without being null. dynamic_pointer_cast
530  // only promises result of failed cast is empty, so test for that
531  return typedPointer.use_count() > 0;
532  } catch (boost::bad_get const&) {
533  return false;
534  }
535  }
536 
537  // Type alias to properly handle Visitor output
538  // Assume that each operator() has the same return type; variant will enforce it
540  template <class Visitor>
541  using _VisitorResult = std::result_of_t<Visitor && (K&&, bool&)>;
543 
544  // No return value, const GenericMap
545  template <class Visitor, typename std::enable_if_t<std::is_void<_VisitorResult<Visitor>>::value, int> = 0>
546  void _apply(Visitor&& visitor) const {
547  for (K const& key : keys()) {
548  boost::variant<K> varKey = key;
549  boost::apply_visitor(visitor, varKey, unsafeLookup(key));
550  }
551  }
552 
553  // Return value, const GenericMap
554  template <class Visitor,
555  typename std::enable_if_t<!std::is_void<_VisitorResult<Visitor>>::value, int> = 0>
556  auto _apply(Visitor&& visitor) const {
558  results.reserve(size());
559 
560  for (K const& key : keys()) {
561  boost::variant<K> varKey = key;
562  results.emplace_back(boost::apply_visitor(visitor, varKey, unsafeLookup(key)));
563  }
564  return results;
565  }
566 
567  // No return value, non-const GenericMap
568  template <class Visitor, typename std::enable_if_t<std::is_void<_VisitorResult<Visitor>>::value, int> = 0>
569  void _apply(Visitor&& visitor) {
570  for (K const& key : keys()) {
571  boost::variant<K> varKey = key;
572  // Boost gets confused if we pass it a temporary variant
574  boost::apply_visitor(visitor, varKey, ref);
575  }
576  }
577 
578  // Return value, non-const GenericMap
579  template <class Visitor,
580  typename std::enable_if_t<!std::is_void<_VisitorResult<Visitor>>::value, int> = 0>
581  auto _apply(Visitor&& visitor) {
583  results.reserve(size());
584 
585  for (K const& key : keys()) {
586  boost::variant<K> varKey = key;
587  // Boost gets confused if we pass it a temporary variant
589  results.emplace_back(boost::apply_visitor(visitor, varKey, ref));
590  }
591  return results;
592  }
593 };
594 
605 template <typename K>
606 class MutableGenericMap : public GenericMap<K> {
607 protected:
608  using typename GenericMap<K>::StorableType;
609 
610 public:
611  virtual ~MutableGenericMap() noexcept = default;
612 
618  virtual void clear() noexcept = 0;
619 
638  template <typename T>
639  bool insert(Key<K, T> const& key, T const& value) {
640  if (this->contains(key.getId())) {
641  return false;
642  }
643 
644  return unsafeInsert(key.getId(), StorableType(value));
645  }
646 
662  template <typename T>
663  std::pair<Key<K, T>, bool> insert(K const& key, T const& value) {
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  }
670 
684  template <typename T>
685  bool erase(Key<K, T> const& key) {
686  if (this->contains(key)) {
687  return unsafeErase(key.getId());
688  } else {
689  return false;
690  }
691  }
692 
693 protected:
706  virtual bool unsafeInsert(K key, StorableType&& value) = 0;
707 
717  virtual bool unsafeErase(K key) = 0;
718 };
719 
720 } // namespace typehandling
721 } // namespace afw
722 } // namespace lsst
723 
724 #endif
lsst::afw::typehandling::GenericMap::at
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.
Definition: GenericMap.h:120
lsst::afw::typehandling::GenericMap::count
size_type count(Key< K, T > const &key) const
Return the number of elements mapped to the specified key.
Definition: GenericMap.h:156
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::typehandling::GenericMap::keys
virtual std::vector< K > const & keys() const noexcept=0
Return the set of all keys, without type information.
lsst::afw::typehandling::GenericMap::apply
auto apply(Visitor &&visitor)
Apply a modifying operation to each key-value pair in the map.
Definition: GenericMap.h:328
lsst::afw::typehandling::GenericMap::contains
virtual bool contains(K const &key) const =0
Return true if this map contains a mapping whose key has the specified label.
std::pair
std::vector::reserve
T reserve(T... args)
lsst::afw::typehandling::GenericMap::max_size
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 implemen...
lsst::afw::typehandling::GenericMap::ConstValueReference
decltype(_typeToConstRef(std::declval< StorableType >())) ConstValueReference
A type-agnostic reference to the value stored inside the map.
Definition: GenericMap.h:367
PolymorphicValue.h
std::vector< K >
lsst::afw::typehandling::GenericMap::unsafeLookup
ValueReference unsafeLookup(K key)
Return a reference to the mapped value of the element with key equal to key.
Definition: GenericMap.h:389
lsst::afw
Definition: imageAlgorithm.dox:1
std::stringstream
STL class.
std::is_permutation
T is_permutation(T... args)
lsst::afw::typehandling::GenericMap::size
virtual size_type size() const noexcept=0
Return the number of key-value pairs in the map.
lsst::afw::typehandling::GenericMap::operator!=
bool operator!=(GenericMap const &other) const
Test for map equality.
Definition: GenericMap.h:242
std::is_fundamental
lsst::afw::typehandling::MutableGenericMap::clear
virtual void clear() noexcept=0
Remove all of the mappings from this map.
lsst::afw::typehandling::GenericMap
Interface for a heterogeneous map.
Definition: GenericMap.h:78
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::afw::typehandling::GenericMap::StorableType
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
lsst::afw::typehandling::GenericMap::unsafeLookup
virtual ConstValueReference unsafeLookup(K key) const =0
Return a reference to the mapped value of the element with key equal to key.
Storable.h
lsst::afw::typehandling::PolymorphicValue
Container that passes Storable objects by value while preserving type.
Definition: PolymorphicValue.h:52
lsst::afw::typehandling::GenericMap::ValueReference
decltype(_typeToRef(std::declval< StorableType >())) ValueReference
A type-agnostic reference to the value stored inside the map.
Definition: GenericMap.h:368
lsst::afw::typehandling::detail::IS_SMART_PTR
constexpr bool IS_SMART_PTR
Definition: type_traits.h:38
result
py::object result
Definition: _schema.cc:429
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::typehandling::Key
Key for type-safe lookup in a GenericMap.
Definition: Key.h:52
std::vector::emplace_back
T emplace_back(T... args)
std::is_const
lsst::afw::typehandling::GenericMap::size_type
std::size_t size_type
Definition: GenericMap.h:81
lsst::afw::typehandling::MutableGenericMap::erase
bool erase(Key< K, T > const &key)
Remove the mapping for a key from this map, if it exists.
Definition: GenericMap.h:685
std
STL namespace.
type_traits.h
lsst::afw::typehandling::GenericMap::at
T & at(Key< K, T > const &key)
Return a reference to the mapped value of the element with key equal to key.
Definition: GenericMap.h:108
key
Key< U > key
Definition: Schema.cc:281
lsst::afw::typehandling::MutableGenericMap
Interface for a GenericMap that allows element addition and removal.
Definition: GenericMap.h:606
lsst.pex::exceptions::OutOfRangeError
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
std::ptrdiff_t
lsst::afw::typehandling::MutableGenericMap::insert
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.
Definition: GenericMap.h:663
lsst::afw::typehandling::GenericMap::~GenericMap
virtual ~GenericMap() noexcept=default
lsst::afw::typehandling::MutableGenericMap::insert
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...
Definition: GenericMap.h:639
std::stringstream::str
T str(T... args)
std::size_t
lsst::afw::typehandling::GenericMap::apply
auto apply(Visitor &&visitor) const
Apply an operation to each key-value pair in the map.
Definition: GenericMap.h:306
lsst::afw::typehandling::GenericMap::at
T const & at(Key< K, T > const &key) const
Return a reference to the mapped value of the element with key equal to key.
Definition: GenericMap.h:114
lsst::afw::typehandling::GenericMap::empty
virtual bool empty() const noexcept=0
Return true if this map contains no key-value pairs.
lsst::afw::typehandling::GenericMap::contains
bool contains(Key< K, T > const &key) const
Return true if this map contains a mapping for the specified key.
Definition: GenericMap.h:193
lsst::afw::typehandling::MutableGenericMap::~MutableGenericMap
virtual ~MutableGenericMap() noexcept=default
Key.h
lsst::afw::typehandling::MutableGenericMap::unsafeErase
virtual bool unsafeErase(K key)=0
Remove the mapping for a key from this map, if it exists.
lsst::afw::typehandling::MutableGenericMap::unsafeInsert
virtual bool unsafeInsert(K key, StorableType &&value)=0
Create a new mapping with key equal to key and value equal to value.
lsst::afw::typehandling::GenericMap::key_type
K key_type
Definition: GenericMap.h:80
std::ref
T ref(T... args)
std::is_base_of
exceptions.h