lsst.utils  13.0-7-gd7d2357+2
 All Classes Namespaces Files Functions Variables Groups Pages
python.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * See COPYRIGHT file at the top of the source tree.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef LSST_UTILS_PYTHON_H
25 #define LSST_UTILS_PYTHON_H
26 
27 #include "pybind11/pybind11.h"
28 
29 #include <cstddef>
30 #include <memory>
31 #include <string>
32 #include <sstream>
33 #include <utility>
34 
35 #include "lsst/pex/exceptions.h"
36 
37 namespace lsst {
38 namespace utils {
39 namespace python {
40 
57 template<typename T, typename PyClass>
58 inline void addSharedPtrEquality(PyClass & cls) {
59  cls.def("__eq__",
60  [](std::shared_ptr<T> self, std::shared_ptr<T> other) { return self.get() == other.get(); },
61  pybind11::is_operator());
62  cls.def("__ne__",
63  [](std::shared_ptr<T> self, std::shared_ptr<T> other) { return self.get() != other.get(); },
64  pybind11::is_operator());
65 }
66 
79 inline std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i) {
80  auto const i_orig = i;
81  if (i < 0) {
82  // index backwards from the end
83  i += size;
84  }
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());
89  }
90  return static_cast<std::size_t>(i);
91 }
92 
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) {
107  try {
108  return {cppIndex(size_i, i), cppIndex(size_j, 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());
115  }
116 }
117 
118 }}} // namespace lsst::utils::python
119 
120 #endif
121 
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.
Definition: python.h:79
void addSharedPtrEquality(PyClass &cls)
Add __eq__ and __ne__ methods based on two std::shared_ptr&lt;T&gt; pointing to the same address...
Definition: python.h:58