lsst.daf.base  14.0-6-gf4f1c34+2
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 
33 #include "lsst/daf/base/DateTime.h"
34 
36 
37 using namespace std;
38 
39 namespace lsst {
40 namespace daf {
41 namespace base {
42 
45 PropertyList::PropertyList(void) : PropertySet(true) {
46 }
47 
51 }
52 
54 // Accessors
56 
58  Ptr n(new PropertyList);
59  n->PropertySet::combine(this->PropertySet::deepCopy());
60  n->_order = _order;
61  n->_comments = _comments;
62  return n;
63 }
64 
65 // The following throw an exception if the type does not match exactly.
66 
67 template <typename T>
68 T PropertyList::get(string const& name) const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
69  return PropertySet::get<T>(name);
70 }
71 
72 template <typename T>
73 T PropertyList::get(string const& name, T const& defaultValue) const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
74  return PropertySet::get<T>(name, defaultValue);
75 }
76 
77 template <typename T>
78 vector<T> PropertyList::getArray(string const& name) const {
79  return PropertySet::getArray<T>(name);
80 }
81 
83  std::string const& name) const {
84  return _comments.find(name)->second;
85 }
86 
90  i != _order.end(); ++i) {
91  v.push_back(*i);
92  }
93  return v;
94 }
95 
97 PropertyList::begin(void) const {
98  return _order.begin();
99 }
100 
102 PropertyList::end(void) const {
103  return _order.end();
104 }
105 
107  std::string const& indent) const {
108  ostringstream s;
110  i != _order.end(); ++i) {
111  s << _format(*i);
112  std::string const& comment = _comments.find(*i)->second;
113  if (comment.size()) {
114  s << "// " << comment << std::endl;
115  }
116  }
117  return s.str();
118 }
119 
120 
122 // Modifiers
124 
126 // Normal versions of set/add with placement control
127 
128 template <typename T>
130  std::string const& name, T const& value) {
131  PropertySet::set(name, value);
132 }
133 
135  std::string const& name, PropertySet::Ptr const& value) {
137  PropertySet::set(name, value);
138  _comments.erase(name);
139  _order.remove(name);
140  vector<string> names = value->paramNames(false);
141  for (vector<string>::const_iterator i = names.begin();
142  i != names.end(); ++i) {
143  if (pl) {
144  _commentOrderFix(name + "." + *i, pl->getComment(*i));
145  }
146  }
147 }
148 
150  std::string const& name, char const* value) {
151  set(name, string(value));
152 }
153 
154 template <typename T>
156  std::string const& name, vector<T> const& value) {
157  PropertySet::set(name, value);
158 }
159 
160 template <typename T>
162  std::string const& name, T const& value) {
163  PropertySet::add(name, value);
164 }
165 
167  std::string const& name, char const* value) {
168  add(name, string(value));
169 }
170 
171 template <typename T>
173  std::string const& name, vector<T> const& value) {
174  PropertySet::add(name, value);
175 }
176 
177 
179 // Commented versions of set/add
180 
181 template <typename T>
183  std::string const& name, T const& value,
184  std::string const& comment) {
185  PropertySet::set(name, value);
186  _commentOrderFix(name, comment);
187 }
188 
190  std::string const& name, char const* value,
191  std::string const& comment) {
192  set(name, string(value), comment);
193 }
194 
195 template <typename T>
197  std::string const& name, vector<T> const& value,
198  std::string const& comment) {
199  PropertySet::set(name, value);
200  _commentOrderFix(name, comment);
201 }
202 template <typename T>
204  std::string const& name, T const& value,
205  std::string const& comment) {
206  PropertySet::add(name, value);
207  _commentOrderFix(name, comment);
208 }
209 
211  std::string const& name, char const* value,
212  std::string const& comment) {
213  add(name, string(value), comment);
214 }
215 
216 template <typename T>
218  std::string const& name, vector<T> const& value,
219  std::string const& comment) {
220  PropertySet::add(name, value);
221  _commentOrderFix(name, comment);
222 }
223 
224 
226 // Other modifiers
227 
229  std::string const& dest, PropertySet::ConstPtr source,
230  std::string const& name, bool asScalar) {
231  PropertySet::copy(dest, source, name, asScalar);
232  ConstPtr pl =
234  source);
235  if (pl) {
236  _comments[name] = pl->_comments.find(name)->second;
237  }
238 }
239 
241  ConstPtr pl =
243  source);
244  std::list<std::string> newOrder;
245  if (pl) {
246  newOrder = _order;
247  for (std::list<std::string>::const_iterator i = pl->begin();
248  i != pl->end(); ++i) {
249  bool present = _comments.find(*i) != _comments.end();
250  if (!present) {
251  newOrder.push_back(*i);
252  }
253  }
254  }
255  PropertySet::combine(source);
256  if (pl) {
257  _order = newOrder;
258  for (std::list<std::string>::const_iterator i = pl->begin();
259  i != pl->end(); ++i) {
260  _comments[*i] = pl->_comments.find(*i)->second;
261  }
262  }
263 }
264 
266  PropertySet::remove(name);
267  _comments.erase(name);
268  _order.remove(name);
269 }
270 
272 // Private member functions
274 
275 void PropertyList::_set(std::string const& name,
277  PropertySet::_set(name, vp);
278  if (_comments.find(name) == _comments.end()) {
279  _comments.insert(std::make_pair(name, std::string()));
280  _order.push_back(name);
281  }
282 }
283 
284 void PropertyList::_moveToEnd(std::string const& name) {
285  _order.remove(name);
286  _order.push_back(name);
287 }
288 
289 void PropertyList::_commentOrderFix(
290  std::string const& name, std::string const& comment) {
291  _comments[name] = comment;
292 }
293 
295 // Explicit template instantiations
297 
299 // Explicit template instantiations are not well understood by doxygen.
300 
301 #define INSTANTIATE(t) \
302  template t PropertyList::get<t>(string const& name) const; \
303  template t PropertyList::get<t>(string const& name, t const& defaultValue) const; \
304  template vector<t> PropertyList::getArray<t>(string const& name) const; \
305  template void PropertyList::set<t>(string const& name, t const& value); \
306  template void PropertyList::set<t>(string const& name, vector<t> const& value); \
307  template void PropertyList::add<t>(string const& name, t const& value); \
308  template void PropertyList::add<t>(string const& name, vector<t> const& value); \
309  template void PropertyList::set<t>(string const& name, t const& value, string const& comment); \
310  template void PropertyList::set<t>(string const& name, vector<t> const& value, string const& comment); \
311  template void PropertyList::add<t>(string const& name, t const& value, string const& comment); \
312  template void PropertyList::add<t>(string const& name, vector<t> const& value, string const& comment); \
313  template void PropertyList::set<t>(string const& name, t const& value, char const* comment); \
314  template void PropertyList::set<t>(string const& name, vector<t> const& value, char const* comment); \
315  template void PropertyList::add<t>(string const& name, t const& value, char const* comment); \
316  template void PropertyList::add<t>(string const& name, vector<t> const& value, char const* comment);
317 
318 INSTANTIATE(bool)
319 INSTANTIATE(char)
320 INSTANTIATE(signed char)
321 INSTANTIATE(unsigned char)
322 INSTANTIATE(short)
323 INSTANTIATE(unsigned short)
324 INSTANTIATE(int)
325 INSTANTIATE(unsigned int)
326 INSTANTIATE(long)
327 INSTANTIATE(unsigned long)
328 INSTANTIATE(long long)
329 INSTANTIATE(unsigned long long)
330 INSTANTIATE(float)
331 INSTANTIATE(double)
332 INSTANTIATE(string)
333 INSTANTIATE(Persistable::Ptr)
334 INSTANTIATE(DateTime)
335 
336 
338 } } } // namespace lsst::daf::base
virtual Ptr deepCopy(void) const
Make a deep copy of the PropertySet and all of its contents.
Definition: PropertySet.cc:51
std::list< std::string >::const_iterator end(void) const
End iterator over the list of property names, in the order they were added.
virtual std::string _format(std::string const &name) const
Definition: PropertySet.cc:363
std::list< std::string >::const_iterator begin(void) const
Begin iterator over the list of property names, in the order they were added.
Definition: PropertyList.cc:97
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
Definition: PropertySet.cc:554
Class for storing ordered metadata with comments.
Definition: PropertyList.h:73
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:82
T endl(T... args)
STL namespace.
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
T end(T... args)
PropertyList(void)
Construct an empty PropertyList.
Definition: PropertyList.cc:45
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.
Definition: PropertySet.cc:431
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
STL class.
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.
Definition: PropertySet.cc:519
T push_back(T... args)
virtual PropertySet::Ptr deepCopy(void) const
Make a deep copy of the PropertyList and all of its contents.
Definition: PropertyList.cc:57
virtual void combine(ConstPtr source)
Append all value vectors from the source to their corresponding properties.
Definition: PropertySet.cc:542
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 > names(bool topLevelOnly=true) const
Get the names in the PropertySet, optionally including those in subproperties.
Definition: PropertySet.cc:87
std::vector< std::string > getOrderedNames(void) const
Get the list of property names, in the order they were added.
Definition: PropertyList.cc:87
T erase(T... args)
T str(T... args)
T make_pair(T... args)
T dynamic_pointer_cast(T... args)
virtual ~PropertyList(void)
Destructor.
Definition: PropertyList.cc:50
STL class.
Interface for DateTime class.
T insert(T... args)
T find(T... args)
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:68
virtual void _set(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
Definition: PropertySet.cc:622
T begin(T... args)
Class for storing generic metadata.
Definition: PropertySet.h:73
void add(std::string const &name, T const &value)
Append a single value to the vector of values for a property name (possibly hierarchical).
Definition: PropertySet.cc:451
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.