LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
PropertyPrinter.h
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 
30 #ifndef LSST_PEX_PROPERTYPRINTER_H
31 #define LSST_PEX_PROPERTYPRINTER_H
32 
33 #include <string>
34 #include <ostream>
35 #include <sstream>
36 #include <vector>
37 #include <typeinfo>
38 
40 #include "lsst/daf/base/DateTime.h"
41 #include "boost/any.hpp"
42 
43 namespace lsst {
44 namespace pex {
45 namespace logging {
46 
47 
55 class PrinterIter {
56 public:
57 
58  virtual ~PrinterIter()=0;
59 
63  virtual std::ostream& write(std::ostream *strm) const=0;
64 
68  virtual PrinterIter& operator++()=0;
69 
73  virtual PrinterIter& operator--()=0;
74 
81  virtual bool operator==(const PrinterIter& that) const=0;
82 
89  virtual bool operator!=(const PrinterIter& that) const=0;
90 
95  virtual bool notAtEnd() const=0;
96 
101  virtual bool notLTBegin() const=0;
102 
108  const std::string operator*() const {
109  std::ostringstream out;
110  write(&out);
111  return out.str();
112  }
113 };
114 
115 
116 template <class T>
118 public:
119  BaseTmplPrinterIter(typename std::vector<T>::const_iterator listiter,
120  typename std::vector<T>::const_iterator beginiter,
121  typename std::vector<T>::const_iterator enditer)
122  : _it(listiter), _begin(beginiter), _end(enditer) { }
123  virtual ~BaseTmplPrinterIter();
124  virtual PrinterIter& operator++();
125  virtual PrinterIter& operator--();
126  virtual bool operator==(const PrinterIter& that) const;
127  virtual bool operator!=(const PrinterIter& that) const;
128  virtual bool notAtEnd() const;
129  virtual bool notLTBegin() const;
130  bool operator==(const BaseTmplPrinterIter& that) const {
131  return (this->_it == that._it);
132  }
133  bool operator!=(const BaseTmplPrinterIter& that) const {
134  return (this->_it != that._it);
135  }
136 protected:
137  typename std::vector<T>::const_iterator _it, _begin, _end;
138 };
139 
140 template <class T>
142 template <class T>
144 template <class T>
146 template <class T>
148  const BaseTmplPrinterIter<T> *thit =
149  dynamic_cast<const BaseTmplPrinterIter<T>*>(&that);
150  return (thit == 0) ? false : (*this == *thit);
151 }
152 template <class T>
154  const BaseTmplPrinterIter<T> *thit =
155  dynamic_cast<const BaseTmplPrinterIter<T>*>(&that);
156  return (thit == 0) ? true : (*this != *thit);
157 }
158 template <class T>
159 bool BaseTmplPrinterIter<T>::notAtEnd() const { return (_it != _end); }
160 template <class T>
161 bool BaseTmplPrinterIter<T>::notLTBegin() const { return (_it >= _begin); }
162 
163 template <class T>
165 public:
166  TmplPrinterIter(typename std::vector<T>::const_iterator listiter,
167  typename std::vector<T>::const_iterator beginiter,
168  typename std::vector<T>::const_iterator enditer)
169  : BaseTmplPrinterIter<T>(listiter, beginiter, enditer) { }
170  virtual std::ostream& write(std::ostream *strm) const;
171 };
172 
173 template <class T>
174 std::ostream& TmplPrinterIter<T>::write(std::ostream *strm) const {
175  (*strm) << *(this->_it);
176  return *strm;
177 }
178 
179 
187 public:
188  WrappedPrinterIter(boost::shared_ptr<PrinterIter> iter) : _it(iter) { }
189  virtual ~WrappedPrinterIter();
190  virtual std::ostream& write(std::ostream *strm) const;
191  virtual PrinterIter& operator++();
192  virtual PrinterIter& operator--();
193  virtual bool operator==(const PrinterIter& that) const;
194  virtual bool operator!=(const PrinterIter& that) const;
195  virtual bool notAtEnd() const;
196  virtual bool notLTBegin() const;
197 private:
198  boost::shared_ptr<PrinterIter> _it;
199 };
200 
227 class PrinterList {
228 public:
230 
231  virtual ~PrinterList();
232 
236  virtual iterator begin() const=0;
237 
241  virtual iterator last() const=0;
242 
246  virtual size_t valueCount() const=0;
247 };
248 
249 template <class T>
251 public:
253  const std::string& name)
254  : _list(prop.getArray<T>(name)) { }
255 
256  virtual ~BaseTmplPrinterList();
257  virtual size_t valueCount() const;
258 
259 protected:
260  std::vector<T> _list;
261 };
262 
263 template <class T>
265 template <class T>
266 size_t BaseTmplPrinterList<T>::valueCount() const { return _list.size(); }
267 
268 template <class T>
270 public:
271  TmplPrinterList(const lsst::daf::base::PropertySet& prop, const std::string& name)
272  : BaseTmplPrinterList<T>(prop, name) { }
273 
274  virtual PrinterList::iterator begin() const;
275  virtual PrinterList::iterator last() const;
276 
277 private:
279 };
280 
281 template <class T>
286  return PrinterList::iterator(boost::shared_ptr<PrinterIter>(it));
287 }
288 template <class T>
293  return PrinterList::iterator(boost::shared_ptr<PrinterIter>(it));
294 }
295 
305 template <class T>
307  const std::string& name) {
308  return new TmplPrinterList<T>(prop, name);
309 }
310 
311 class DateTimePrinterIter : public BaseTmplPrinterIter<lsst::daf::base::DateTime> {
312 public:
313  DateTimePrinterIter(std::vector<lsst::daf::base::DateTime>::const_iterator listiter,
314  std::vector<lsst::daf::base::DateTime>::const_iterator beginiter,
315  std::vector<lsst::daf::base::DateTime>::const_iterator enditer)
316  : BaseTmplPrinterIter<lsst::daf::base::DateTime>(listiter, beginiter, enditer)
317  { }
318 
319  virtual ~DateTimePrinterIter();
320  virtual std::ostream& write(std::ostream *strm) const;
321 };
322 
323 class DateTimePrinterList : public BaseTmplPrinterList<lsst::daf::base::DateTime> {
324 public:
326  const std::string& name)
327  : BaseTmplPrinterList<lsst::daf::base::DateTime>(prop, name) { }
328 
329  virtual ~DateTimePrinterList();
330  virtual iterator begin() const;
331  virtual iterator last() const;
332 };
333 
334 PrinterList* makeDateTimePrinter(const lsst::daf::base::PropertySet& prop,
335  const std::string& name);
336 
337 class BoolPrinterIter : public BaseTmplPrinterIter<bool> {
338 public:
339  BoolPrinterIter(std::vector<bool>::const_iterator listiter,
340  std::vector<bool>::const_iterator beginiter,
341  std::vector<bool>::const_iterator enditer)
342  : BaseTmplPrinterIter<bool>(listiter, beginiter, enditer) { }
343  virtual ~BoolPrinterIter();
344  virtual std::ostream& write(std::ostream *strm) const;
345 };
346 
347 class BoolPrinterList : public BaseTmplPrinterList<bool> {
348 public:
349  BoolPrinterList(const lsst::daf::base::PropertySet& prop, const std::string& name)
350  : BaseTmplPrinterList<bool>(prop, name) { }
351 
352  virtual ~BoolPrinterList();
353  virtual iterator begin() const;
354  virtual iterator last() const;
355 };
356 
357 PrinterList* makeBoolPrinter(const lsst::daf::base::PropertySet& prop,
358  const std::string& name);
359 
381 public:
382  typedef PrinterList*
383  (*factoryFuncPtr)(const lsst::daf::base::PropertySet&, const std::string&);
384 
385  PrinterFactory(bool loadDefaults=false) : _factFuncs() {
386  if (loadDefaults) _loadDefaults();
387  }
388 
389  void add(const std::type_info& tp, factoryFuncPtr func) {
390  _factFuncs[tp.name()] = func;
391  }
392 
394  const std::string& name) const
395  {
396  Lookup::const_iterator fi = _factFuncs.find(prop.typeOf(name).name());
397  return (fi == _factFuncs.end()) ? 0 : (*(fi->second))(prop, name);
398  }
399 
400 private:
401  void _loadDefaults();
402 
403  typedef std::map<std::string, factoryFuncPtr> Lookup;
405 };
406 
452 public:
453 
458 
465 
469  PropertyPrinter(const lsst::daf::base::PropertySet& prop, const std::string& name,
471 
475  iterator begin();
476 
480  iterator last();
481 
485  size_t valueCount() { return _list.get()->valueCount(); }
486 
487 
488 private:
489  boost::shared_ptr<PrinterList> _list;
490 };
491 
492 }}} // end lsst::pex::logging
493 
494 #endif // end LSST_PEX_PROPERTYPRINTER_H
495 
virtual bool operator==(const PrinterIter &that) const
int iter
BoolPrinterIter(std::vector< bool >::const_iterator listiter, std::vector< bool >::const_iterator beginiter, std::vector< bool >::const_iterator enditer)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
an abstract iterator class used to print out property values
virtual bool operator==(const PrinterIter &that) const
PropertyPrinter(const lsst::daf::base::PropertySet &prop, const std::string &name, const PrinterFactory &fact=defaultPrinterFactory)
virtual iterator last() const
virtual PrinterIter & operator--()
virtual std::ostream & write(std::ostream *strm) const
void add(const std::type_info &tp, factoryFuncPtr func)
virtual iterator begin() const
PrinterList * makeBoolPrinter(const lsst::daf::base::PropertySet &prop, const std::string &name)
TmplPrinterIter(typename std::vector< T >::const_iterator listiter, typename std::vector< T >::const_iterator beginiter, typename std::vector< T >::const_iterator enditer)
virtual bool operator!=(const PrinterIter &that) const =0
virtual PrinterIter & operator++()=0
DateTimePrinterList(const lsst::daf::base::PropertySet &prop, const std::string &name)
PrinterList * makePrinter(const lsst::daf::base::PropertySet &prop, const std::string &name)
the template factory function for supporting printing of printable types
an class for printing the values associated with a name in a PropertySet.
virtual bool operator==(const PrinterIter &that) const =0
PrinterList * makeDateTimePrinter(const lsst::daf::base::PropertySet &prop, const std::string &name)
virtual iterator begin() const =0
PrinterFactory(bool loadDefaults=false)
virtual iterator last() const =0
virtual std::ostream & write(std::ostream *strm) const =0
virtual bool operator!=(const PrinterIter &that) const
virtual std::ostream & write(std::ostream *strm) const
PrinterList * makePrinter(const lsst::daf::base::PropertySet &prop, const std::string &name) const
virtual std::ostream & write(std::ostream *strm) const
std::vector< T >::const_iterator _it
virtual bool operator!=(const PrinterIter &that) const
boost::shared_ptr< PrinterList > _list
std::map< std::string, factoryFuncPtr > Lookup
bool operator==(const BaseTmplPrinterIter &that) const
Interface for DateTime class.
PrinterList *(* factoryFuncPtr)(const lsst::daf::base::PropertySet &, const std::string &)
virtual std::ostream & write(std::ostream *strm) const
BaseTmplPrinterIter(typename std::vector< T >::const_iterator listiter, typename std::vector< T >::const_iterator beginiter, typename std::vector< T >::const_iterator enditer)
std::vector< T >::const_iterator _begin
virtual iterator begin() const
an abstract class that encapsulates a list of property values to be printed out.
std::type_info const & typeOf(std::string const &name) const
Definition: PropertySet.cc:227
boost::shared_ptr< PrinterIter > _it
virtual PrinterIter & operator++()
Class for storing generic metadata.
Definition: PropertySet.h:82
static PrinterFactory defaultPrinterFactory
std::vector< T >::const_iterator _end
DateTimePrinterIter(std::vector< lsst::daf::base::DateTime >::const_iterator listiter, std::vector< lsst::daf::base::DateTime >::const_iterator beginiter, std::vector< lsst::daf::base::DateTime >::const_iterator enditer)
virtual PrinterIter & operator--()=0
Interface for PropertySet class.
BoolPrinterList(const lsst::daf::base::PropertySet &prop, const std::string &name)
bool operator!=(const BaseTmplPrinterIter &that) const
TmplPrinterList(const lsst::daf::base::PropertySet &prop, const std::string &name)
virtual size_t valueCount() const =0
BaseTmplPrinterList(const lsst::daf::base::PropertySet &prop, const std::string &name)
WrappedPrinterIter(boost::shared_ptr< PrinterIter > iter)
virtual iterator last() const
virtual PrinterList::iterator last() const
virtual bool notAtEnd() const =0
virtual PrinterList::iterator begin() const
a wrapper PrinterIter class that hides the polymorphic (and possibly templatized) nature of an underl...
const std::string operator*() const
virtual bool notLTBegin() const =0
a factory used to create PrinterList instances to be used by a PropertyPrinter instance.