LSSTApplications  19.0.0-11-g2ce9f25+4,20.0.0+1,20.0.0+10,20.0.0+11,20.0.0+13,20.0.0+2,20.0.0+3,20.0.0+4,20.0.0+6,20.0.0-1-g10df615+10,20.0.0-1-g253301a+5,20.0.0-1-g596936a+11,20.0.0-1-g8a53f90+1,20.0.0-1-gc96f8cb+12,20.0.0-1-gd1c87d7+1,20.0.0-17-g41c5faf,20.0.0-2-g04cfba9+4,20.0.0-2-gd11eeda,20.0.0-2-gec03fae+3,20.0.0-3-g082faa5+1,20.0.0-3-gbdbfa727+3,20.0.0-3-gc53c7b6,20.0.0-4-gde602ef96+4,20.0.0-4-ge48a6ca+6,20.0.0-4-ge987224+1,20.0.0-8-g7eef53f7+7,20.0.0-9-g8e1b333,w.2020.28
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 
50 PropertySet::Ptr PropertyList::deepCopy() const {
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 
118  Ptr pl = std::dynamic_pointer_cast<PropertyList, PropertySet>(value);
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);
189  ConstPtr pl = std::dynamic_pointer_cast<PropertyList const, PropertySet const>(source);
190  if (pl) {
191  _comments[name] = pl->_comments.find(name)->second;
192  }
193 }
194 
196  ConstPtr pl = std::dynamic_pointer_cast<PropertyList const, PropertySet const>(source);
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  }
208  if (pl) {
209  _order = newOrder;
210  for (auto const& name : *pl) {
211  _comments[name] = pl->_comments.find(name)->second;
212  }
213  }
214 }
215 
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)
288 INSTANTIATE(DateTime)
289 
290 
292 } // namespace base
293 } // namespace daf
294 } // namespace lsst
lsst::daf::base::PropertyList::combine
virtual void combine(PropertySet::ConstPtr source)
Append all value vectors from the source to their corresponding properties.
Definition: PropertyList.cc:195
lsst::daf::base::PropertyList::getComment
std::string const & getComment(std::string const &name) const
Get the comment for a string property name (possibly hierarchical).
Definition: PropertyList.cc:77
lsst::daf::base::PropertyList::end
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
std::string
STL class.
std::shared_ptr
STL class.
std::list
STL class.
lsst::daf::base::Persistable::Ptr
std::shared_ptr< Persistable > Ptr
Definition: Persistable.h:77
lsst::daf::base::PropertySet::_format
virtual std::string _format(std::string const &name) const
std::vector
STL class.
std::unordered_map::find
T find(T... args)
lsst::daf::base::PropertySet::add
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::string::size
T size(T... args)
DateTime.h
Interface for DateTime class.
lsst::daf::base::PropertyList
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
lsst::daf::base::PropertySet::copy
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.
INSTANTIATE
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:484
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::daf::base::PropertyList::get
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
Definition: PropertyList.cc:61
std::vector::push_back
T push_back(T... args)
PropertyList.h
lsst::daf::base::PropertySet::_set
virtual void _set(std::string const &name, std::shared_ptr< std::vector< boost::any > > vp)
ast::detail::source
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
lsst::daf::base::PropertyList::add
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: PropertyList.cc:138
lsst::daf::base::PropertyList::begin
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
lsst::daf::base::PropertyList::set
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.
Definition: PropertyList.cc:113
lsst::daf::base::PropertyList::copy
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.
Definition: PropertyList.cc:186
std::unordered_map::erase
T erase(T... args)
std::list::remove
T remove(T... args)
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::daf::base::PropertySet::paramNames
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
lsst::daf::base::PropertySet::remove
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
lsst::daf::base::PropertyList::getArray
std::vector< T > getArray(std::string const &name) const
Get the vector of values for a property name (possibly hierarchical).
Definition: PropertyList.cc:73
std::ostringstream
STL class.
lsst::daf::base::PropertyList::PropertyList
PropertyList()
Construct an empty PropertyList.
Definition: PropertyList.cc:40
std::endl
T endl(T... args)
std::list::begin
T begin(T... args)
lsst::daf::base::PropertyList::toString
virtual std::string toString(bool topLevelOnly=false, std::string const &indent="") const
Generate a string representation of the PropertySet.
Definition: PropertyList.cc:93
std::unordered_map::insert
T insert(T... args)
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst::daf::base::PropertyList::~PropertyList
virtual ~PropertyList() noexcept
Destructor.
std::ostringstream::str
T str(T... args)
lsst::daf::base::PropertyList::remove
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
Definition: PropertyList.cc:216
std::make_pair
T make_pair(T... args)
std::list::end
T end(T... args)
lsst::daf::base::PropertySet::combine
virtual void combine(ConstPtr source)
Append all value vectors from the source to their corresponding properties.
lsst::daf::base::PropertyList::getOrderedNames
std::vector< std::string > getOrderedNames() const
Get the list of property names, in the order they were added.
Definition: PropertyList.cc:81
lsst::daf::base::PropertySet::deepCopy
virtual Ptr deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
lsst::daf::base::PropertySet::set
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new scalar value.