lsst.daf.base g5c4744a4d9+2f20519ee0
PropertyList.cc
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
26
27#include <algorithm>
28#include <iomanip>
29#include <sstream>
30#include <stdexcept>
31#include <any>
32
34
35namespace lsst {
36namespace daf {
37namespace base {
38
42
45PropertyList::~PropertyList() noexcept = default;
46
48// Accessors
50
51std::shared_ptr<PropertySet> PropertyList::deepCopy() const {
53 n->PropertySet::combine(*this->PropertySet::deepCopy());
54 n->_order = _order;
55 n->_comments = _comments;
56 return n;
57}
58
59// The following throw an exception if the type does not match exactly.
60
61template <typename T>
63 const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
64 return PropertySet::get<T>(name);
65}
66
67template <typename T>
68T PropertyList::get(std::string const& name, T const& defaultValue)
69 const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
70 return PropertySet::get<T>(name, defaultValue);
71}
72
73template <typename T>
75 return PropertySet::getArray<T>(name);
76}
77
79 return _comments.find(name)->second;
80}
81
84 for (auto const& name : _order) {
85 v.push_back(name);
86 }
87 return v;
88}
89
91
93
94std::string PropertyList::toString(bool topLevelOnly, std::string const& indent) const {
96 for (auto const& name : _order) {
97 s << _format(name);
98 std::string const& comment = _comments.find(name)->second;
99 if (comment.size()) {
100 s << "// " << comment << std::endl;
101 }
102 }
103 return s.str();
104}
105
107// Modifiers
109
111// Normal versions of set/add with placement control
112
113template <typename T>
114void PropertyList::set(std::string const& name, T const& value) {
115 PropertySet::set(name, value);
116}
117
119 auto pl = std::dynamic_pointer_cast<PropertyList, PropertySet>(value);
120 PropertySet::set(name, value);
121 _comments.erase(name);
122 _order.remove(name);
123 std::vector<std::string> paramNames = value->paramNames(false);
124 if (pl) {
125 for (auto const& paramName : paramNames) {
126 _commentOrderFix(name + "." + paramName, pl->getComment(paramName));
127 }
128 }
129}
130
131void PropertyList::set(std::string const& name, char const* value) { set(name, std::string(value)); }
132
133template <typename T>
134void PropertyList::set(std::string const& name, std::vector<T> const& value) {
135 PropertySet::set(name, value);
136}
137
138template <typename T>
139void PropertyList::add(std::string const& name, T const& value) {
140 PropertySet::add(name, value);
141}
142
143void PropertyList::add(std::string const& name, char const* value) { add(name, std::string(value)); }
144
145template <typename T>
146void PropertyList::add(std::string const& name, std::vector<T> const& value) {
147 PropertySet::add(name, value);
148}
149
151// Commented versions of set/add
152
153template <typename T>
154void PropertyList::set(std::string const& name, T const& value, std::string const& comment) {
155 PropertySet::set(name, value);
156 _commentOrderFix(name, comment);
157}
158
159void PropertyList::set(std::string const& name, char const* value, std::string const& comment) {
160 set(name, std::string(value), comment);
161}
162
163template <typename T>
164void PropertyList::set(std::string const& name, std::vector<T> const& value, std::string const& comment) {
165 PropertySet::set(name, value);
166 _commentOrderFix(name, comment);
167}
168template <typename T>
169void PropertyList::add(std::string const& name, T const& value, std::string const& comment) {
170 PropertySet::add(name, value);
171 _commentOrderFix(name, comment);
172}
173
174void PropertyList::add(std::string const& name, char const* value, std::string const& comment) {
175 add(name, std::string(value), comment);
176}
177
178template <typename T>
179void PropertyList::add(std::string const& name, std::vector<T> const& value, std::string const& comment) {
180 PropertySet::add(name, value);
181 _commentOrderFix(name, comment);
182}
183
185// Other modifiers
186
187void PropertyList::copy(std::string const& dest, PropertySet const & source, std::string const& name,
188 bool asScalar) {
189 PropertySet::copy(dest, source, name, asScalar);
190 auto const * pl = dynamic_cast<PropertyList const *>(&source);
191 if (pl) {
192 _comments[name] = pl->_comments.find(name)->second;
193 }
194}
195
197 std::string const& name, bool asScalar) {
198 if (source) {
199 copy(dest, *source, name, asScalar);
200 }
201}
202
203void PropertyList::combine(PropertySet const & source) {
204 auto const * pl = dynamic_cast<PropertyList const *>(&source);
205 std::list<std::string> newOrder;
206 if (pl) {
207 newOrder = _order;
208 for (auto const& name : *pl) {
209 bool present = _comments.find(name) != _comments.end();
210 if (!present) {
211 newOrder.push_back(name);
212 }
213 }
214 }
215 PropertySet::combine(source);
216 if (pl) {
217 _order = newOrder;
218 for (auto const& name : *pl) {
219 _comments[name] = pl->_comments.find(name)->second;
220 }
221 }
222}
223
225 if (!source) {
226 return;
227 }
228 combine(*source);
229}
230
233 _comments.erase(name);
234 _order.remove(name);
235}
236
238// Private member functions
240
242 PropertySet::_set(name, vp);
243 if (_comments.find(name) == _comments.end()) {
244 _comments.insert(std::make_pair(name, std::string()));
245 _order.push_back(name);
246 }
247}
248
250 _order.remove(name);
251 _order.push_back(name);
252}
253
254void PropertyList::_commentOrderFix(std::string const& name, std::string const& comment) {
255 _comments[name] = comment;
256}
257
259// Explicit template instantiations
261
263// Explicit template instantiations are not well understood by doxygen.
264
265#define INSTANTIATE(t) \
266 template t PropertyList::get<t>(std::string const& name) const; \
267 template t PropertyList::get<t>(std::string const& name, t const& defaultValue) const; \
268 template std::vector<t> PropertyList::getArray<t>(std::string const& name) const; \
269 template void PropertyList::set<t>(std::string const& name, t const& value); \
270 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value); \
271 template void PropertyList::add<t>(std::string const& name, t const& value); \
272 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value); \
273 template void PropertyList::set<t>(std::string const& name, t const& value, std::string const& comment); \
274 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
275 std::string const& comment); \
276 template void PropertyList::add<t>(std::string const& name, t const& value, std::string const& comment); \
277 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
278 std::string const& comment); \
279 template void PropertyList::set<t>(std::string const& name, t const& value, char const* comment); \
280 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
281 char const* comment); \
282 template void PropertyList::add<t>(std::string const& name, t const& value, char const* comment); \
283 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
284 char const* comment);
285
286INSTANTIATE(bool)
287INSTANTIATE(char)
288INSTANTIATE(signed char)
289INSTANTIATE(unsigned char)
290INSTANTIATE(short)
291INSTANTIATE(unsigned short)
292INSTANTIATE(int)
293INSTANTIATE(unsigned int)
294INSTANTIATE(long)
295INSTANTIATE(unsigned long)
296INSTANTIATE(long long)
297INSTANTIATE(unsigned long long)
298INSTANTIATE(float)
299INSTANTIATE(double)
300INSTANTIATE(std::nullptr_t)
301INSTANTIATE(std::string)
302INSTANTIATE(Persistable::Ptr)
303INSTANTIATE(DateTime)
304
305
306
307} // namespace base
308} // namespace daf
309} // namespace lsst
Interface for DateTime class.
T begin(T... args)
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:64
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
std::list< std::string >::const_iterator end() const
End iterator over the list of property names, in the order they were added.
Definition: PropertyList.cc:92
PropertyList()
Construct an empty PropertyList.
Definition: PropertyList.cc:41
virtual void combine(PropertySet const &source)
Append all value vectors from the source to their corresponding properties.
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
Definition: PropertyList.cc:62
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 void _set(std::string const &name, std::shared_ptr< std::vector< std::any > > vp)
std::string const & getComment(std::string const &name) const
Get the comment for a string property name (possibly hierarchical).
Definition: PropertyList.cc:78
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
Definition: PropertyList.cc:74
std::vector< std::string > getOrderedNames() const
Get the list of property names, in the order they were added.
Definition: PropertyList.cc:82
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
virtual ~PropertyList() noexcept
Destructor.
virtual void _moveToEnd(std::string const &name)
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 _commentOrderFix(std::string const &name, std::string const &comment)
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
Definition: PropertyList.cc:94
std::list< std::string >::const_iterator begin() const
Begin iterator over the list of property names, in the order they were added.
Definition: PropertyList.cc:90
Class for storing generic metadata.
Definition: PropertySet.h:66
virtual void _set(std::string const &name, std::shared_ptr< std::vector< std::any > > vp)
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
virtual std::string _format(std::string const &name) const
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
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.
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 > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
T end(T... args)
T endl(T... args)
T erase(T... args)
T find(T... args)
T insert(T... args)
T make_pair(T... args)
STL namespace.
T push_back(T... args)
T remove(T... args)
T size(T... args)
T str(T... args)