LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 
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 namespace base {
62 
63 #if defined(__ICC)
64 #pragma warning(push)
65 #pragma warning(disable : 444)
66 #endif
67 
69 public:
70  // Typedefs
73 
79  explicit PropertySet(bool flat = false);
80 
82  virtual ~PropertySet() noexcept;
83 
84  // No copying
85  PropertySet(const PropertySet&) = delete;
86  PropertySet& operator=(const PropertySet&) = delete;
87 
88  // No moving
89  PropertySet(PropertySet&&) = delete;
90  PropertySet& operator=(PropertySet&&) = delete;
91 
92  // Accessors
93 
99  virtual Ptr deepCopy() const;
100 
107  size_t nameCount(bool topLevelOnly = true) const;
108 
116  std::vector<std::string> names(bool topLevelOnly = true) const;
117 
121  std::vector<std::string> paramNames(bool topLevelOnly = true) const;
122 
126  std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
127 
134  bool exists(std::string const& name) const;
135 
142  bool isArray(std::string const& name) const;
143 
150  bool isPropertySetPtr(std::string const& name) const;
151 
158  size_t valueCount(std::string const& name) const;
159 
169  std::type_info const& typeOf(std::string const& name) const;
170 
174  // Implemented in the .cc file to work around symbol visiblity issues on macOS
175  // e.g. https://github.com/pybind/pybind11/issues/1503
176  template <typename T>
177  static std::type_info const& typeOfT();
178 
179  // The following throw an exception if the type does not match exactly.
180 
192  template <typename T>
193  T get(std::string const& name) const;
194 
207  template <typename T>
208  T get(std::string const& name, T const& defaultValue) const;
209 
221  template <typename T>
222  std::vector<T> getArray(std::string const& name) const;
223 
224  // The following throw an exception if the conversion is inappropriate.
225 
236  bool getAsBool(std::string const& name) const;
237 
247  int getAsInt(std::string const& name) const;
248 
260  int64_t getAsInt64(std::string const& name) const;
261 
271  double getAsDouble(std::string const& name) const;
272 
284  std::string getAsString(std::string const& name) const;
285 
294  PropertySet::Ptr getAsPropertySetPtr(std::string const& name) const;
295 
304  Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
305 
315  virtual std::string toString(bool topLevelOnly = false, std::string const& indent = "") const;
316 
317  // Modifiers
318 
327  template <typename T>
328  void set(std::string const& name, T const& value);
329 
338  template <typename T>
339  void set(std::string const& name, std::vector<T> const& value);
340 
348  void set(std::string const& name, char const* value);
349 
359  template <typename T>
360  void add(std::string const& name, T const& value);
361 
373  template <typename T>
374  void add(std::string const& name, std::vector<T> const& value);
375 
386  void add(std::string const& name, char const* value);
387 
401  virtual void copy(std::string const& dest, ConstPtr source, std::string const& name,
402  bool asScalar = false);
403 
417  virtual void combine(ConstPtr source);
418 
425  virtual void remove(std::string const& name);
426 
427 protected:
428  /*
429  * Find the property name (possibly hierarchical) and set or replace its
430  * value with the given vector of values. Hook for subclass overrides of
431  * top-level setting.
432  *
433  * @param[in] name Property name to find, possibly hierarchical.
434  * @param[in] vp shared_ptr to vector of values.
435  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
436  */
437  virtual void _set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
438 
439  /*
440  * Find the property name (possibly hierarchical) and append or set its
441  * value with the given vector of values.
442  *
443  * @param[in] name Property name to find, possibly hierarchical.
444  * @param[in] vp shared_ptr to vector of values.
445  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
446  */
447  virtual void _add(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
448 
449  // Format a value in human-readable form; called by toString
450  virtual std::string _format(std::string const& name) const;
451 
452 private:
453 
454  typedef std::unordered_map<std::string, std::shared_ptr<std::vector<boost::any> > > AnyMap;
455 
456  /*
457  * Find the property name (possibly hierarchical).
458  *
459  * @param[in] name Property name to find, possibly hierarchical.
460  * @return unordered_map::iterator to the property or end() if nonexistent.
461  */
462  AnyMap::iterator _find(std::string const& name);
463 
464  /*
465  * Find the property name (possibly hierarchical). Const version.
466  *
467  * @param[in] name Property name to find, possibly hierarchical.
468  * @return unordered_map::const_iterator to the property or end().
469  */
470  AnyMap::const_iterator _find(std::string const& name) const;
471 
472  /*
473  * Find the property name (possibly hierarchical) and set or replace its
474  * value with the given vector of values.
475  *
476  * @param[in] name Property name to find, possibly hierarchical.
477  * @param[in] vp shared_ptr to vector of values.
478  * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
479  */
480  virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp);
481  void _cycleCheckPtrVec(std::vector<Ptr> const& v, std::string const& name);
482  void _cycleCheckAnyVec(std::vector<boost::any> const& v, std::string const& name);
483  void _cycleCheckPtr(Ptr const& v, std::string const& name);
484 
485  AnyMap _map;
486  bool _flat;
487 };
488 
489 #if defined(__ICC)
490 #pragma warning(pop)
491 #endif
492 
493 template <>
494 void PropertySet::add<PropertySet::Ptr>(std::string const& name, Ptr const& value);
495 template <>
496 void PropertySet::add<PropertySet::Ptr>(std::string const& name, std::vector<Ptr> const& value);
497 }
498 } // namespace daf
499 } // namespace lsst
500 
501 #endif
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:71
Definition: Polygon.cc:25
STL namespace.
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:72
daf::base::PropertySet * set
Definition: fits.cc:832
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
Interface for Persistable base class.
STL class.
#define LSST_EXPORT
Make a symbol visible even if visiblity is hidden (e.g.
Definition: base.h:54
A base class for image defects.
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
STL class.
Class for storing generic metadata.
Definition: PropertySet.h:68
Base class for all persistable classes.
Definition: Persistable.h:75
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
Basic LSST definitions.