Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+2945638123,g07dc498a13+7e3c5f68a2,g12483e3c20+e0fa9c8503,g1409bbee79+7e3c5f68a2,g1a7e361dbc+7e3c5f68a2,g1fd858c14a+9f35e23ec3,g35bb328faa+fcb1d3bbc8,g3ad4f90e5c+e0fa9c8503,g3bd4b5ce2c+36fb6ead7f,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g5477a8d5ce+db04660fe6,g60b5630c4e+e0fa9c8503,g75b6c65c88+55830bd904,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8852436030+4639f750a5,g89139ef638+7e3c5f68a2,g8f1d76f86f+11a01f47f4,g9125e01d80+fcb1d3bbc8,g919ac25b3e+28db628500,g95236ca021+f7a31438ed,g989de1cb63+7e3c5f68a2,g9f33ca652e+d1e6496fea,ga365f290ef+26a6f9aaf0,gaaedd4e678+7e3c5f68a2,gabe3b4be73+1e0a283bba,gb1101e3267+c09b629e83,gb4a253aaf5+0122250889,gb58c049af0+f03b321e39,gbaa7868d32+e0fa9c8503,gc99c83e5f0+76d20ab76d,gcf25f946ba+4639f750a5,gd6cbbdb0b4+c8606af20c,gde0f65d7ad+021d0de224,ge278dab8ac+932305ba37,gfba249425e+fcb1d3bbc8,w.2025.08
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
45
46#include <memory>
47#include <string>
48#include <typeinfo>
49#include <unordered_map>
50#include <vector>
51#include <any>
52#include <ostream>
53
54#include "lsst/base.h"
56#include "lsst/pex/exceptions.h"
57
58namespace lsst {
59namespace daf {
60namespace base {
61
62#if defined(__ICC)
63#pragma warning(push)
64#pragma warning(disable : 444)
65#endif
66
68public:
69 // Typedefs
72
78 explicit PropertySet(bool flat = false);
79
81 virtual ~PropertySet() noexcept;
82
83 // No copying
84 PropertySet(const PropertySet&) = delete;
85 PropertySet& operator=(const PropertySet&) = delete;
86
87 // No moving
89 PropertySet& operator=(PropertySet&&) = delete;
90
91 // Accessors
92
98 virtual std::shared_ptr<PropertySet> deepCopy() const;
99
106 size_t nameCount(bool topLevelOnly = true) const;
107
115 std::vector<std::string> names(bool topLevelOnly = true) const;
116
120 std::vector<std::string> paramNames(bool topLevelOnly = true) const;
121
125 std::vector<std::string> propertySetNames(bool topLevelOnly = true) const;
126
133 bool exists(std::string const& name) const;
134
141 bool isArray(std::string const& name) const;
142
149 bool isPropertySetPtr(std::string const& name) const;
150
157 bool isUndefined(std::string const& name) const;
158
165 size_t valueCount() const;
166
173 size_t valueCount(std::string const& name) const;
174
184 std::type_info const& typeOf(std::string const& name) const;
185
189 // Implemented in the .cc file to work around symbol visiblity issues on macOS
190 // e.g. https://github.com/pybind/pybind11/issues/1503
191 template <typename T>
192 static std::type_info const& typeOfT();
193
194 // The following throw an exception if the type does not match exactly.
195
207 template <typename T>
208 T get(std::string const& name) const;
209
222 template <typename T>
223 T get(std::string const& name, T const& defaultValue) const;
224
236 template <typename T>
237 std::vector<T> getArray(std::string const& name) const;
238
239 // The following throw an exception if the conversion is inappropriate.
240
251 bool getAsBool(std::string const& name) const;
252
262 int getAsInt(std::string const& name) const;
263
275 int64_t getAsInt64(std::string const& name) const;
276
286 uint64_t getAsUInt64(std::string const& name) const;
287
297 double getAsDouble(std::string const& name) const;
298
310 std::string getAsString(std::string const& name) const;
311
320 std::shared_ptr<PropertySet> getAsPropertySetPtr(std::string const& name) const;
321
330 Persistable::Ptr getAsPersistablePtr(std::string const& name) const;
331
341 virtual std::string toString(bool topLevelOnly = false, std::string const& indent = "") const;
342
343 // Modifiers
344
359 template <typename T>
360 void set(std::string const& name, T const& value);
361
370 template <typename T>
371 void set(std::string const& name, std::vector<T> const& value);
372
380 void set(std::string const& name, char const* value);
381
397 template <typename T>
398 void add(std::string const& name, T const& value);
399
411 template <typename T>
412 void add(std::string const& name, std::vector<T> const& value);
413
424 void add(std::string const& name, char const* value);
425
427
440 virtual void copy(std::string const& dest, PropertySet const & source, std::string const& name,
441 bool asScalar = false);
443
445
458 virtual void combine(PropertySet const & source);
460
467 virtual void remove(std::string const& name);
468
469protected:
470 /*
471 * Find the property name (possibly hierarchical) and set or replace its
472 * value with the given vector of values. Hook for subclass overrides of
473 * top-level setting.
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 _set(std::string const& name, std::shared_ptr<std::vector<std::any> > vp);
480
481 /*
482 * Find the property name (possibly hierarchical) and append or set its
483 * value with the given vector of values.
484 *
485 * @param[in] name Property name to find, possibly hierarchical.
486 * @param[in] vp shared_ptr to vector of values.
487 * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
488 */
489 virtual void _add(std::string const& name, std::shared_ptr<std::vector<std::any> > vp);
490
491 // Format a value in human-readable form; called by toString
492 virtual std::string _format(std::string const& name) const;
493
494private:
495
496 typedef std::unordered_map<std::string, std::shared_ptr<std::vector<std::any> > > AnyMap;
497
498 /*
499 * Find the property name (possibly hierarchical).
500 *
501 * @param[in] name Property name to find, possibly hierarchical.
502 * @return unordered_map::iterator to the property or end() if nonexistent.
503 */
504 AnyMap::iterator _find(std::string const& name);
505
506 /*
507 * Find the property name (possibly hierarchical). Const version.
508 *
509 * @param[in] name Property name to find, possibly hierarchical.
510 * @return unordered_map::const_iterator to the property or end().
511 */
512 AnyMap::const_iterator _find(std::string const& name) const;
513
514 /*
515 * Find the property name (possibly hierarchical) and set or replace its
516 * value with the given vector of values.
517 *
518 * @param[in] name Property name to find, possibly hierarchical.
519 * @param[in] vp shared_ptr to vector of values.
520 * @throws InvalidParameterError Hierarchical name uses non-PropertySet.
521 */
522 virtual void _findOrInsert(std::string const& name, std::shared_ptr<std::vector<std::any> > vp);
523 void _cycleCheckPtrVec(std::vector<std::shared_ptr<PropertySet>> const& v, std::string const& name);
524 void _cycleCheckAnyVec(std::vector<std::any> const& v, std::string const& name);
525 void _cycleCheckPtr(std::shared_ptr<PropertySet> const & v, std::string const& name);
526
527 AnyMap _map;
528 bool _flat;
529};
530
531
532std::ostream &operator<<(std::ostream &os, PropertySet const &propertySet);
533
534
535#if defined(__ICC)
536#pragma warning(pop)
537#endif
538
539template <>
541 std::string const& name,
543);
544
545template <>
547 std::string const& name,
549);
550
551}
552} // namespace daf
553} // namespace lsst
554
555#endif
Basic LSST definitions.
#define LSST_EXPORT
Make a symbol visible even if visiblity is hidden (e.g.
Definition base.h:37
Base class for all persistable classes.
Definition Persistable.h:75
Class for storing generic metadata.
Definition PropertySet.h:67
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
virtual void _set(std::string const &name, std::shared_ptr< std::vector< std::any > > vp)
size_t nameCount(bool topLevelOnly=true) const
Get the number of names in the PropertySet, optionally including those in subproperties.
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
std::shared_ptr< PropertySet > Ptr
Definition PropertySet.h:70
bool isArray(std::string const &name) const
Determine if a name (possibly hierarchical) has multiple values.
int64_t getAsInt64(std::string const &name) const
Get the last value for a bool/char/short/int/int64_t property name (possibly hierarchical).
virtual std::string _format(std::string const &name) const
bool isUndefined(std::string const &name) const
Determine if a name (possibly hierarchical) has a defined value.
virtual void _findOrInsert(std::string const &name, std::shared_ptr< std::vector< std::any > > vp)
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
std::vector< std::string > names(bool topLevelOnly=true) const
Get the names in the PropertySet, optionally including those in subproperties.
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
std::shared_ptr< PropertySet const > ConstPtr
Definition PropertySet.h:71
size_t valueCount() const
Get the number of values in the entire PropertySet, counting each element of a vector.
bool isPropertySetPtr(std::string const &name) const
Determine if a name (possibly hierarchical) is a subproperty.
PropertySet(bool flat=false)
Construct an empty PropertySet.
virtual void copy(std::string const &dest, PropertySet const &source, std::string const &name, bool asScalar=false)
Replace a single value vector in the destination with one from the source.
virtual void combine(PropertySet const &source)
Append all value vectors from the source to their corresponding properties.
std::shared_ptr< PropertySet > getAsPropertySetPtr(std::string const &name) const
Get the last value for a subproperty name (possibly hierarchical).
std::type_info const & typeOf(std::string const &name) const
Get the type of values for a property name (possibly hierarchical).
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
static std::type_info const & typeOfT()
Get type info for the specified class.
virtual ~PropertySet() noexcept
Destructor.
Persistable::Ptr getAsPersistablePtr(std::string const &name) const
Get the last value for a Persistable name (possibly hierarchical).
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
virtual void _add(std::string const &name, std::shared_ptr< std::vector< std::any > > vp)
uint64_t getAsUInt64(std::string const &name) const
Get the last value for an bool/char/short/int/int64_t property name (possibly hierarchical).
void add(std::string const &name, T const &value)
Append a single value to the vector of values for a property name (possibly hierarchical).
virtual std::shared_ptr< PropertySet > deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
std::vector< std::string > propertySetNames(bool topLevelOnly=true) const
A variant of names that only returns the names of subproperties.
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
bool getAsBool(std::string const &name) const
Get the last value for a bool property name (possibly hierarchical).
Interface for Persistable base class.
STL namespace.