24 #ifndef LSST_UTILS_PYTHON_H 25 #define LSST_UTILS_PYTHON_H 27 #include "pybind11/pybind11.h" 35 #include "lsst/pex/exceptions.h" 57 template<
typename T,
typename PyClass>
60 [](std::shared_ptr<T>
self, std::shared_ptr<T> other) {
return self.get() == other.get(); },
61 pybind11::is_operator());
63 [](std::shared_ptr<T>
self, std::shared_ptr<T> other) {
return self.get() != other.get(); },
64 pybind11::is_operator());
79 inline std::size_t
cppIndex(std::ptrdiff_t size, std::ptrdiff_t i) {
80 auto const i_orig = i;
85 if (i < 0 || i >= size) {
86 std::ostringstream os;
87 os <<
"Index " << i_orig <<
" not in range [" << -size <<
", " << size - 1 <<
"]";
88 throw pybind11::index_error(os.str());
90 return static_cast<std::size_t
>(i);
105 inline std::pair<std::size_t, std::size_t>
cppIndex(std::ptrdiff_t size_i, std::ptrdiff_t size_j,
106 std::ptrdiff_t i, std::ptrdiff_t j) {
109 }
catch (lsst::pex::exceptions::OutOfRangeError) {
110 std::ostringstream os;
111 os <<
"Index (" << i <<
", " << j <<
") not in range [" 112 << -size_i <<
", " << size_i - 1 <<
"], [" 113 << -size_j <<
", " << size_j - 1 <<
"]";
114 throw pybind11::index_error(os.str());
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
Compute a C++ index from a Python index (negative values count from the end) and range-check.
void addSharedPtrEquality(PyClass &cls)
Add __eq__ and __ne__ methods based on two std::shared_ptr<T> pointing to the same address...