lsst.daf.base  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
55 #include <memory>
56 #include <string>
57 #include <typeinfo>
58 #include <unordered_map>
59 #include <vector>
60 
61 #include "boost/any.hpp"
62 
63 #include "lsst/daf/base/Citizen.h"
65 #include "lsst/pex/exceptions.h"
66 
67 namespace lsst {
68 namespace daf {
69 
70 namespace persistence {
71  class PropertySetFormatter;
72 } // namespace lsst::daf::persistence
73 
74 
75 namespace base {
76 
77 #if defined(__ICC)
78 #pragma warning (push)
79 #pragma warning (disable: 444)
80 #endif
81 
82 class PropertySet : public Persistable, public Citizen {
83 public:
84 // Typedefs
85  typedef std::shared_ptr<PropertySet> Ptr;
86  typedef std::shared_ptr<PropertySet const> ConstPtr;
87 
88 // Constructors
89  explicit PropertySet(bool flat=false);
90  virtual ~PropertySet(void);
91 
92 #ifndef SWIG
93  // No copying
94  PropertySet (const PropertySet&) = delete;
95  PropertySet& operator=(const PropertySet&) = delete;
96 
97  // No moving
98  PropertySet (PropertySet&&) = delete;
99  PropertySet& operator=(PropertySet&&) = delete;
100 #endif
101 // Accessors
102  virtual Ptr deepCopy(void) const;
103  // Returns a PropertySet::Ptr pointing to a new deep copy.
104 
105  size_t nameCount(bool topLevelOnly = true) const;
106  std::vector<std::string> names(bool topLevelOnly = true) const;
107  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
108  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
109 
110  bool exists(std::string const& name) const;
111  bool isArray(std::string const& name) const;
112  bool isPropertySetPtr(std::string const& name) const;
113 
114  size_t valueCount(std::string const& name) const;
115  std::type_info const& typeOf(std::string const& name) const;
116  // This returns typeof(vector::value_type), not the type of the value
117  // vector itself.
118 
119  // The following throw an exception if the type does not match exactly.
120  template <typename T> T get(std::string const& name) const;
121  // Note that the type must be explicitly specified for this template:
122  // int i = propertySet.get<int>("foo");
123  template <typename T>
124  T get(std::string const& name, T const& defaultValue) const;
125  // Returns the provided default value if the name does not exist.
126  template <typename T>
127  std::vector<T> getArray(std::string const& name) const;
128 
129  // The following throw an exception if the conversion is inappropriate.
130  bool getAsBool(std::string const& name) const; // for bools only
131  int getAsInt(std::string const& name) const; // bool/char/short/int
132  int64_t getAsInt64(std::string const& name) const; // above + int64_t
133  double getAsDouble(std::string const& name) const; // + float, double
134  std::string getAsString(std::string const& name) const; // for strings only
135  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
136  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
137 
138  // Use this for debugging, not for serialization/persistence.
139  virtual std::string toString(bool topLevelOnly = false,
140  std::string const& indent = "") const;
141 
142 // Modifiers
143  template <typename T> void set(std::string const& name, T const& value);
144  template <typename T> void set(std::string const& name,
145  std::vector<T> const& value);
146  void set(std::string const& name, char const* value);
147  template <typename T> void add(std::string const& name, T const& value);
148  template <typename T> void add(std::string const& name,
149  std::vector<T> const& value);
150  void add(std::string const& name, char const* value);
151 
152  virtual void copy(std::string const& dest, ConstPtr source,
153  std::string const& name);
154  virtual void combine(ConstPtr source);
155  // All vectors from the source are add()ed to the destination with the
156  // same names. Types must match.
157 
158  virtual void remove(std::string const& name);
159 
160 protected:
161  virtual void _set(std::string const& name,
162  std::shared_ptr< std::vector<boost::any> > vp);
163  virtual void _add(std::string const& name,
164  std::shared_ptr< std::vector<boost::any> > vp);
165  virtual std::string _format(std::string const& name) const;
166 
167 private:
168  LSST_PERSIST_FORMATTER(lsst::daf::persistence::PropertySetFormatter)
169 
170  typedef std::unordered_map<std::string,
171  std::shared_ptr< std::vector<boost::any> > > AnyMap;
172 
173  AnyMap::iterator _find(std::string const& name);
174  AnyMap::const_iterator _find(std::string const& name) const;
175  virtual void _findOrInsert(std::string const& name,
176  std::shared_ptr< std::vector<boost::any> > vp);
177  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
178  void _cycleCheckAnyVec(std::vector<boost::any> const& v,
179  std::string const& name);
180  void _cycleCheckPtr(Ptr const& v, std::string const& name);
181 
182  AnyMap _map;
183  bool _flat;
184 };
185 
186 #if defined(__ICC)
187 #pragma warning (pop)
188 #endif
189 
190 template<> void PropertySet::add<PropertySet::Ptr>(
191  std::string const& name, Ptr const& value);
192 template<> void PropertySet::add<PropertySet::Ptr>(
193  std::string const& name, std::vector<Ptr> const& value);
194 
195 }}} // namespace lsst::daf::base
196 
197 #endif
bool getAsBool(std::string const &name) const
Get the last value for a bool property name (possibly hierarchical).
Definition: PropertySet.cc:321
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Get the names of parameters (non-subproperties) in the PropertySet, optionally including those in sub...
Definition: PropertySet.cc:142
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
int64_t getAsInt64(std::string const &name) const
Get the last value for a bool/char/short/int/int64_t property name (possibly hierarchical).
Definition: PropertySet.cc:372
Persistable::Ptr getAsPersistablePtr(std::string const &name) const
Get the last value for a Persistable name (possibly hierarchical).
Definition: PropertySet.cc:466
bool isPropertySetPtr(std::string const &name) const
Determine if a name (possibly hierarchical) is a subproperty.
Definition: PropertySet.cc:207
size_t nameCount(bool topLevelOnly=true) const
Get the number of names in the PropertySet, optionally including those in subproperties.
Definition: PropertySet.cc:97
std::vector< std::string > propertySetNames(bool topLevelOnly=true) const
Get the names of subproperties in the PropertySet, optionally including those in subproperties.
Definition: PropertySet.cc:168
virtual void _add(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
Finds the property name (possibly hierarchical) and appends or sets its value with the given vector o...
Definition: PropertySet.cc:848
virtual void copy(std::string const &dest, ConstPtr source, std::string const &name)
Replaces a single value vector in the destination with one from the source.
Definition: PropertySet.cc:713
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:86
PropertySet & operator=(const PropertySet &)=delete
PropertySet(bool flat=false)
Constructor.
Definition: PropertySet.cc:55
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new value.
Definition: PropertySet.cc:581
std::vector< std::string > names(bool topLevelOnly=true) const
Get the names in the PropertySet, optionally including those in subproperties.
Definition: PropertySet.cc:117
size_t valueCount(std::string const &name) const
Get number of values for a property name (possibly hierarchical).
Definition: PropertySet.cc:216
bool isArray(std::string const &name) const
Determine if a name (possibly hierarchical) has multiple values.
Definition: PropertySet.cc:198
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
Definition: PropertySet.cc:333
virtual void combine(ConstPtr source)
Appends all value vectors from the source to their corresponding properties.
Definition: PropertySet.cc:738
Interface for Persistable base class.
virtual std::string _format(std::string const &name) const
Definition: PropertySet.cc:507
PropertySet::Ptr getAsPropertySetPtr(std::string const &name) const
Get the last value for a subproperty name (possibly hierarchical).
Definition: PropertySet.cc:455
virtual void _set(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
Finds the property name (possibly hierarchical) and sets or replaces its value with the given vector ...
Definition: PropertySet.cc:837
std::type_info const & typeOf(std::string const &name) const
Get the type of values for a property name (possibly hierarchical).
Definition: PropertySet.cc:227
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
Definition: PropertySet.cc:406
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Copy the PropertySet and all of its contents.
Definition: PropertySet.cc:70
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
Definition: PropertySet.cc:476
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
Definition: PropertySet.cc:444
#define LSST_PERSIST_FORMATTER(formatter...)
Macro used to connect the persistable class with the Formatter and boost::serialization.
Definition: Persistable.h:98
std::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
virtual ~PropertySet(void)
Destructor.
Definition: PropertySet.cc:60
Base class for all persistable classes.
Definition: Persistable.h:74
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
void add(std::string const &name, T const &value)
Appends a single value to the vector of values for a property name (possibly hierarchical).
Definition: PropertySet.cc:619
std::vector< T > getArray(std::string const &name) const
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
Definition: PropertySet.cc:190