lsst.daf.base  16.0-4-g50d071e+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/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 persistence
64 
65 namespace base {
66 
67 #if defined(__ICC)
68 #pragma warning(push)
69 #pragma warning(disable : 444)
70 #endif
71 
72 class PropertySet : public Persistable, public Citizen {
73 public:
74  // Typedefs
77 
83  explicit PropertySet(bool flat = false);
84 
86  virtual ~PropertySet(void);
87 
88  // No copying
89  PropertySet(const PropertySet&) = delete;
90  PropertySet& operator=(const PropertySet&) = delete;
91 
92  // No moving
93  PropertySet(PropertySet&&) = delete;
94  PropertySet& operator=(PropertySet&&) = delete;
95 
96  // Accessors
97 
103  virtual Ptr deepCopy(void) const;
104 
111  size_t nameCount(bool topLevelOnly = true) const;
112 
120  std::vector<std::string> names(bool topLevelOnly = true) const;
121 
125  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
126 
130  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
131 
138  bool exists(std::string const& name) const;
139 
146  bool isArray(std::string const& name) const;
147 
154  bool isPropertySetPtr(std::string const& name) const;
155 
162  size_t valueCount(std::string const& name) const;
163 
173  std::type_info const& typeOf(std::string const& name) const;
174 
175  // The following throw an exception if the type does not match exactly.
176 
188  template <typename T>
189  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, std::string const& indent = "") const;
312 
313  // Modifiers
314 
323  template <typename T>
324  void set(std::string const& name, T const& value);
325 
334  template <typename T>
335  void set(std::string const& name, std::vector<T> const& value);
336 
344  void set(std::string const& name, char const* value);
345 
355  template <typename T>
356  void add(std::string const& name, T const& value);
357 
369  template <typename T>
370  void add(std::string const& name, std::vector<T> const& value);
371 
382  void add(std::string const& name, char const* value);
383 
397  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
398  bool asScalar = false);
399 
413  virtual void combine(ConstPtr source);
414 
421  virtual void remove(std::string const& name);
422 
423 protected:
424  /*
425  * Find the property name (possibly hierarchical) and set or replace its
426  * value with the given vector of values. Hook for subclass overrides of
427  * top-level setting.
428  *
429  * @param[in] name Property name to find, possibly hierarchical.
430  * @param[in] vp shared_ptr to vector of values.
431  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
432  */
433  virtual void _set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
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, std::shared_ptr<std::vector<boost::any> > vp);
444 
445  // Format a value in human-readable form; called by toString
446  virtual std::string _format(std::string const& name) const;
447 
448 private:
449  LSST_PERSIST_FORMATTER(lsst::daf::persistence::PropertySetFormatter)
450 
452 
453  /*
454  * Find the property name (possibly hierarchical).
455  *
456  * @param[in] name Property name to find, possibly hierarchical.
457  * @return unordered_map::iterator to the property or end() if nonexistent.
458  */
459  AnyMap::iterator _find(std::string const& name);
460 
461  /*
462  * Find the property name (possibly hierarchical). Const version.
463  *
464  * @param[in] name Property name to find, possibly hierarchical.
465  * @return unordered_map::const_iterator to the property or end().
466  */
467  AnyMap::const_iterator _find(std::string const& name) const;
468 
469  /*
470  * Find the property name (possibly hierarchical) and set or replace its
471  * value with the given vector of values.
472  *
473  * @param[in] name Property name to find, possibly hierarchical.
474  * @param[in] vp shared_ptr to vector of values.
475  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
476  */
477  virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
478  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
479  void _cycleCheckAnyVec(std::vector<boost::any> const& v, std::string const& name);
480  void _cycleCheckPtr(Ptr const& v, std::string const& name);
481 
482  AnyMap _map;
483  bool _flat;
484 };
485 
486 #if defined(__ICC)
487 #pragma warning(pop)
488 #endif
489 
490 template <>
491 void PropertySet::add<PropertySet::Ptr>(std::string const& name, Ptr const& value);
492 template <>
493 void PropertySet::add<PropertySet::Ptr>(std::string const& name, std::vector<Ptr> const& value);
494 }
495 } // namespace daf
496 } // namespace lsst
497 
498 #endif
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:75
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:76
STL class.
Interface for Persistable base class.
STL class.
Class for storing generic metadata.
Definition: PropertySet.h:72
#define LSST_PERSIST_FORMATTER(formatter...)
Macro used to connect the persistable class with the Formatter and boost::serialization.
Definition: Persistable.h:97
Base class for all persistable classes.
Definition: Persistable.h:73
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53