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
PropertySetFormatter.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 
37 #include <string>
38 #include <vector>
39 
40 #include <boost/serialization/nvp.hpp>
41 #include <boost/serialization/shared_ptr.hpp>
42 
44 #include "lsst/daf/base/DateTime.h"
45 #include <lsst/pex/exceptions.h>
46 #include <lsst/pex/logging/Trace.h>
47 
48 
49 using boost::serialization::make_nvp;
50 
51 namespace lsst {
52 namespace daf {
53 namespace persistence {
54 
55 namespace dafBase = lsst::daf::base;
56 
57 namespace {
58 
64 template <class Archive, typename T>
65 static void serializeItem(Archive& ar, std::string const& name,
67  std::vector<T> value;
68  if (Archive::is_saving::value) {
69  value = ps->getArray<T>(name);
70  ar & make_nvp("value", value);
71  }
72  else {
73  ar & make_nvp("value", value);
74  ps->set(name, value);
75  }
76 }
77 
83 template <class Archive>
84 static void serializeDateTime(Archive& ar, std::string const& name,
86  std::vector<dafBase::DateTime> value;
87  std::vector<long long> nsecs;
88  if (Archive::is_saving::value) {
89  value = ps->getArray<dafBase::DateTime>(name);
90  for (std::vector<dafBase::DateTime>::const_iterator i = value.begin();
91  i != value.end(); ++i) {
92  nsecs.push_back(i->nsecs());
93  }
94  ar & make_nvp("value", nsecs);
95  }
96  else {
97  ar & make_nvp("value", nsecs);
98  for (std::vector<long long>::const_iterator i = nsecs.begin();
99  i != nsecs.end(); ++i) {
100  value.push_back(dafBase::DateTime(*i));
101  }
102  ps->set(name, value);
103  }
104 }
105 
106 } // anonymous namespace
107 
114 template <class Archive>
116  Archive& ar, unsigned int const version, dafBase::Persistable* persistable) {
118  dynamic_cast<dafBase::PropertySet*>(persistable);
119  if (ps == 0) {
120  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-PropertySet");
121  }
122  ar & make_nvp("base",
123  boost::serialization::base_object<dafBase::Persistable>(*ps));
124 
125  std::vector<std::string> names;
126  size_t nNames;
127  if (Archive::is_saving::value) {
128  names = ps->paramNames(false);
129  nNames = names.size();
130  }
131  ar & make_nvp("nitems", nNames);
132 
133  char type;
134  std::string name;
135  for (size_t i = 0; i < nNames; ++i) {
136  if (Archive::is_saving::value) {
137  name = names[i];
138  std::type_info const& id(ps->typeOf(name));
139 
140  if (id == typeid(bool)) type = 'b';
141  else if (id == typeid(char)) type = 'c';
142  else if (id == typeid(signed char)) type = 'y';
143  else if (id == typeid(unsigned char)) type = 'C';
144  else if (id == typeid(short)) type = 'w';
145  else if (id == typeid(unsigned short)) type = 'W';
146  else if (id == typeid(int)) type = 'i';
147  else if (id == typeid(unsigned int)) type = 'I';
148  else if (id == typeid(long)) type = 'l';
149  else if (id == typeid(unsigned long)) type = 'L';
150  else if (id == typeid(long long)) type = 'x';
151  else if (id == typeid(unsigned long long)) type = 'X';
152  else if (id == typeid(float)) type = 'f';
153  else if (id == typeid(double)) type = 'd';
154  else if (id == typeid(std::string)) type = 's';
155  else if (id == typeid(dafBase::DateTime)) type = 'T';
156  else if (id == typeid(dafBase::Persistable::Ptr)) type = 'p';
157  else {
158  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
159  name +
160  ": Unknown type in PropertySet serialize");
161  }
162  }
163 
164  ar & make_nvp("name", name);
165  ar & make_nvp("type", type);
166  switch (type) {
167  case 'b': serializeItem<Archive, bool>(ar, name, ps); break;
168  case 'c': serializeItem<Archive, char>(ar, name, ps); break;
169  case 'y': serializeItem<Archive, signed char>(ar, name, ps); break;
170  case 'C': serializeItem<Archive, unsigned char>(ar, name, ps); break;
171  case 'w': serializeItem<Archive, short>(ar, name, ps); break;
172  case 'W': serializeItem<Archive, unsigned short>(ar, name, ps); break;
173  case 'i': serializeItem<Archive, int>(ar, name, ps); break;
174  case 'I': serializeItem<Archive, unsigned int>(ar, name, ps); break;
175  case 'l': serializeItem<Archive, long>(ar, name, ps); break;
176  case 'L': serializeItem<Archive, unsigned long>(ar, name, ps); break;
177  case 'x': serializeItem<Archive, long long>(ar, name, ps); break;
178  case 'X': serializeItem<Archive, unsigned long long>(ar, name, ps); break;
179  case 'f': serializeItem<Archive, float>(ar, name, ps); break;
180  case 'd': serializeItem<Archive, double>(ar, name, ps); break;
181  case 's': serializeItem<Archive, std::string>(ar, name, ps); break;
182  case 'T': serializeDateTime<Archive>(ar, name, ps); break;
183  case 'p': serializeItem<Archive, dafBase::Persistable::Ptr>(ar, name, ps); break;
184  default:
185  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
186  std::string("Unknown type reading PropertySet") +
187  type + ", name = " + name);
188  }
189  }
190 }
191 
192 }}} // namespace lsst::daf::persistence
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:142
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Include files required for standard LSST Exception handling.
definition of the Trace messaging facilities
void set(std::string const &name, T const &value)
Definition: PropertySet.cc:581
boost::shared_ptr< Persistable > Ptr
Definition: Persistable.h:76
Interface for DateTime class.
static void delegateSerialize(Archive &ar, unsigned int const version, dafBase::Persistable *persistable)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::type_info const & typeOf(std::string const &name) const
Definition: PropertySet.cc:227
Class for storing generic metadata.
Definition: PropertySet.h:82
int id
Definition: CR.cc:151
Interface for PropertySet class.
Base class for all persistable classes.
Definition: Persistable.h:74
std::vector< T > getArray(std::string const &name) const