lsst.daf.base  15.0
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/daf/base/Citizen.h"
56 #include "lsst/pex/exceptions.h"
57 
58 namespace lsst {
59 namespace daf {
60 
61 namespace persistence {
62  class PropertySetFormatter;
63 } // namespace lsst::daf::persistence
64 
65 
66 namespace base {
67 
68 #if defined(__ICC)
69 #pragma warning (push)
70 #pragma warning (disable: 444)
71 #endif
72 
73 class 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 
176  // The following throw an exception if the type does not match exactly.
177 
189  template <typename T> T get(std::string const& name) const;
190 
203  template <typename T>
204  T get(std::string const& name, T const& defaultValue) const;
205 
217  template <typename T>
218  std::vector<T> getArray(std::string const& name) const;
219 
220  // The following throw an exception if the conversion is inappropriate.
221 
232  bool getAsBool(std::string const& name) const;
233 
243  int getAsInt(std::string const& name) const;
244 
256  int64_t getAsInt64(std::string const& name) const;
257 
267  double getAsDouble(std::string const& name) const;
268 
280  std::string getAsString(std::string const& name) const;
281 
290  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
291 
300  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
301 
311  virtual std::string toString(bool topLevelOnly = false,
312  std::string const& indent = "") const;
313 
314 // Modifiers
315 
324  template <typename T> void set(std::string const& name, T const& value);
325 
334  template <typename T> void set(std::string const& name,
335  std::vector<T> const& value);
336 
344  void set(std::string const& name, char const* value);
345 
355  template <typename T> void add(std::string const& name, T const& value);
356 
368  template <typename T> void add(std::string const& name,
369  std::vector<T> const& value);
370 
381  void add(std::string const& name, char const* value);
382 
396  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
397  bool asScalar = false);
398 
412  virtual void combine(ConstPtr source);
413 
420  virtual void remove(std::string const& name);
421 
422 protected:
423  /*
424  * Find the property name (possibly hierarchical) and set or replace its
425  * value with the given vector of values. Hook for subclass overrides of
426  * top-level setting.
427  *
428  * @param[in] name Property name to find, possibly hierarchical.
429  * @param[in] vp shared_ptr to vector of values.
430  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
431  */
432  virtual void _set(std::string const& name,
434 
435  /*
436  * Find the property name (possibly hierarchical) and append or set its
437  * value with the given vector of values.
438  *
439  * @param[in] name Property name to find, possibly hierarchical.
440  * @param[in] vp shared_ptr to vector of values.
441  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
442  */
443  virtual void _add(std::string const& name,
445 
446  // Format a value in human-readable form; called by toString
447  virtual std::string _format(std::string const& name) const;
448 
449 private:
450  LSST_PERSIST_FORMATTER(lsst::daf::persistence::PropertySetFormatter)
451 
454 
455  /*
456  * Find the property name (possibly hierarchical).
457  *
458  * @param[in] name Property name to find, possibly hierarchical.
459  * @return unordered_map::iterator to the property or end() if nonexistent.
460  */
461  AnyMap::iterator _find(std::string const& name);
462 
463  /*
464  * Find the property name (possibly hierarchical). Const version.
465  *
466  * @param[in] name Property name to find, possibly hierarchical.
467  * @return unordered_map::const_iterator to the property or end().
468  */
469  AnyMap::const_iterator _find(std::string const& name) const;
470 
471  /*
472  * Find the property name (possibly hierarchical) and set or replace its
473  * value with the given vector of values.
474  *
475  * @param[in] name Property name to find, possibly hierarchical.
476  * @param[in] vp shared_ptr to vector of values.
477  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
478  */
479  virtual void _findOrInsert(std::string const& name,
481  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
482  void _cycleCheckAnyVec(std::vector<boost::any> const& v,
483  std::string const& name);
484  void _cycleCheckPtr(Ptr const& v, std::string const& name);
485 
486  AnyMap _map;
487  bool _flat;
488 };
489 
490 #if defined(__ICC)
491 #pragma warning (pop)
492 #endif
493 
494 template<> void PropertySet::add<PropertySet::Ptr>(
495  std::string const& name, Ptr const& value);
496 template<> void PropertySet::add<PropertySet::Ptr>(
497  std::string const& name, std::vector<Ptr> const& value);
498 
499 }}} // namespace lsst::daf::base
500 
501 #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:98
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