LSSTApplications  20.0.0
LSSTDataManagementBasePackage
PropertySet.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_DAF_BASE_PROPERTYSET
26 #define LSST_DAF_BASE_PROPERTYSET
27 
46 #include <memory>
47 #include <string>
48 #include <typeinfo>
49 #include <unordered_map>
50 #include <vector>
51 
52 #include "boost/any.hpp"
53 
54 #include "lsst/base.h"
56 #include "lsst/pex/exceptions.h"
57 
58 namespace lsst {
59 namespace daf {
60 namespace base {
61 
62 #if defined(__ICC)
63 #pragma warning(push)
64 #pragma warning(disable : 444)
65 #endif
66 
68 public:
69  // Typedefs
72 
78  explicit PropertySet(bool flat = false);
79 
81  virtual ~PropertySet() noexcept;
82 
83  // No copying
84  PropertySet(const PropertySet&) = delete;
85  PropertySet& operator=(const PropertySet&) = delete;
86 
87  // No moving
88  PropertySet(PropertySet&&) = delete;
89  PropertySet& operator=(PropertySet&&) = delete;
90 
91  // Accessors
92 
98  virtual Ptr deepCopy() const;
99 
106  size_t nameCount(bool topLevelOnly = true) const;
107 
115  std::vector<std::string> names(bool topLevelOnly = true) const;
116 
120  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
121 
125  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
126 
133  bool exists(std::string const& name) const;
134 
141  bool isArray(std::string const& name) const;
142 
149  bool isPropertySetPtr(std::string const& name) const;
150 
157  bool isUndefined(std::string const& name) const;
158 
165  size_t valueCount(std::string const& name) const;
166 
176  std::type_info const& typeOf(std::string const& name) const;
177 
181  // Implemented in the .cc file to work around symbol visiblity issues on macOS
182  // e.g. https://github.com/pybind/pybind11/issues/1503
183  template <typename T>
184  static std::type_info const& typeOfT();
185 
186  // The following throw an exception if the type does not match exactly.
187 
199  template <typename T>
200  T get(std::string const& name) const;
201 
214  template <typename T>
215  T get(std::string const& name, T const& defaultValue) const;
216 
228  template <typename T>
229  std::vector<T> getArray(std::string const& name) const;
230 
231  // The following throw an exception if the conversion is inappropriate.
232 
243  bool getAsBool(std::string const& name) const;
244 
254  int getAsInt(std::string const& name) const;
255 
267  int64_t getAsInt64(std::string const& name) const;
268 
278  uint64_t getAsUInt64(std::string const& name) const;
279 
289  double getAsDouble(std::string const& name) const;
290 
302  std::string getAsString(std::string const& name) const;
303 
312  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
313 
322  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
323 
333  virtual std::string toString(bool topLevelOnly = false, std::string const& indent = "") const;
334 
335  // Modifiers
336 
345  template <typename T>
346  void set(std::string const& name, T const& value);
347 
356  template <typename T>
357  void set(std::string const& name, std::vector<T> const& value);
358 
366  void set(std::string const& name, char const* value);
367 
377  template <typename T>
378  void add(std::string const& name, T const& value);
379 
391  template <typename T>
392  void add(std::string const& name, std::vector<T> const& value);
393 
404  void add(std::string const& name, char const* value);
405 
419  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
420  bool asScalar = false);
421 
435  virtual void combine(ConstPtr source);
436 
443  virtual void remove(std::string const& name);
444 
445 protected:
446  /*
447  * Find the property name (possibly hierarchical) and set or replace its
448  * value with the given vector of values. Hook for subclass overrides of
449  * top-level setting.
450  *
451  * @param[in] name Property name to find, possibly hierarchical.
452  * @param[in] vp shared_ptr to vector of values.
453  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
454  */
455  virtual void _set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
456 
457  /*
458  * Find the property name (possibly hierarchical) and append or set its
459  * value with the given vector of values.
460  *
461  * @param[in] name Property name to find, possibly hierarchical.
462  * @param[in] vp shared_ptr to vector of values.
463  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
464  */
465  virtual void _add(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
466 
467  // Format a value in human-readable form; called by toString
468  virtual std::string _format(std::string const& name) const;
469 
470 private:
471 
472  typedef std::unordered_map<std::string, std::shared_ptr<std::vector<boost::any> > > AnyMap;
473 
474  /*
475  * Find the property name (possibly hierarchical).
476  *
477  * @param[in] name Property name to find, possibly hierarchical.
478  * @return unordered_map::iterator to the property or end() if nonexistent.
479  */
480  AnyMap::iterator _find(std::string const& name);
481 
482  /*
483  * Find the property name (possibly hierarchical). Const version.
484  *
485  * @param[in] name Property name to find, possibly hierarchical.
486  * @return unordered_map::const_iterator to the property or end().
487  */
488  AnyMap::const_iterator _find(std::string const& name) const;
489 
490  /*
491  * Find the property name (possibly hierarchical) and set or replace its
492  * value with the given vector of values.
493  *
494  * @param[in] name Property name to find, possibly hierarchical.
495  * @param[in] vp shared_ptr to vector of values.
496  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
497  */
498  virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
499  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
500  void _cycleCheckAnyVec(std::vector<boost::any> const& v, std::string const& name);
501  void _cycleCheckPtr(Ptr const& v, std::string const& name);
502 
503  AnyMap _map;
504  bool _flat;
505 };
506 
507 #if defined(__ICC)
508 #pragma warning(pop)
509 #endif
510 
511 template <>
512 void PropertySet::add<PropertySet::Ptr>(std::string const& name, Ptr const& value);
513 template <>
514 void PropertySet::add<PropertySet::Ptr>(std::string const& name, std::vector<Ptr> const& value);
515 }
516 } // namespace daf
517 } // namespace lsst
518 
519 #endif
std::string
STL class.
std::shared_ptr< PropertySet >
base
Definition: __init__.py:1
std::vector
STL class.
boost
Definition: Polygon.cc:25
lsst::daf::base::PropertySet::Ptr
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:70
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
Persistable.h
Interface for Persistable base class.
LSST_EXPORT
#define LSST_EXPORT
Make a symbol visible even if visiblity is hidden (e.g.
Definition: base.h:54
ast::detail::source
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
lsst::geom::any
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
Definition: CoordinateExpr.h:89
base.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::daf::base::PropertySet::ConstPtr
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:71
lsst::daf::base::Persistable
Base class for all persistable classes.
Definition: Persistable.h:75
std
STL namespace.
lsst::daf::base::PropertySet::~PropertySet
virtual ~PropertySet() noexcept
Destructor.
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst::daf::base::PropertySet::PropertySet
PropertySet(bool flat=false)
Construct an empty PropertySet.
std::unordered_map< std::string, std::shared_ptr< std::vector< boost::any > > >
set
daf::base::PropertySet * set
Definition: fits.cc:912
exceptions.h