lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Persistable.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_IO_Persistable_h_INCLUDED
3 #define AFW_TABLE_IO_Persistable_h_INCLUDED
4 
5 #include <climits>
6 #include "lsst/base.h"
7 #include "lsst/pex/exceptions.h"
8 
9 namespace lsst {
10 namespace afw {
11 
12 namespace fits {
13 
14 class Fits;
15 class MemFileManager;
16 
17 } // namespace fits
18 
19 namespace table {
20 namespace io {
21 
22 class InputArchive;
23 class OutputArchive;
24 class OutputArchiveHandle;
25 class CatalogVector;
26 
30 LSST_EXCEPTION_TYPE(PersistenceError, lsst::pex::exceptions::IoError, lsst::afw::table::io::PersistenceError)
31 
32 
37 LSST_EXCEPTION_TYPE(MalformedArchiveError, lsst::afw::table::io::PersistenceError,
38  lsst::afw::table::io::MalformedArchiveError)
39 
47 #define LSST_ARCHIVE_ASSERT(EXPR) \
48  if (!(EXPR)) \
49  throw LSST_EXCEPT(lsst::afw::table::io::MalformedArchiveError, "Archive assertion failed: " #EXPR)
50 
73 class Persistable {
74 public:
82  void writeFits(std::string const& fileName, std::string const& mode = "w") const;
83 
91  void writeFits(fits::MemFileManager& manager, std::string const& mode = "w") const;
92 
98  void writeFits(fits::Fits& fitsfile) const;
99 
101  virtual bool isPersistable() const { return false; }
102 
103  virtual ~Persistable() {}
104 
105 protected:
106  // convenient for derived classes not in afw::table::io
108 
114  virtual std::string getPersistenceName() const;
115 
124  virtual std::string getPythonModule() const;
125 
133  virtual void write(OutputArchiveHandle& handle) const;
134 
136 
137  Persistable(Persistable const& other) {}
138 
139  void operator=(Persistable const& other) {}
140 
141 private:
142  friend class io::OutputArchive;
143  friend class io::InputArchive;
144 
145  template <typename T>
146  friend class PersistableFacade;
147 
148  static std::shared_ptr<Persistable> _readFits(std::string const& fileName, int hdu = INT_MIN);
149 
150  static std::shared_ptr<Persistable> _readFits(fits::MemFileManager& manager, int hdu = INT_MIN);
151 
152  static std::shared_ptr<Persistable> _readFits(fits::Fits& fitsfile);
153 };
154 
172 template <typename T>
174 public:
180  static std::shared_ptr<T> readFits(fits::Fits& fitsfile) {
181  return std::dynamic_pointer_cast<T>(Persistable::_readFits(fitsfile));
182  }
183 
191  static std::shared_ptr<T> readFits(std::string const& fileName, int hdu = INT_MIN) {
192  return std::dynamic_pointer_cast<T>(Persistable::_readFits(fileName, hdu));
193  }
194 
202  static std::shared_ptr<T> readFits(fits::MemFileManager& manager, int hdu = INT_MIN) {
203  return std::dynamic_pointer_cast<T>(Persistable::_readFits(manager, hdu));
204  }
205 };
206 
215 protected:
216  typedef io::InputArchive InputArchive; // convenient for derived classes not in afw::table::io
218 
219 public:
231  explicit PersistableFactory(std::string const& name);
232 
234  virtual std::shared_ptr<Persistable> read(InputArchive const& archive,
235  CatalogVector const& catalogs) const = 0;
236 
244  static PersistableFactory const& lookup(std::string const& name, std::string const& module = "");
245 
246  virtual ~PersistableFactory() {}
247 
248  // No copying
249  PersistableFactory(const PersistableFactory&) = delete;
251 
252  // No moving
255 };
256 }
257 }
258 }
259 } // namespace lsst::afw::table::io
260 
261 #endif // !AFW_TABLE_IO_Persistable_h_INCLUDED
static std::shared_ptr< T > readFits(std::string const &fileName, int hdu=INT_MIN)
Read an object from a regular FITS file.
Definition: Persistable.h:191
PersistableFactory(std::string const &name)
Constructor for the factory.
An object passed to Persistable::write to allow it to persist itself.
PersistableFactory & operator=(const PersistableFactory &)=delete
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
A base class for factory classes used to reconstruct objects from records.
Definition: Persistable.h:214
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
virtual std::shared_ptr< Persistable > read(InputArchive const &archive, CatalogVector const &catalogs) const =0
Construct a new object from the given InputArchive and vector of catalogs.
A multi-catalog archive object used to save table::io::Persistable objects.
Definition: OutputArchive.h:34
Persistable(Persistable const &other)
Definition: Persistable.h:137
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
Definition: Persistable.h:101
void operator=(Persistable const &other)
Definition: Persistable.h:139
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:101
static std::shared_ptr< T > readFits(fits::MemFileManager &manager, int hdu=INT_MIN)
Read an object from a FITS file in memory.
Definition: Persistable.h:202
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
io::OutputArchiveHandle OutputArchiveHandle
Definition: Persistable.h:107
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
static std::shared_ptr< T > readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
Definition: Persistable.h:180
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:173
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
static PersistableFactory const & lookup(std::string const &name, std::string const &module="")
Return the factory that has been registered with the given name.
An exception thrown when problems occur during persistence.
Definition: Persistable.h:30
An exception thrown when an InputArchive&#39;s contents do not make sense.
Definition: Persistable.h:38