LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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 
33 #include "lsst/daf/base/DateTime.h"
34 
35 namespace lsst {
36 namespace daf {
37 namespace base {
38 
42 
45 PropertyList::~PropertyList() noexcept = default;
46 
48 // Accessors
50 
51 PropertySet::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 
61 template <typename T>
63  const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
64  return PropertySet::get<T>(name);
65 }
66 
67 template <typename T>
68 T 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 
73 template <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 
94 std::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 
113 template <typename T>
114 void PropertyList::set(std::string const& name, T const& value) {
115  PropertySet::set(name, value);
116 }
117 
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 
131 void PropertyList::set(std::string const& name, char const* value) { set(name, std::string(value)); }
132 
133 template <typename T>
134 void PropertyList::set(std::string const& name, std::vector<T> const& value) {
135  PropertySet::set(name, value);
136 }
137 
138 template <typename T>
139 void PropertyList::add(std::string const& name, T const& value) {
140  PropertySet::add(name, value);
141 }
142 
143 void PropertyList::add(std::string const& name, char const* value) { add(name, std::string(value)); }
144 
145 template <typename T>
146 void 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 
153 template <typename T>
154 void PropertyList::set(std::string const& name, T const& value, std::string const& comment) {
155  PropertySet::set(name, value);
156  _commentOrderFix(name, comment);
157 }
158 
159 void PropertyList::set(std::string const& name, char const* value, std::string const& comment) {
160  set(name, std::string(value), comment);
161 }
162 
163 template <typename T>
164 void 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 }
168 template <typename T>
169 void PropertyList::add(std::string const& name, T const& value, std::string const& comment) {
170  PropertySet::add(name, value);
171  _commentOrderFix(name, comment);
172 }
173 
174 void PropertyList::add(std::string const& name, char const* value, std::string const& comment) {
175  add(name, std::string(value), comment);
176 }
177 
178 template <typename T>
179 void 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  }
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 
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 
272 INSTANTIATE(bool)
273 INSTANTIATE(char)
274 INSTANTIATE(signed char)
275 INSTANTIATE(unsigned char)
276 INSTANTIATE(short)
277 INSTANTIATE(unsigned short)
278 INSTANTIATE(int)
279 INSTANTIATE(unsigned int)
280 INSTANTIATE(long)
281 INSTANTIATE(unsigned long)
282 INSTANTIATE(long long)
283 INSTANTIATE(unsigned long long)
284 INSTANTIATE(float)
285 INSTANTIATE(double)
290 
291 
293 } // namespace base
294 } // namespace daf
295 } // namespace lsst
table::Key< std::string > name
Definition: Amplifier.cc:116
Interface for DateTime class.
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:484
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
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
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
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).
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)
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
A base class for image defects.
T push_back(T... args)
T remove(T... args)
T size(T... args)
T str(T... args)