lsst.daf.base  16.0-7-gc370964+1
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 
62 namespace persistence {
63 class PropertySetFormatter;
64 } // namespace persistence
65 
66 namespace base {
67 
68 #if defined(__ICC)
69 #pragma warning(push)
70 #pragma warning(disable : 444)
71 #endif
72 
73 class LSST_EXPORT PropertySet : public Persistable, public Citizen {
74 public:
75  // Typedefs
78 
84  explicit PropertySet(bool flat = false);
85 
87  virtual ~PropertySet(void);
88 
89  // No copying
90  PropertySet(const PropertySet&) = delete;
91  PropertySet& operator=(const PropertySet&) = delete;
92 
93  // No moving
94  PropertySet(PropertySet&&) = delete;
95  PropertySet& operator=(PropertySet&&) = delete;
96 
97  // Accessors
98 
104  virtual Ptr deepCopy(void) const;
105 
112  size_t nameCount(bool topLevelOnly = true) const;
113 
121  std::vector<std::string> names(bool topLevelOnly = true) const;
122 
126  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
127 
131  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
132 
139  bool exists(std::string const& name) const;
140 
147  bool isArray(std::string const& name) const;
148 
155  bool isPropertySetPtr(std::string const& name) const;
156 
163  size_t valueCount(std::string const& name) const;
164 
174  std::type_info const& typeOf(std::string const& name) const;
175 
179  // Implemented in the .cc file to work around symbol visiblity issues on macOS
180  // e.g. https://github.com/pybind/pybind11/issues/1503
181  template <typename T>
182  static std::type_info const& typeOfT();
183 
184  // The following throw an exception if the type does not match exactly.
185 
197  template <typename T>
198  T get(std::string const& name) const;
199 
212  template <typename T>
213  T get(std::string const& name, T const& defaultValue) const;
214 
226  template <typename T>
227  std::vector<T> getArray(std::string const& name) const;
228 
229  // The following throw an exception if the conversion is inappropriate.
230 
241  bool getAsBool(std::string const& name) const;
242 
252  int getAsInt(std::string const& name) const;
253 
265  int64_t getAsInt64(std::string const& name) const;
266 
276  double getAsDouble(std::string const& name) const;
277 
289  std::string getAsString(std::string const& name) const;
290 
299  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
300 
309  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
310 
320  virtual std::string toString(bool topLevelOnly = false, std::string const& indent = "") const;
321 
322  // Modifiers
323 
332  template <typename T>
333  void set(std::string const& name, T const& value);
334 
343  template <typename T>
344  void set(std::string const& name, std::vector<T> const& value);
345 
353  void set(std::string const& name, char const* value);
354 
364  template <typename T>
365  void add(std::string const& name, T const& value);
366 
378  template <typename T>
379  void add(std::string const& name, std::vector<T> const& value);
380 
391  void add(std::string const& name, char const* value);
392 
406  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
407  bool asScalar = false);
408 
422  virtual void combine(ConstPtr source);
423 
430  virtual void remove(std::string const& name);
431 
432 protected:
433  /*
434  * Find the property name (possibly hierarchical) and set or replace its
435  * value with the given vector of values. Hook for subclass overrides of
436  * top-level setting.
437  *
438  * @param[in] name Property name to find, possibly hierarchical.
439  * @param[in] vp shared_ptr to vector of values.
440  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
441  */
442  virtual void _set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
443 
444  /*
445  * Find the property name (possibly hierarchical) and append or set its
446  * value with the given vector of values.
447  *
448  * @param[in] name Property name to find, possibly hierarchical.
449  * @param[in] vp shared_ptr to vector of values.
450  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
451  */
452  virtual void _add(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
453 
454  // Format a value in human-readable form; called by toString
455  virtual std::string _format(std::string const& name) const;
456 
457 private:
458  LSST_PERSIST_FORMATTER(lsst::daf::persistence::PropertySetFormatter)
459 
461 
462  /*
463  * Find the property name (possibly hierarchical).
464  *
465  * @param[in] name Property name to find, possibly hierarchical.
466  * @return unordered_map::iterator to the property or end() if nonexistent.
467  */
468  AnyMap::iterator _find(std::string const& name);
469 
470  /*
471  * Find the property name (possibly hierarchical). Const version.
472  *
473  * @param[in] name Property name to find, possibly hierarchical.
474  * @return unordered_map::const_iterator to the property or end().
475  */
476  AnyMap::const_iterator _find(std::string const& name) const;
477 
478  /*
479  * Find the property name (possibly hierarchical) and set or replace its
480  * value with the given vector of values.
481  *
482  * @param[in] name Property name to find, possibly hierarchical.
483  * @param[in] vp shared_ptr to vector of values.
484  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
485  */
486  virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
487  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
488  void _cycleCheckAnyVec(std::vector<boost::any> const& v, std::string const& name);
489  void _cycleCheckPtr(Ptr const& v, std::string const& name);
490 
491  AnyMap _map;
492  bool _flat;
493 };
494 
495 #if defined(__ICC)
496 #pragma warning(pop)
497 #endif
498 
499 template <>
500 void PropertySet::add<PropertySet::Ptr>(std::string const& name, Ptr const& value);
501 template <>
502 void PropertySet::add<PropertySet::Ptr>(std::string const& name, std::vector<Ptr> const& value);
503 }
504 } // namespace daf
505 } // namespace lsst
506 
507 #endif
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:76
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:77
STL class.
Interface for Persistable base class.
STL class.
Class for storing generic metadata.
Definition: PropertySet.h:73
#define LSST_PERSIST_FORMATTER(formatter...)
Macro used to connect the persistable class with the Formatter and boost::serialization.
Definition: Persistable.h:99
Base class for all persistable classes.
Definition: Persistable.h:75
#define LSST_EXPORT
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55