lsst.daf.base  13.0-2-g167564e+11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
25 
38 
39 #include <algorithm>
40 #include <iomanip>
41 #include <sstream>
42 #include <stdexcept>
43 
44 #include "lsst/pex/exceptions/Runtime.h"
45 #include "lsst/daf/base/DateTime.h"
46 
47 namespace pexExcept = lsst::pex::exceptions;
48 
49 using namespace std;
50 
51 namespace lsst {
52 namespace daf {
53 namespace base {
54 
57 PropertyList::PropertyList(void) : PropertySet(true) {
58 }
59 
63 }
64 
66 // Accessors
68 
73  Ptr n(new PropertyList);
74  n->PropertySet::combine(this->PropertySet::deepCopy());
75  n->_order = _order;
76  n->_comments = _comments;
77  return n;
78 }
79 
80 // The following throw an exception if the type does not match exactly.
81 
90 template <typename T>
91 T PropertyList::get(string const& name) const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
92  return PropertySet::get<T>(name);
93 }
94 
104 template <typename T>
105 T PropertyList::get(string const& name, T const& defaultValue) const { /* parasoft-suppress LsstDm-3-4a LsstDm-4-6 "allow template over bool" */
106  return PropertySet::get<T>(name, defaultValue);
107 }
108 
117 template <typename T>
118 vector<T> PropertyList::getArray(string const& name) const {
119  return PropertySet::getArray<T>(name);
120 }
121 
122 
128 std::string const& PropertyList::getComment(
129  std::string const& name) const {
130  return _comments.find(name)->second;
131 }
132 
133 std::vector<std::string> PropertyList::getOrderedNames(void) const {
134  std::vector<std::string> v;
135  for (std::list<std::string>::const_iterator i = _order.begin();
136  i != _order.end(); ++i) {
137  v.push_back(*i);
138  }
139  return v;
140 }
141 
142 std::list<std::string>::const_iterator
143 PropertyList::begin(void) const {
144  return _order.begin();
145 }
146 
147 std::list<std::string>::const_iterator
148 PropertyList::end(void) const {
149  return _order.end();
150 }
151 
158 std::string PropertyList::toString(bool topLevelOnly,
159  std::string const& indent) const {
160  ostringstream s;
161  for (std::list<std::string>::const_iterator i = _order.begin();
162  i != _order.end(); ++i) {
163  s << _format(*i);
164  std::string const& comment = _comments.find(*i)->second;
165  if (comment.size()) {
166  s << "// " << comment << std::endl;
167  }
168  }
169  return s.str();
170 }
171 
172 
174 // Modifiers
176 
178 // Normal versions of set/add with placement control
179 
186 template <typename T>
188  std::string const& name, T const& value) {
189  PropertySet::set(name, value);
190 }
191 
193  std::string const& name, PropertySet::Ptr const& value) {
194  Ptr pl = std::dynamic_pointer_cast<PropertyList, PropertySet>(value);
195  PropertySet::set(name, value);
196  _comments.erase(name);
197  _order.remove(name);
198  vector<string> names = value->paramNames(false);
199  for (vector<string>::const_iterator i = names.begin();
200  i != names.end(); ++i) {
201  if (pl) {
202  _commentOrderFix(name + "." + *i, pl->getComment(*i));
203  }
204  }
205 }
206 
214  std::string const& name, char const* value) {
215  set(name, string(value));
216 }
217 
224 template <typename T>
226  std::string const& name, vector<T> const& value) {
227  PropertySet::set(name, value);
228 }
229 
237 template <typename T>
239  std::string const& name, T const& value) {
240  PropertySet::add(name, value);
241 }
242 
252  std::string const& name, char const* value) {
253  add(name, string(value));
254 }
255 
265 template <typename T>
267  std::string const& name, vector<T> const& value) {
268  PropertySet::add(name, value);
269 }
270 
271 
273 // Commented versions of set/add
274 
282 template <typename T>
284  std::string const& name, T const& value,
285  std::string const& comment) {
286  PropertySet::set(name, value);
287  _commentOrderFix(name, comment);
288 }
289 
298  std::string const& name, char const* value,
299  std::string const& comment) {
300  set(name, string(value), comment);
301 }
302 
310 template <typename T>
312  std::string const& name, vector<T> const& value,
313  std::string const& comment) {
314  PropertySet::set(name, value);
315  _commentOrderFix(name, comment);
316 }
317 
326 template <typename T>
328  std::string const& name, T const& value,
329  std::string const& comment) {
330  PropertySet::add(name, value);
331  _commentOrderFix(name, comment);
332 }
333 
345  std::string const& name, char const* value,
346  std::string const& comment) {
347  add(name, string(value), comment);
348 }
349 
360 template <typename T>
362  std::string const& name, vector<T> const& value,
363  std::string const& comment) {
364  PropertySet::add(name, value);
365  _commentOrderFix(name, comment);
366 }
367 
368 
370 // Other modifiers
371 
382  std::string const& dest, PropertySet::ConstPtr source,
383  std::string const& name) {
384  PropertySet::copy(dest, source, name);
385  ConstPtr pl =
386  std::dynamic_pointer_cast<PropertyList const, PropertySet const>(
387  source);
388  if (pl) {
389  _comments[name] = pl->_comments.find(name)->second;
390  }
391 }
392 
402  ConstPtr pl =
403  std::dynamic_pointer_cast<PropertyList const, PropertySet const>(
404  source);
405  std::list<std::string> newOrder;
406  if (pl) {
407  newOrder = _order;
408  for (std::list<std::string>::const_iterator i = pl->begin();
409  i != pl->end(); ++i) {
410  bool present = _comments.find(*i) != _comments.end();
411  if (!present) {
412  newOrder.push_back(*i);
413  }
414  }
415  }
416  PropertySet::combine(source);
417  if (pl) {
418  _order = newOrder;
419  for (std::list<std::string>::const_iterator i = pl->begin();
420  i != pl->end(); ++i) {
421  _comments[*i] = pl->_comments.find(*i)->second;
422  }
423  }
424 }
425 
430 void PropertyList::remove(std::string const& name) {
431  PropertySet::remove(name);
432  _comments.erase(name);
433  _order.remove(name);
434 }
435 
437 // Private member functions
439 
440 void PropertyList::_set(std::string const& name,
441  std::shared_ptr< std::vector<boost::any> > vp) {
442  PropertySet::_set(name, vp);
443  if (_comments.find(name) == _comments.end()) {
444  _comments.insert(std::make_pair(name, std::string()));
445  _order.push_back(name);
446  }
447 }
448 
449 void PropertyList::_moveToEnd(std::string const& name) {
450  _order.remove(name);
451  _order.push_back(name);
452 }
453 
454 void PropertyList::_commentOrderFix(
455  std::string const& name, std::string const& comment) {
456  _comments[name] = comment;
457 }
458 
460 // Explicit template instantiations
462 
464 // Explicit template instantiations are not well understood by doxygen.
465 
466 #define INSTANTIATE(t) \
467  template t PropertyList::get<t>(string const& name) const; \
468  template t PropertyList::get<t>(string const& name, t const& defaultValue) const; \
469  template vector<t> PropertyList::getArray<t>(string const& name) const; \
470  template void PropertyList::set<t>(string const& name, t const& value); \
471  template void PropertyList::set<t>(string const& name, vector<t> const& value); \
472  template void PropertyList::add<t>(string const& name, t const& value); \
473  template void PropertyList::add<t>(string const& name, vector<t> const& value); \
474  template void PropertyList::set<t>(string const& name, t const& value, string const& comment); \
475  template void PropertyList::set<t>(string const& name, vector<t> const& value, string const& comment); \
476  template void PropertyList::add<t>(string const& name, t const& value, string const& comment); \
477  template void PropertyList::add<t>(string const& name, vector<t> const& value, string const& comment); \
478  template void PropertyList::set<t>(string const& name, t const& value, char const* comment); \
479  template void PropertyList::set<t>(string const& name, vector<t> const& value, char const* comment); \
480  template void PropertyList::add<t>(string const& name, t const& value, char const* comment); \
481  template void PropertyList::add<t>(string const& name, vector<t> const& value, char const* comment);
482 
483 INSTANTIATE(bool)
484 INSTANTIATE(char)
485 INSTANTIATE(signed char)
486 INSTANTIATE(unsigned char)
487 INSTANTIATE(short)
488 INSTANTIATE(unsigned short)
489 INSTANTIATE(int)
490 INSTANTIATE(unsigned int)
491 INSTANTIATE(long)
492 INSTANTIATE(unsigned long)
493 INSTANTIATE(long long)
494 INSTANTIATE(unsigned long long)
495 INSTANTIATE(float)
496 INSTANTIATE(double)
497 INSTANTIATE(string)
498 INSTANTIATE(Persistable::Ptr)
499 INSTANTIATE(DateTime)
500 
502 
503 } } } // namespace lsst::daf::base
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
std::string const & getComment(std::string const &name) const
Get the comment for a string property name (possibly hierarchical).
std::vector< T > getArray(std::string const &name) const
virtual void remove(std::string const &name)
Removes all values for a property name (possibly hierarchical).
Definition: PropertySet.cc:754
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
virtual void copy(std::string const &dest, ConstPtr source, std::string const &name)
Replaces a single value vector in the destination with one from the source.
Definition: PropertySet.cc:713
virtual void combine(PropertySet::ConstPtr source)
Appends all value vectors from the source to their corresponding properties.
Interface for PropertyList class.
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
Definition: PropertyList.cc:91
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new value.
virtual void copy(std::string const &dest, PropertySet::ConstPtr source, std::string const &name)
Replaces a single value vector in the destination with one from the source.
std::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:86
std::list< std::string >::const_iterator begin(void) const
PropertyList(void)
Constructor.
Definition: PropertyList.cc:57
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new value.
Definition: PropertySet.cc:581
std::vector< std::string > names(bool topLevelOnly=true) const
Get the names in the PropertySet, optionally including those in subproperties.
Definition: PropertySet.cc:117
virtual void remove(std::string const &name)
Removes all values for a property name (possibly hierarchical).
std::shared_ptr< PropertyList const > ConstPtr
Definition: PropertyList.h:86
virtual void combine(ConstPtr source)
Appends all value vectors from the source to their corresponding properties.
Definition: PropertySet.cc:738
void add(std::string const &name, T const &value)
Appends a single value to the vector of values for a property name (possibly hierarchical).
virtual std::string _format(std::string const &name) const
Definition: PropertySet.cc:507
virtual ~PropertyList(void)
Destructor.
Definition: PropertyList.cc:62
std::vector< std::string > getOrderedNames(void) const
Interface for DateTime class.
std::list< std::string >::const_iterator end(void) const
virtual void _set(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
Finds the property name (possibly hierarchical) and sets or replaces its value with the given vector ...
Definition: PropertySet.cc:837
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertyList.
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Copy the PropertySet and all of its contents.
Definition: PropertySet.cc:70
std::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
void add(std::string const &name, T const &value)
Appends a single value to the vector of values for a property name (possibly hierarchical).
Definition: PropertySet.cc:619
virtual PropertySet::Ptr deepCopy(void) const
Copy the PropertyList and all of its contents.
Definition: PropertyList.cc:72