cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
ObjectPtr.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGenPython_ObjectPtr_h
20#define CepGenPython_ObjectPtr_h
21
22#include <Python.h>
23
24#if PY_MAJOR_VERSION < 3
25#define PYTHON2
26#endif
27
28#include <memory>
29#include <string>
30#include <vector>
31
32namespace cepgen::python {
33 typedef std::unique_ptr<PyObject, void (*)(PyObject*)> PyObjectPtr;
35 class ObjectPtr : public PyObjectPtr {
36 public:
37 explicit ObjectPtr(PyObject*, bool wrap_only = false);
38
40 static ObjectPtr wrap(PyObject*);
42 static ObjectPtr importModule(const std::string&);
44 static ObjectPtr defineModule(const std::string&, const std::string&);
45
47 template <typename T>
48 static ObjectPtr make(const T&);
50 template <typename T>
51 static inline ObjectPtr make(const std::vector<T>& vec) {
52 ObjectPtr list(PyList_New(vec.size()));
53 for (size_t i = 0; i < vec.size(); ++i)
54 PyList_SetItem(list.get(), i, make(vec.at(i)).release());
55 return list;
56 }
57
59 template <typename T>
60 bool is() const;
62 template <typename T>
63 T value() const;
65 template <typename T>
66 bool isVector() const;
68 template <typename T>
69 std::vector<T> vector() const;
70
72 template <typename T>
73 static ObjectPtr tupleFromVector(const std::vector<T>&);
75 template <typename... Args>
76 static inline ObjectPtr tuple(const std::tuple<Args...>& c_tuple) {
77 const auto tuple_size = sizeof...(Args);
78 ObjectPtr tuple(PyTuple_New(tuple_size));
79 Py_ssize_t i = 0;
80 std::apply(
81 [&tuple, &i](auto... vals) {
82 ((PyTuple_SetItem(tuple.get(), i++, make<decltype(vals)>(vals).release())), ...);
83 },
84 c_tuple);
85 return tuple;
86 }
87
88 friend std::ostream& operator<<(std::ostream&, const ObjectPtr&);
89
91 template <typename... Args>
92 inline ObjectPtr operator()(Args&&... args) {
93 return call(tuple(std::make_tuple(std::forward<Args>(args)...))); // new
94 }
95 template <typename T>
96 ObjectPtr call(const T&) const;
97 ObjectPtr call(const ObjectPtr&) const;
98 ObjectPtr attribute(const std::string&) const;
99 };
100} // namespace cepgen::python
101
102#endif
Smart pointer to a Python object and its dereferencing operator.
Definition ObjectPtr.h:35
bool isVector() const
Check if a Python object is compatible with a vector of uniform objects.
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
static ObjectPtr tupleFromVector(const std::vector< T > &)
Build a Python tuple from a (uniform) vector of objects.
std::vector< T > vector() const
Retrieve a vector of objects, either from a Python list or tuple.
ObjectPtr operator()(Args &&... args)
Call a python function with an uncounted set of arguments.
Definition ObjectPtr.h:92
friend std::ostream & operator<<(std::ostream &, const ObjectPtr &)
T value() const
Cast a Python object into a C++ type.
static ObjectPtr make(const std::vector< T > &vec)
Build a new Python list from a C++ STL vector.
Definition ObjectPtr.h:51
static ObjectPtr tuple(const std::tuple< Args... > &c_tuple)
Build a Python tuple from a C++ tuple.
Definition ObjectPtr.h:76
ObjectPtr(PyObject *, bool wrap_only=false)
static ObjectPtr make(const T &)
Build a new Python object from a C++ one.
static ObjectPtr wrap(PyObject *)
Wrap a PyObject without cleaning at the destructor.
ObjectPtr call(const T &) const
Call a python function with a single argument.
ObjectPtr call(const ObjectPtr &) const
Call a python function with a tuple of arguments.
bool is() const
Check if a Python object holds a given C++ type.
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.