LSSTApplications  18.1.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"
55 #include "lsst/daf/base/Citizen.h"
57 #include "lsst/pex/exceptions.h"
58 
59 namespace lsst {
60 namespace daf {
61 namespace base {
62 
63 #if defined(__ICC)
64 #pragma warning(push)
65 #pragma warning(disable : 444)
66 #endif
67 
69 public:
70  // Typedefs
73 
79  explicit PropertySet(bool flat = false);
80 
82  virtual ~PropertySet() noexcept;
83 
84  // No copying
85  PropertySet(const PropertySet&) = delete;
86  PropertySet& operator=(const PropertySet&) = delete;
87 
88  // No moving
89  PropertySet(PropertySet&&) = delete;
90  PropertySet& operator=(PropertySet&&) = delete;
91 
92  // Accessors
93 
99  virtual Ptr deepCopy() const;
100 
107  size_t nameCount(bool topLevelOnly = true) const;
108 
116  std::vector<std::string> names(bool topLevelOnly = true) const;
117 
121  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
122 
126  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
127 
134  bool exists(std::string const& name) const;
135 
142  bool isArray(std::string const& name) const;
143 
150  bool isPropertySetPtr(std::string const& name) const;
151 
158  bool isUndefined(std::string const& name) const;
159 
166  size_t valueCount(std::string const& name) const;
167 
177  std::type_info const& typeOf(std::string const& name) const;
178 
182  // Implemented in the .cc file to work around symbol visiblity issues on macOS
183  // e.g. https://github.com/pybind/pybind11/issues/1503
184  template <typename T>
185  static std::type_info const& typeOfT();
186 
187  // The following throw an exception if the type does not match exactly.
188 
200  template <typename T>
201  T get(std::string const& name) const;
202 
215  template <typename T>
216  T get(std::string const& name, T const& defaultValue) const;
217 
229  template <typename T>
230  std::vector<T> getArray(std::string const& name) const;
231 
232  // The following throw an exception if the conversion is inappropriate.
233 
244  bool getAsBool(std::string const& name) const;
245 
255  int getAsInt(std::string const& name) const;
256 
268  int64_t getAsInt64(std::string const& name) const;
269 
279  uint64_t getAsUInt64(std::string const& name) const;
280 
290  double getAsDouble(std::string const& name) const;
291 
303  std::string getAsString(std::string const& name) const;
304 
313  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
314 
323  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
324 
334  virtual std::string toString(bool topLevelOnly = false, std::string const& indent = "") const;
335 
336  // Modifiers
337 
346  template <typename T>
347  void set(std::string const& name, T const& value);
348 
357  template <typename T>
358  void set(std::string const& name, std::vector<T> const& value);
359 
367  void set(std::string const& name, char const* value);
368 
378  template <typename T>
379  void add(std::string const& name, T const& value);
380 
392  template <typename T>
393  void add(std::string const& name, std::vector<T> const& value);
394 
405  void add(std::string const& name, char const* value);
406 
420  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
421  bool asScalar = false);
422 
436  virtual void combine(ConstPtr source);
437 
444  virtual void remove(std::string const& name);
445 
446 protected:
447  /*
448  * Find the property name (possibly hierarchical) and set or replace its
449  * value with the given vector of values. Hook for subclass overrides of
450  * top-level setting.
451  *
452  * @param[in] name Property name to find, possibly hierarchical.
453  * @param[in] vp shared_ptr to vector of values.
454  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
455  */
456  virtual void _set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
457 
458  /*
459  * Find the property name (possibly hierarchical) and append or set its
460  * value with the given vector of values.
461  *
462  * @param[in] name Property name to find, possibly hierarchical.
463  * @param[in] vp shared_ptr to vector of values.
464  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
465  */
466  virtual void _add(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
467 
468  // Format a value in human-readable form; called by toString
469  virtual std::string _format(std::string const& name) const;
470 
471 private:
472 
473  typedef std::unordered_map<std::string, std::shared_ptr<std::vector<boost::any> > > AnyMap;
474 
475  /*
476  * Find the property name (possibly hierarchical).
477  *
478  * @param[in] name Property name to find, possibly hierarchical.
479  * @return unordered_map::iterator to the property or end() if nonexistent.
480  */
481  AnyMap::iterator _find(std::string const& name);
482 
483  /*
484  * Find the property name (possibly hierarchical). Const version.
485  *
486  * @param[in] name Property name to find, possibly hierarchical.
487  * @return unordered_map::const_iterator to the property or end().
488  */
489  AnyMap::const_iterator _find(std::string const& name) const;
490 
491  /*
492  * Find the property name (possibly hierarchical) and set or replace its
493  * value with the given vector of values.
494  *
495  * @param[in] name Property name to find, possibly hierarchical.
496  * @param[in] vp shared_ptr to vector of values.
497  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
498  */
499  virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
500  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
501  void _cycleCheckAnyVec(std::vector<boost::any> const& v, std::string const& name);
502  void _cycleCheckPtr(Ptr const& v, std::string const& name);
503 
504  AnyMap _map;
505  bool _flat;
506 };
507 
508 #if defined(__ICC)
509 #pragma warning(pop)
510 #endif
511 
512 template <>
513 void PropertySet::add<PropertySet::Ptr>(std::string const& name, Ptr const& value);
514 template <>
515 void PropertySet::add<PropertySet::Ptr>(std::string const& name, std::vector<Ptr> const& value);
516 }
517 } // namespace daf
518 } // namespace lsst
519 
520 #endif
Interface for Persistable base class.
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:71
Basic LSST definitions.
Definition: Polygon.cc:25
STL namespace.
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:72
daf::base::PropertySet * set
Definition: fits.cc:884
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
STL class.
A base class for image defects.
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
STL class.
Class for storing generic metadata.
Definition: PropertySet.h:68
#define LSST_EXPORT
Make a symbol visible even if visiblity is hidden (e.g.
Definition: base.h:54
Base class for all persistable classes.
Definition: Persistable.h:75
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55