lsst.daf.base g66d7acc217+99b691c7d8
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
51PropertySet::Ptr PropertyList::deepCopy() const {
52 Ptr n(new PropertyList);
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
118void PropertyList::set(std::string const& name, PropertySet::Ptr const& value) {
119 Ptr 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
188 bool asScalar) {
189 PropertySet::copy(dest, source, name, asScalar);
190 ConstPtr pl = std::dynamic_pointer_cast<PropertyList const, PropertySet const>(source);
191 if (pl) {
192 _comments[name] = pl->_comments.find(name)->second;
193 }
194}
195
197 ConstPtr pl = std::dynamic_pointer_cast<PropertyList const, PropertySet const>(source);
198 std::list<std::string> newOrder;
199 if (pl) {
200 newOrder = _order;
201 for (auto const& name : *pl) {
202 bool present = _comments.find(name) != _comments.end();
203 if (!present) {
204 newOrder.push_back(name);
205 }
206 }
207 }
208 PropertySet::combine(source);
209 if (pl) {
210 _order = newOrder;
211 for (auto const& name : *pl) {
212 _comments[name] = pl->_comments.find(name)->second;
213 }
214 }
215}
216
219 _comments.erase(name);
220 _order.remove(name);
221}
222
224// Private member functions
226
228 PropertySet::_set(name, vp);
229 if (_comments.find(name) == _comments.end()) {
230 _comments.insert(std::make_pair(name, std::string()));
231 _order.push_back(name);
232 }
233}
234
236 _order.remove(name);
237 _order.push_back(name);
238}
239
240void PropertyList::_commentOrderFix(std::string const& name, std::string const& comment) {
241 _comments[name] = comment;
242}
243
245// Explicit template instantiations
247
249// Explicit template instantiations are not well understood by doxygen.
250
251#define INSTANTIATE(t) \
252 template t PropertyList::get<t>(std::string const& name) const; \
253 template t PropertyList::get<t>(std::string const& name, t const& defaultValue) const; \
254 template std::vector<t> PropertyList::getArray<t>(std::string const& name) const; \
255 template void PropertyList::set<t>(std::string const& name, t const& value); \
256 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value); \
257 template void PropertyList::add<t>(std::string const& name, t const& value); \
258 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value); \
259 template void PropertyList::set<t>(std::string const& name, t const& value, std::string const& comment); \
260 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
261 std::string const& comment); \
262 template void PropertyList::add<t>(std::string const& name, t const& value, std::string const& comment); \
263 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
264 std::string const& comment); \
265 template void PropertyList::set<t>(std::string const& name, t const& value, char const* comment); \
266 template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
267 char const* comment); \
268 template void PropertyList::add<t>(std::string const& name, t const& value, char const* comment); \
269 template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
270 char const* comment);
271
272INSTANTIATE(bool)
273INSTANTIATE(char)
274INSTANTIATE(signed char)
275INSTANTIATE(unsigned char)
276INSTANTIATE(short)
277INSTANTIATE(unsigned short)
278INSTANTIATE(int)
279INSTANTIATE(unsigned int)
280INSTANTIATE(long)
281INSTANTIATE(unsigned long)
282INSTANTIATE(long long)
283INSTANTIATE(unsigned long long)
284INSTANTIATE(float)
285INSTANTIATE(double)
286INSTANTIATE(std::nullptr_t)
287INSTANTIATE(std::string)
288INSTANTIATE(Persistable::Ptr)
289INSTANTIATE(DateTime)
290
291
292
293} // namespace base
294} // namespace daf
295} // namespace lsst
Interface for DateTime class.
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
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
virtual void copy(std::string const &dest, PropertySet::ConstPtr source, std::string const &name, bool asScalar=false)
Replace a single value vector in the destination with one from the source.
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)
virtual void combine(PropertySet::ConstPtr source)
Append all value vectors from the source to their corresponding properties.
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 _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 Ptr deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
virtual void copy(std::string const &dest, ConstPtr source, std::string const &name, bool asScalar=false)
Replace a single value vector in the destination with one from the source.
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 combine(ConstPtr 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).
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)
T push_back(T... args)
T size(T... args)
T str(T... args)