LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
Public Member Functions | Protected Types | Static Protected Member Functions | Related Functions | List of all members
lsst::afw::typehandling::StorableHelper< Base > Class Template Reference

"Trampoline" for Storable to let it be used as a base class in Python. More...

#include <python.h>

Inheritance diagram for lsst::afw::typehandling::StorableHelper< Base >:
lsst::afw::typehandling::Storable lsst::afw::table::io::Persistable lsst::afw::detection::PsfTrampoline< Base > lsst::meas::algorithms::ImagePsfTrampoline< Base >

Public Member Functions

template<typename... Args>
 StorableHelper (Args... args)
 Delegating constructor for wrapped class. More...
 
std::shared_ptr< StorablecloneStorable () const override
 Create a new object that is a copy of this one (optional operation). More...
 
std::string toString () const override
 Create a string representation of this object (optional operation). More...
 
std::size_t hash_value () const override
 Return a hash of this object (optional operation). More...
 
bool equals (Storable const &other) const noexcept override
 Compare this object to another Storable. More...
 
bool isPersistable () const noexcept override
 Return true if this particular object can be persisted using afw::table::io. More...
 
std::string getPersistenceName () const override
 Return the unique name used to persist this object and look up its factory. More...
 
std::string getPythonModule () const override
 Return the fully-qualified Python module that should be imported to guarantee that its factory is registered. More...
 
void write (table::io::OutputArchiveHandle &handle) const override
 Write the object to one or more catalogs. More...
 
void writeFits (std::string const &fileName, std::string const &mode="w") const
 Write the object to a regular FITS file. More...
 
void writeFits (fits::MemFileManager &manager, std::string const &mode="w") const
 Write the object to a FITS image in memory. More...
 
void writeFits (fits::Fits &fitsfile) const
 Write the object to an already-open FITS object. More...
 

Protected Types

using OutputArchiveHandle = io::OutputArchiveHandle
 

Static Protected Member Functions

template<class T >
static bool singleClassEquals (T const &lhs, Storable const &rhs)
 Test if a Storable is of a particular class and equal to another object. More...
 

Related Functions

(Note that these are not member functions.)

std::ostreamoperator<< (std::ostream &os, Storable const &storable)
 Output operator for Storable. More...
 

Detailed Description

template<class Base = Storable>
class lsst::afw::typehandling::StorableHelper< Base >

"Trampoline" for Storable to let it be used as a base class in Python.

Subclasses of Storable that are wrapped in pybind11 should have a similar helper that subclasses StorableHelper<subclass>. This helper can be skipped if the subclass neither adds any virtual methods nor implements any abstract methods.

Template Parameters
Basethe exact (most specific) class being wrapped
See also
pybind11 documentation

Definition at line 54 of file python.h.

Member Typedef Documentation

◆ OutputArchiveHandle

using lsst::afw::table::io::Persistable::OutputArchiveHandle = io::OutputArchiveHandle
protectedinherited

Definition at line 108 of file Persistable.h.

Constructor & Destructor Documentation

◆ StorableHelper()

template<class Base = Storable>
template<typename... Args>
lsst::afw::typehandling::StorableHelper< Base >::StorableHelper ( Args...  args)
inlineexplicit

Delegating constructor for wrapped class.

While we would like to simply inherit base class constructors, when doing so, we cannot change their access specifiers. One consequence is that it's not possible to use inheritance to expose a protected constructor to python. The alternative, used here, is to create a new public constructor that delegates to the base class public or protected constructor with the same signature.

Template Parameters
ArgsVariadic type specification
Parameters
...argsArguments to forward to the Base class constructor.

Definition at line 193 of file python.h.

Member Function Documentation

◆ cloneStorable()

template<class Base = Storable>
std::shared_ptr<Storable> lsst::afw::typehandling::StorableHelper< Base >::cloneStorable ( ) const
inlineoverridevirtual

Create a new object that is a copy of this one (optional operation).

This operation is required for Storables that are stored in GenericMap by value, but not for those stored by shared pointer.

Exceptions
UnsupportedOperationExceptionThrown if this object is not cloneable.
Note
If this class supports a clone operation, the two should behave identically except for the formal return type.
When called on Python classes, this method delegates to __deepcopy__ if it exists.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 73 of file python.h.

73  {
74  /* __deepcopy__ takes an optional dict, but PYBIND11_OVERLOAD_* won't
75  * compile unless you give it arguments that work for the C++ method
76  */
77  PYBIND11_OVERLOAD_NAME(std::shared_ptr<Storable>, Base, "__deepcopy__", cloneStorable, );
78  }
std::shared_ptr< Storable > cloneStorable() const override
Create a new object that is a copy of this one (optional operation).
Definition: python.h:73

◆ equals()

template<class Base = Storable>
bool lsst::afw::typehandling::StorableHelper< Base >::equals ( Storable const &  other) const
inlineoverridevirtualnoexcept

Compare this object to another Storable.

Subclasses that implement equality comparison must override this method to give results consistent with operator== for all inputs that are accepted by both.

Returns
This implementation returns whether the two objects are the same.
Warning
This method compares an object to any type of Storable, although cross-class comparisons should usually return false. If cross-class comparisons are valid, implementers should take care that they are symmetric and will give the same result no matter what the compile-time types of the left- and right-hand sides are.
See also
singleClassEquals
Note
When called on Python classes, this method delegates to __eq__ if it exists.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 88 of file python.h.

88  {
89  PYBIND11_OVERLOAD_NAME(bool, Base, "__eq__", equals, other);
90  }
bool equals(Storable const &other) const noexcept override
Compare this object to another Storable.
Definition: python.h:88

◆ getPersistenceName()

template<class Base = Storable>
std::string lsst::afw::typehandling::StorableHelper< Base >::getPersistenceName ( ) const
inlineoverridevirtual

Return the unique name used to persist this object and look up its factory.

Must be less than ArchiveIndexSchema::MAX_NAME_LENGTH characters.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 98 of file python.h.

98  {
99  PYBIND11_OVERLOAD_NAME(
100  std::string, Base, "_getPersistenceName", getPersistenceName
101  );
102  }
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: python.h:98

◆ getPythonModule()

template<class Base = Storable>
std::string lsst::afw::typehandling::StorableHelper< Base >::getPythonModule ( ) const
inlineoverridevirtual

Return the fully-qualified Python module that should be imported to guarantee that its factory is registered.

Must be less than ArchiveIndexSchema::MAX_MODULE_LENGTH characters.

Will be ignored if empty.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 104 of file python.h.

104  {
105  PYBIND11_OVERLOAD_NAME(
106  std::string, Base, "_getPythonModule", getPythonModule
107  );
108  }
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: python.h:104

◆ hash_value()

template<class Base = Storable>
std::size_t lsst::afw::typehandling::StorableHelper< Base >::hash_value ( ) const
inlineoverridevirtual

Return a hash of this object (optional operation).

Exceptions
UnsupportedOperationExceptionThrown if this object is not hashable.
Note
C++ subclass authors are responsible for any associated specializations of std::hash.
When called on Python classes, this method delegates to __hash__ if it exists.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 84 of file python.h.

84  {
85  PYBIND11_OVERLOAD_NAME(std::size_t, Base, "__hash__", hash_value, );
86  }
std::size_t hash_value() const override
Return a hash of this object (optional operation).
Definition: python.h:84

◆ isPersistable()

template<class Base = Storable>
bool lsst::afw::typehandling::StorableHelper< Base >::isPersistable ( ) const
inlineoverridevirtualnoexcept

Return true if this particular object can be persisted using afw::table::io.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 92 of file python.h.

92  {
93  PYBIND11_OVERLOAD(
94  bool, Base, isPersistable
95  );
96  }
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: python.h:92

◆ singleClassEquals()

template<class T >
static bool lsst::afw::typehandling::Storable::singleClassEquals ( T const &  lhs,
Storable const &  rhs 
)
inlinestaticprotectedinherited

Test if a Storable is of a particular class and equal to another object.

This method template simplifies implementations of equals that delegate to operator== without supporting cross-class comparisons.

Template Parameters
TThe class expected of the two objects to be compared.
Parameters
lhs,rhsThe objects to compare. Note that rhs need not be a T, while lhs must be.
Returns
true if rhs is a T and lhs == rhs; false otherwise.
Exception Safety
Provides the same level of exception safety as operator==. Most implementations of operator== do not throw.
Note
This method template calls operator== with both arguments of compile-time type T const&. Its use is not recommended if there would be any ambiguity as to which operator== gets picked by overload resolution.

This method template is typically called from equals as:

bool MyType::equals(Storable const& other) const noexcept {
    return singleClassEquals(*this, other);
}

Definition at line 151 of file Storable.h.

151  {
152  auto typedRhs = dynamic_cast<T const*>(&rhs);
153  if (typedRhs != nullptr) {
154  return lhs == *typedRhs;
155  } else {
156  return false;
157  }
158  }

◆ toString()

template<class Base = Storable>
std::string lsst::afw::typehandling::StorableHelper< Base >::toString ( ) const
inlineoverridevirtual

Create a string representation of this object (optional operation).

Exceptions
UnsupportedOperationExceptionThrown if this object does not have a string representation.
Note
When called on Python classes, this method delegates to __repr__.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 80 of file python.h.

80  {
81  PYBIND11_OVERLOAD_NAME(std::string, Base, "__repr__", toString, );
82  }
std::string toString() const override
Create a string representation of this object (optional operation).
Definition: python.h:80

◆ write()

template<typename Base >
void StorableHelper::write ( table::io::OutputArchiveHandle handle) const
overridevirtual

Write the object to one or more catalogs.

The handle object passed to this function provides an interface for adding new catalogs and adding nested objects to the same archive (while checking for duplicates). See OutputArchiveHandle for more information.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 193 of file python.h.

193  {
194  pybind11::gil_scoped_acquire gil;
195  pybind11::function overload = pybind11::get_overload(static_cast<const Base *>(this), "_write");
196  if (!overload)
197  throw std::runtime_error("Cannot find StorableHelper _write overload");
198  auto o = overload().cast<std::string>();
199  auto const &keys = StorableHelperPersistenceHelper::get();
200  table::BaseCatalog cat = handle.makeCatalog(keys.schema);
201  std::shared_ptr<table::BaseRecord> record = cat.addNew();
202  record->set(keys.bytes, formatters::stringToBytes(o));
203  handle.saveCatalog(cat);
204 }
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:161
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:72

◆ writeFits() [1/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::Fits fitsfile) const
inherited

Write the object to an already-open FITS object.

Parameters
[in]fitsfileOpen FITS object to write to.

Definition at line 18 of file Persistable.cc.

18  {
19  OutputArchive archive;
20  archive.put(this);
21  archive.writeFits(fitsfile);
22 }

◆ writeFits() [2/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::MemFileManager manager,
std::string const &  mode = "w" 
) const
inherited

Write the object to a FITS image in memory.

Parameters
[in]managerName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 29 of file Persistable.cc.

29  {
30  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
31  writeFits(fitsfile);
32 }
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
Definition: Persistable.cc:24

◆ writeFits() [3/3]

void lsst::afw::table::io::Persistable::writeFits ( std::string const &  fileName,
std::string const &  mode = "w" 
) const
inherited

Write the object to a regular FITS file.

Parameters
[in]fileNameName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 24 of file Persistable.cc.

24  {
25  fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
26  writeFits(fitsfile);
27 }

Friends And Related Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream os,
Storable const &  storable 
)
related

Output operator for Storable.

Parameters
osthe desired output stream
storablethe object to print
Returns
a reference to os
Exceptions
UnsupportedOperationExceptionThrown if storable does not have an implementation of Storable::toString.

Definition at line 174 of file Storable.h.

174  {
175  return os << storable.toString();
176 }
std::ostream * os
Definition: Schema.cc:557

The documentation for this class was generated from the following file: