LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
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 
32 #include "lsst/daf/base/DateTime.h"
33 
34 namespace lsst {
35 namespace daf {
36 namespace base {
37 
41 
44 PropertyList::~PropertyList() noexcept = default;
45 
47 // Accessors
49 
51  Ptr n(new PropertyList);
52  n->PropertySet::combine(this->PropertySet::deepCopy());
53  n->_order = _order;
54  n->_comments = _comments;
55  return n;
56 }
57 
58 // The following throw an exception if the type does not match exactly.
59 
60 template <typename T>
62  const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
63  return PropertySet::get<T>(name);
64 }
65 
66 template <typename T>
67 T PropertyList::get(std::string const& name, T const& defaultValue)
68  const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
69  return PropertySet::get<T>(name, defaultValue);
70 }
71 
72 template <typename T>
74  return PropertySet::getArray<T>(name);
75 }
76 
78  return _comments.find(name)->second;
79 }
80 
83  for (auto const& name : _order) {
84  v.push_back(name);
85  }
86  return v;
87 }
88 
90 
92 
93 std::string PropertyList::toString(bool topLevelOnly, std::string const& indent) const {
95  for (auto const& name : _order) {
96  s << _format(name);
97  std::string const& comment = _comments.find(name)->second;
98  if (comment.size()) {
99  s << "// " << comment << std::endl;
100  }
101  }
102  return s.str();
103 }
104 
106 // Modifiers
108 
110 // Normal versions of set/add with placement control
111 
112 template <typename T>
113 void PropertyList::set(std::string const& name, T const& value) {
114  PropertySet::set(name, value);
115 }
116 
119  PropertySet::set(name, value);
120  _comments.erase(name);
121  _order.remove(name);
122  std::vector<std::string> paramNames = value->paramNames(false);
123  if (pl) {
124  for (auto const& paramName : paramNames) {
125  _commentOrderFix(name + "." + paramName, pl->getComment(paramName));
126  }
127  }
128 }
129 
130 void PropertyList::set(std::string const& name, char const* value) { set(name, std::string(value)); }
131 
132 template <typename T>
133 void PropertyList::set(std::string const& name, std::vector<T> const& value) {
134  PropertySet::set(name, value);
135 }
136 
137 template <typename T>
138 void PropertyList::add(std::string const& name, T const& value) {
139  PropertySet::add(name, value);
140 }
141 
142 void PropertyList::add(std::string const& name, char const* value) { add(name, std::string(value)); }
143 
144 template <typename T>
145 void PropertyList::add(std::string const& name, std::vector<T> const& value) {
146  PropertySet::add(name, value);
147 }
148 
150 // Commented versions of set/add
151 
152 template <typename T>
153 void PropertyList::set(std::string const& name, T const& value, std::string const& comment) {
154  PropertySet::set(name, value);
155  _commentOrderFix(name, comment);
156 }
157 
158 void PropertyList::set(std::string const& name, char const* value, std::string const& comment) {
159  set(name, std::string(value), comment);
160 }
161 
162 template <typename T>
163 void PropertyList::set(std::string const& name, std::vector<T> const& value, std::string const& comment) {
164  PropertySet::set(name, value);
165  _commentOrderFix(name, comment);
166 }
167 template <typename T>
168 void PropertyList::add(std::string const& name, T const& value, std::string const& comment) {
169  PropertySet::add(name, value);
170  _commentOrderFix(name, comment);
171 }
172 
173 void PropertyList::add(std::string const& name, char const* value, std::string const& comment) {
174  add(name, std::string(value), comment);
175 }
176 
177 template <typename T>
178 void PropertyList::add(std::string const& name, std::vector<T> const& value, std::string const& comment) {
179  PropertySet::add(name, value);
180  _commentOrderFix(name, comment);
181 }
182 
184 // Other modifiers
185 
187  bool asScalar) {
188  PropertySet::copy(dest, source, name, asScalar);
190  if (pl) {
191  _comments[name] = pl->_comments.find(name)->second;
192  }
193 }
194 
197  std::list<std::string> newOrder;
198  if (pl) {
199  newOrder = _order;
200  for (auto const& name : *pl) {
201  bool present = _comments.find(name) != _comments.end();
202  if (!present) {
203  newOrder.push_back(name);
204  }
205  }
206  }
207  PropertySet::combine(source);
208  if (pl) {
209  _order = newOrder;
210  for (auto const& name : *pl) {
211  _comments[name] = pl->_comments.find(name)->second;
212  }
213  }
214 }
215 
217  PropertySet::remove(name);
218  _comments.erase(name);
219  _order.remove(name);
220 }
221 
223 // Private member functions
225 
226 void PropertyList::_set(std::string const& name, std::shared_ptr<std::vector<boost::any> > vp) {
227  PropertySet::_set(name, vp);
228  if (_comments.find(name) == _comments.end()) {
229  _comments.insert(std::make_pair(name, std::string()));
230  _order.push_back(name);
231  }
232 }
233 
234 void PropertyList::_moveToEnd(std::string const& name) {
235  _order.remove(name);
236  _order.push_back(name);
237 }
238 
239 void PropertyList::_commentOrderFix(std::string const& name, std::string const& comment) {
240  _comments[name] = comment;
241 }
242 
244 // Explicit template instantiations
246 
248 // Explicit template instantiations are not well understood by doxygen.
249 
250 #define INSTANTIATE(t) \
251  template t PropertyList::get<t>(std::string const& name) const; \
252  template t PropertyList::get<t>(std::string const& name, t const& defaultValue) const; \
253  template std::vector<t> PropertyList::getArray<t>(std::string const& name) const; \
254  template void PropertyList::set<t>(std::string const& name, t const& value); \
255  template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value); \
256  template void PropertyList::add<t>(std::string const& name, t const& value); \
257  template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value); \
258  template void PropertyList::set<t>(std::string const& name, t const& value, std::string const& comment); \
259  template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
260  std::string const& comment); \
261  template void PropertyList::add<t>(std::string const& name, t const& value, std::string const& comment); \
262  template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
263  std::string const& comment); \
264  template void PropertyList::set<t>(std::string const& name, t const& value, char const* comment); \
265  template void PropertyList::set<t>(std::string const& name, std::vector<t> const& value, \
266  char const* comment); \
267  template void PropertyList::add<t>(std::string const& name, t const& value, char const* comment); \
268  template void PropertyList::add<t>(std::string const& name, std::vector<t> const& value, \
269  char const* comment);
270 
271 INSTANTIATE(bool)
272 INSTANTIATE(char)
273 INSTANTIATE(signed char)
274 INSTANTIATE(unsigned char)
275 INSTANTIATE(short)
276 INSTANTIATE(unsigned short)
277 INSTANTIATE(int)
278 INSTANTIATE(unsigned int)
279 INSTANTIATE(long)
280 INSTANTIATE(unsigned long)
281 INSTANTIATE(long long)
282 INSTANTIATE(unsigned long long)
283 INSTANTIATE(float)
284 INSTANTIATE(double)
285 INSTANTIATE(nullptr_t)
289 
290 
292 } // namespace base
293 } // namespace daf
294 } // namespace lsst
virtual PropertySet::Ptr deepCopy() const
Make a deep copy of the PropertyList and all of its contents.
Definition: PropertyList.cc:50
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:64
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 toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
Definition: PropertyList.cc:93
virtual ~PropertyList() noexcept
Destructor.
virtual void combine(ConstPtr source)
Append all value vectors from the source to their corresponding properties.
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
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:77
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
Definition: PropertyList.cc:73
T endl(T... args)
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
virtual std::string _format(std::string const &name) const
T end(T... args)
virtual void remove(std::string const &name)
Remove all 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 remove(T... args)
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
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:89
STL class.
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
T push_back(T... args)
A base class for image defects.
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 erase(T... args)
virtual Ptr deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
T str(T... args)
T make_pair(T... args)
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
T dynamic_pointer_cast(T... args)
virtual void _set(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
STL class.
Interface for DateTime class.
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:91
T insert(T... args)
T find(T... args)
Definition: __init__.py:1
T size(T... args)
STL class.
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
Definition: PropertyList.cc:61
T begin(T... args)
Class for storing generic metadata.
Definition: PropertySet.h:67
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 > getOrderedNames() const
Get the list of property names, in the order they were added.
Definition: PropertyList.cc:81
PropertyList()
Construct an empty PropertyList.
Definition: PropertyList.cc:40
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.
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:484