cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
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 CepGenAddOns_PythonWrapper_ObjectPtr_h
20#define CepGenAddOns_PythonWrapper_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 {
33 namespace python {
34 typedef std::unique_ptr<PyObject, void (*)(PyObject*)> PyObjectPtr;
36 class ObjectPtr : public PyObjectPtr {
37 public:
38 explicit ObjectPtr(PyObject*, bool wrap_only = false);
39
41 static ObjectPtr wrap(PyObject*);
43 static ObjectPtr importModule(const std::string&);
45 static ObjectPtr defineModule(const std::string&, const std::string&);
46
48 template <typename T>
49 static ObjectPtr make(const T&);
51 template <typename T>
52 static inline ObjectPtr make(const std::vector<T>& vec) {
53 ObjectPtr list(PyList_New(vec.size()));
54 for (size_t i = 0; i < vec.size(); ++i)
55 PyList_SetItem(list.get(), i, make(vec.at(i)).release());
56 return list;
57 }
58
60 template <typename T>
61 bool is() const;
63 template <typename T>
64 T value() const;
66 template <typename T>
67 bool isVector() const;
69 template <typename T>
70 std::vector<T> vector() const;
71
73 template <typename T>
74 static ObjectPtr tupleFromVector(const std::vector<T>&);
76 template <typename... Args>
77 static inline ObjectPtr tuple(const std::tuple<Args...>& c_tuple) {
78 const auto tuple_size = sizeof...(Args);
79 ObjectPtr tuple(PyTuple_New(tuple_size));
80 Py_ssize_t i = 0;
81 std::apply(
82 [&tuple, &i](auto... vals) {
83 ((PyTuple_SetItem(tuple.get(), i++, make<decltype(vals)>(vals).release())), ...);
84 },
85 c_tuple);
86 return tuple;
87 }
88
89 friend std::ostream& operator<<(std::ostream&, const ObjectPtr&);
90
92 template <typename... Args>
93 inline ObjectPtr operator()(Args&&... args) {
94 return call(tuple(std::make_tuple(std::forward<Args>(args)...))); // new
95 }
97 ObjectPtr call(const ObjectPtr&) const;
99 ObjectPtr attribute(const std::string&) const;
100 };
101 } // namespace python
102} // namespace cepgen
103
104#endif
Smart pointer to a Python object and its dereferencing operator.
Definition ObjectPtr.h:36
bool isVector() const
Check if a Python object is compatible with a vector of uniform objects.
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
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:93
friend std::ostream & operator<<(std::ostream &, const ObjectPtr &)
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
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:52
static ObjectPtr wrap(PyObject *)
Wrap a PyObject without cleaning at the destructor.
Definition ObjectPtr.cpp:45
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.
static ObjectPtr tuple(const std::tuple< Args... > &c_tuple)
Build a Python tuple from a C++ tuple.
Definition ObjectPtr.h:77
static ObjectPtr make(const T &)
Build a new Python object from a C++ one.
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 tupleFromVector(const std::vector< T > &)
Build a Python tuple from a (uniform) vector of objects.
std::unique_ptr< PyObject, void(*)(PyObject *)> PyObjectPtr
Definition ObjectPtr.h:34
Common namespace for this Monte Carlo generator.