LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 
55 #include <lsst/tr1/unordered_map.h>
56 #include <string>
57 #include <typeinfo>
58 #include <vector>
59 
60 #include "boost/any.hpp"
61 #include "boost/noncopyable.hpp"
62 #include "boost/shared_ptr.hpp"
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 :
83  public Persistable, public Citizen
84 #ifndef SWIG
85  , public boost::noncopyable
86 #endif
87  {
88 public:
89 // Typedefs
90  typedef boost::shared_ptr<PropertySet> Ptr;
91  typedef boost::shared_ptr<PropertySet const> ConstPtr;
92 
93 // Constructors
94  explicit PropertySet(bool flat=false);
95  virtual ~PropertySet(void);
96 
97 // Accessors
98  virtual Ptr deepCopy(void) const;
99  // Returns a PropertySet::Ptr pointing to a new deep copy.
100 
101  size_t nameCount(bool topLevelOnly = true) const;
102  std::vector<std::string> names(bool topLevelOnly = true) const;
103  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
104  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
105 
106  bool exists(std::string const& name) const;
107  bool isArray(std::string const& name) const;
108  bool isPropertySetPtr(std::string const& name) const;
109 
110  size_t valueCount(std::string const& name) const;
111  std::type_info const& typeOf(std::string const& name) const;
112  // This returns typeof(vector::value_type), not the type of the value
113  // vector itself.
114 
115  // The following throw an exception if the type does not match exactly.
116  template <typename T> T get(std::string const& name) const;
117  // Note that the type must be explicitly specified for this template:
118  // int i = propertySet.get<int>("foo");
119  template <typename T>
120  T get(std::string const& name, T const& defaultValue) const;
121  // Returns the provided default value if the name does not exist.
122  template <typename T>
123  std::vector<T> getArray(std::string const& name) const;
124 
125  // The following throw an exception if the conversion is inappropriate.
126  bool getAsBool(std::string const& name) const; // for bools only
127  int getAsInt(std::string const& name) const; // bool/char/short/int
128  int64_t getAsInt64(std::string const& name) const; // above + int64_t
129  double getAsDouble(std::string const& name) const; // + float, double
130  std::string getAsString(std::string const& name) const; // for strings only
131  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
132  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
133 
134  // Use this for debugging, not for serialization/persistence.
135  virtual std::string toString(bool topLevelOnly = false,
136  std::string const& indent = "") const;
137 
138 // Modifiers
139  template <typename T> void set(std::string const& name, T const& value);
140  template <typename T> void set(std::string const& name,
141  std::vector<T> const& value);
142  void set(std::string const& name, char const* value);
143  template <typename T> void add(std::string const& name, T const& value);
144  template <typename T> void add(std::string const& name,
145  std::vector<T> const& value);
146  void add(std::string const& name, char const* value);
147 
148  virtual void copy(std::string const& dest, ConstPtr source,
149  std::string const& name);
150  virtual void combine(ConstPtr source);
151  // All vectors from the source are add()ed to the destination with the
152  // same names. Types must match.
153 
154  virtual void remove(std::string const& name);
155 
156 protected:
157  virtual void _set(std::string const& name,
158  boost::shared_ptr< std::vector<boost::any> > vp);
159  virtual void _add(std::string const& name,
160  boost::shared_ptr< std::vector<boost::any> > vp);
161  virtual std::string _format(std::string const& name) const;
162 
163 private:
165 
166  typedef std::tr1::unordered_map<std::string,
167  boost::shared_ptr< std::vector<boost::any> > > AnyMap;
168 
169  AnyMap::iterator _find(std::string const& name);
170  AnyMap::const_iterator _find(std::string const& name) const;
171  virtual void _findOrInsert(std::string const& name,
172  boost::shared_ptr< std::vector<boost::any> > vp);
173  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
174  void _cycleCheckAnyVec(std::vector<boost::any> const& v,
175  std::string const& name);
176  void _cycleCheckPtr(Ptr const& v, std::string const& name);
177 
178  AnyMap _map;
179  bool _flat;
180 };
181 
182 #if defined(__ICC)
183 #pragma warning (pop)
184 #endif
185 
186 template<> void PropertySet::add<PropertySet::Ptr>(
187  std::string const& name, Ptr const& value);
188 template<> void PropertySet::add<PropertySet::Ptr>(
189  std::string const& name, std::vector<Ptr> const& value);
190 
191 }}} // namespace lsst::daf::base
192 
193 #endif
bool getAsBool(std::string const &name) const
Definition: PropertySet.cc:321
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:142
void _cycleCheckPtr(Ptr const &v, std::string const &name)
Definition: PropertySet.cc:936
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual void _add(std::string const &name, boost::shared_ptr< std::vector< boost::any > > vp)
Definition: PropertySet.cc:848
int64_t getAsInt64(std::string const &name) const
Definition: PropertySet.cc:372
Persistable::Ptr getAsPersistablePtr(std::string const &name) const
Definition: PropertySet.cc:466
bool isPropertySetPtr(std::string const &name) const
Definition: PropertySet.cc:207
size_t nameCount(bool topLevelOnly=true) const
Definition: PropertySet.cc:97
std::vector< std::string > propertySetNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:168
Include files required for standard LSST Exception handling.
virtual void copy(std::string const &dest, ConstPtr source, std::string const &name)
Definition: PropertySet.cc:713
void _cycleCheckPtrVec(std::vector< Ptr > const &v, std::string const &name)
Definition: PropertySet.cc:922
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
boost::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:91
PropertySet(bool flat=false)
Definition: PropertySet.cc:55
void set(std::string const &name, T const &value)
Definition: PropertySet.cc:581
std::vector< std::string > names(bool topLevelOnly=true) const
Definition: PropertySet.cc:117
size_t valueCount(std::string const &name) const
Definition: PropertySet.cc:216
bool isArray(std::string const &name) const
Definition: PropertySet.cc:198
std::tr1::unordered_map< std::string, boost::shared_ptr< std::vector< boost::any > > > AnyMap
Definition: PropertySet.h:167
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
int getAsInt(std::string const &name) const
Definition: PropertySet.cc:333
virtual void combine(ConstPtr source)
Definition: PropertySet.cc:738
boost::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
virtual std::string _format(std::string const &name) const
Definition: PropertySet.cc:507
PropertySet::Ptr getAsPropertySetPtr(std::string const &name) const
Definition: PropertySet.cc:455
#define LSST_PERSIST_FORMATTER(formatter...)
Definition: Persistable.h:98
Formatter for persistence of PropertySet instances.
virtual void _findOrInsert(std::string const &name, boost::shared_ptr< std::vector< boost::any > > vp)
Definition: PropertySet.cc:874
Interface for Persistable base class.
std::type_info const & typeOf(std::string const &name) const
Definition: PropertySet.cc:227
double getAsDouble(std::string const &name) const
Definition: PropertySet.cc:406
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Definition: PropertySet.cc:70
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Definition: PropertySet.cc:476
std::string getAsString(std::string const &name) const
Definition: PropertySet.cc:444
virtual void _set(std::string const &name, boost::shared_ptr< std::vector< boost::any > > vp)
Definition: PropertySet.cc:837
Base class for all persistable classes.
Definition: Persistable.h:74
void _cycleCheckAnyVec(std::vector< boost::any > const &v, std::string const &name)
Definition: PropertySet.cc:929
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
AnyMap::iterator _find(std::string const &name)
Definition: PropertySet.cc:781
void add(std::string const &name, T const &value)
Definition: PropertySet.cc:619
std::vector< T > getArray(std::string const &name) const
bool exists(std::string const &name) const
Definition: PropertySet.cc:190