LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 
25 
38 
39 #include <algorithm>
40 #include <iomanip>
41 #include <sstream>
42 #include <stdexcept>
43 
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 
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 
187 template <typename T>
189  std::string const& name, T const& value, bool inPlace) {
190  PropertySet::set(name, value);
191  if (!inPlace) {
192  _moveToEnd(name);
193  }
194 }
195 
197  std::string const& name, PropertySet::Ptr const& value,
198  bool inPlace) {
199  Ptr pl = boost::dynamic_pointer_cast<PropertyList, PropertySet>(value);
200  PropertySet::set(name, value);
201  _comments.erase(name);
202  _order.remove(name);
203  vector<string> names = value->paramNames(false);
204  for (vector<string>::const_iterator i = names.begin();
205  i != names.end(); ++i) {
206  if (pl) {
207  _commentOrderFix(name + "." + *i, pl->getComment(*i), inPlace);
208  }
209  else if (inPlace) {
210  _moveToEnd(name + "." + *i);
211  }
212  }
213 }
214 
223  std::string const& name, char const* value, bool inPlace) {
224  set(name, string(value), inPlace);
225  if (!inPlace) {
226  _moveToEnd(name);
227  }
228 }
229 
237 template <typename T>
239  std::string const& name, vector<T> const& value, bool inPlace) {
240  PropertySet::set(name, value);
241  if (!inPlace) {
242  _moveToEnd(name);
243  }
244 }
245 
254 template <typename T>
256  std::string const& name, T const& value, bool inPlace) {
257  PropertySet::add(name, value);
258  if (!inPlace) {
259  _moveToEnd(name);
260  }
261 }
262 
273  std::string const& name, char const* value, bool inPlace) {
274  add(name, string(value), inPlace);
275 }
276 
287 template <typename T>
289  std::string const& name, vector<T> const& value, bool inPlace) {
290  PropertySet::add(name, value);
291  if (!inPlace) {
292  _moveToEnd(name);
293  }
294 }
295 
296 
298 // Commented versions of set/add
299 
308 template <typename T>
310  std::string const& name, T const& value,
311  std::string const& comment, bool inPlace) {
312  PropertySet::set(name, value);
313  _commentOrderFix(name, comment, inPlace);
314 }
315 
325  std::string const& name, char const* value,
326  std::string const& comment, bool inPlace) {
327  set(name, string(value), comment, inPlace);
328 }
329 
338 template <typename T>
340  std::string const& name, vector<T> const& value,
341  std::string const& comment, bool inPlace) {
342  PropertySet::set(name, value);
343  _commentOrderFix(name, comment, inPlace);
344 }
345 
355 template <typename T>
357  std::string const& name, T const& value,
358  std::string const& comment, bool inPlace) {
359  PropertySet::add(name, value);
360  _commentOrderFix(name, comment, inPlace);
361 }
362 
375  std::string const& name, char const* value,
376  std::string const& comment, bool inPlace) {
377  add(name, string(value), comment, inPlace);
378 }
379 
391 template <typename T>
393  std::string const& name, vector<T> const& value,
394  std::string const& comment, bool inPlace) {
395  PropertySet::add(name, value);
396  _commentOrderFix(name, comment, inPlace);
397 }
398 
399 
401 // Other modifiers
402 
414  std::string const& dest, PropertySet::ConstPtr source,
415  std::string const& name, bool inPlace) {
416  PropertySet::copy(dest, source, name);
417  ConstPtr pl =
418  boost::dynamic_pointer_cast<PropertyList const, PropertySet const>(
419  source);
420  if (pl) {
421  _comments[name] = pl->_comments.find(name)->second;
422  if (!inPlace) {
423  _moveToEnd(name);
424  }
425  }
426 }
427 
438  bool inPlace) {
439  ConstPtr pl =
440  boost::dynamic_pointer_cast<PropertyList const, PropertySet const>(
441  source);
442  std::list<std::string> newOrder;
443  if (pl) {
444  newOrder = _order;
445  for (std::list<std::string>::const_iterator i = pl->begin();
446  i != pl->end(); ++i) {
447  bool present = _comments.find(*i) != _comments.end();
448  if (!present) {
449  newOrder.push_back(*i);
450  }
451  else if (!inPlace) {
452  newOrder.remove(*i);
453  newOrder.push_back(*i);
454  }
455  }
456  }
457  PropertySet::combine(source);
458  if (pl) {
459  _order = newOrder;
460  for (std::list<std::string>::const_iterator i = pl->begin();
461  i != pl->end(); ++i) {
462  _comments[*i] = pl->_comments.find(*i)->second;
463  }
464  }
465 }
466 
471 void PropertyList::remove(std::string const& name) {
472  PropertySet::remove(name);
473  _comments.erase(name);
474  _order.remove(name);
475 }
476 
478 // Private member functions
480 
481 void PropertyList::_set(std::string const& name,
482  boost::shared_ptr< std::vector<boost::any> > vp) {
483  PropertySet::_set(name, vp);
484  if (_comments.find(name) == _comments.end()) {
485  _comments.insert(std::make_pair(name, std::string()));
486  _order.push_back(name);
487  }
488 }
489 
490 void PropertyList::_moveToEnd(std::string const& name) {
491  _order.remove(name);
492  _order.push_back(name);
493 }
494 
496  std::string const& name, std::string const& comment, bool inPlace) {
497  _comments[name] = comment;
498  if (!inPlace) {
499  _moveToEnd(name);
500  }
501 }
502 
504 // Explicit template instantiations
506 
508 // Explicit template instantiations are not well understood by doxygen.
509 
510 #define INSTANTIATE(t) \
511  template t PropertyList::get<t>(string const& name) const; \
512  template t PropertyList::get<t>(string const& name, t const& defaultValue) const; \
513  template vector<t> PropertyList::getArray<t>(string const& name) const; \
514  template void PropertyList::set<t>(string const& name, t const& value, bool inPlace); \
515  template void PropertyList::set<t>(string const& name, vector<t> const& value, bool inPlace); \
516  template void PropertyList::add<t>(string const& name, t const& value, bool inPlace); \
517  template void PropertyList::add<t>(string const& name, vector<t> const& value, bool inPlace); \
518  template void PropertyList::set<t>(string const& name, t const& value, string const& comment, bool inPlace); \
519  template void PropertyList::set<t>(string const& name, vector<t> const& value, string const& comment, bool inPlace); \
520  template void PropertyList::add<t>(string const& name, t const& value, string const& comment, bool inPlace); \
521  template void PropertyList::add<t>(string const& name, vector<t> const& value, string const& comment, bool inPlace); \
522  template void PropertyList::set<t>(string const& name, t const& value, char const* comment, bool inPlace); \
523  template void PropertyList::set<t>(string const& name, vector<t> const& value, char const* comment, bool inPlace); \
524  template void PropertyList::add<t>(string const& name, t const& value, char const* comment, bool inPlace); \
525  template void PropertyList::add<t>(string const& name, vector<t> const& value, char const* comment, bool inPlace);
526 
527 INSTANTIATE(bool)
528 INSTANTIATE(char)
529 INSTANTIATE(signed char)
530 INSTANTIATE(unsigned char)
531 INSTANTIATE(short)
532 INSTANTIATE(unsigned short)
533 INSTANTIATE(int)
534 INSTANTIATE(unsigned int)
535 INSTANTIATE(long)
536 INSTANTIATE(unsigned long)
537 INSTANTIATE(long long)
538 INSTANTIATE(unsigned long long)
539 INSTANTIATE(float)
540 INSTANTIATE(double)
541 INSTANTIATE(string)
544 
546 
547 } } } // namespace lsst::daf::base
boost::shared_ptr< PropertyList const > ConstPtr
Definition: PropertyList.h:85
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
std::string const & getComment(std::string const &name) const
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::vector< T > getArray(std::string const &name) const
virtual void remove(std::string const &name)
Definition: PropertySet.cc:754
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
virtual void copy(std::string const &dest, ConstPtr source, std::string const &name)
Definition: PropertySet.cc:713
virtual void copy(std::string const &dest, PropertySet::ConstPtr source, std::string const &name)
Definition: PropertyList.h:184
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
Interface for PropertyList class.
boost::shared_ptr< PropertySet const > ConstPtr
Definition: PropertySet.h:91
T get(std::string const &name) const
Definition: PropertyList.cc:91
std::list< std::string >::const_iterator begin(void) const
void set(std::string const &name, T const &value)
Definition: PropertySet.cc:581
virtual void _moveToEnd(std::string const &name)
std::vector< std::string > names(bool topLevelOnly=true) const
Definition: PropertySet.cc:117
virtual void remove(std::string const &name)
void set(std::string const &name, T const &value, bool inPlace=true)
std::list< std::string > _order
Definition: PropertyList.h:210
virtual void combine(ConstPtr source)
Definition: PropertySet.cc:738
boost::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
virtual std::string _format(std::string const &name) const
Definition: PropertySet.cc:507
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
virtual void _set(std::string const &name, boost::shared_ptr< std::vector< boost::any > > vp)
std::vector< std::string > getOrderedNames(void) const
Interface for DateTime class.
std::list< std::string >::const_iterator end(void) const
#define INSTANTIATE(T)
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Definition: PropertySet.cc:70
virtual void _commentOrderFix(std::string const &name, std::string const &comment, bool inPlace)
virtual void _set(std::string const &name, boost::shared_ptr< std::vector< boost::any > > vp)
Definition: PropertySet.cc:837
Base class for all persistable classes.
Definition: Persistable.h:74
void add(std::string const &name, T const &value, bool inPlace=true)
void add(std::string const &name, T const &value)
Definition: PropertySet.cc:619
virtual PropertySet::Ptr deepCopy(void) const
Definition: PropertyList.cc:72
virtual void combine(PropertySet::ConstPtr source)
Definition: PropertyList.h:188