cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
N/A
Central exclusive processes event generator
Toggle main menu visibility
Main Page
Related Pages
Packages
Package List
Package Members
All
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
x
y
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
x
y
Variables
Typedefs
Enumerations
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
p
q
r
s
t
u
v
w
x
y
z
Typedefs
Enumerations
Enumerator
b
c
d
e
g
h
i
l
m
n
p
r
t
u
w
x
y
z
Related Symbols
d
g
h
o
u
v
Files
File List
File Members
All
_
a
b
c
d
e
h
o
p
r
s
Functions
Variables
Macros
_
b
c
d
e
p
r
s
▼
CepGen
Reference manual
Bibliography
►
Packages
►
Classes
▼
Files
▼
File List
▼
include
►
CepGen
►
CepGenBoost
►
CepGenHepMC2
►
CepGenHepMC3
►
CepGenHerwig6
►
CepGenMadGraph
►
CepGenPythia6
►
CepGenPythia8
▼
CepGenPython
►
ConfigWriter.h
►
Environment.h
►
Error.h
►
Functional.h
►
ObjectPtr.h
►
Utils.h
►
CepGenRoot
►
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
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 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
32
namespace
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
}
51
static
inline
ObjectPtr
make
(
const
std::vector<T>& vec) {
…
}
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
}
76
static
inline
ObjectPtr
tuple
(
const
std::tuple<Args...>& c_tuple) {
…
}
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
}
92
inline
ObjectPtr
operator()
(Args&&... args) {
…
}
95
template
<
typename
T>
96
ObjectPtr
call
(
const
T&)
const
;
97
ObjectPtr
call
(
const
ObjectPtr
&)
const
;
98
ObjectPtr
attribute
(
const
std::string&)
const
;
99
};
35
class
ObjectPtr
:
public
PyObjectPtr {
…
};
100
}
// namespace cepgen::python
101
102
#endif
cepgen::python::ObjectPtr
Smart pointer to a Python object and its dereferencing operator.
Definition
ObjectPtr.h:35
cepgen::python::ObjectPtr::isVector
bool isVector() const
Check if a Python object is compatible with a vector of uniform objects.
cepgen::python::ObjectPtr::importModule
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
cepgen::python::ObjectPtr::attribute
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
cepgen::python::ObjectPtr::tupleFromVector
static ObjectPtr tupleFromVector(const std::vector< T > &)
Build a Python tuple from a (uniform) vector of objects.
cepgen::python::ObjectPtr::vector
std::vector< T > vector() const
Retrieve a vector of objects, either from a Python list or tuple.
cepgen::python::ObjectPtr::operator()
ObjectPtr operator()(Args &&... args)
Call a python function with an uncounted set of arguments.
Definition
ObjectPtr.h:92
cepgen::python::ObjectPtr::operator<<
friend std::ostream & operator<<(std::ostream &, const ObjectPtr &)
cepgen::python::ObjectPtr::value
T value() const
Cast a Python object into a C++ type.
cepgen::python::ObjectPtr::make
static ObjectPtr make(const std::vector< T > &vec)
Build a new Python list from a C++ STL vector.
Definition
ObjectPtr.h:51
cepgen::python::ObjectPtr::tuple
static ObjectPtr tuple(const std::tuple< Args... > &c_tuple)
Build a Python tuple from a C++ tuple.
Definition
ObjectPtr.h:76
cepgen::python::ObjectPtr::ObjectPtr
ObjectPtr(PyObject *, bool wrap_only=false)
cepgen::python::ObjectPtr::make
static ObjectPtr make(const T &)
Build a new Python object from a C++ one.
cepgen::python::ObjectPtr::wrap
static ObjectPtr wrap(PyObject *)
Wrap a PyObject without cleaning at the destructor.
cepgen::python::ObjectPtr::call
ObjectPtr call(const T &) const
Call a python function with a single argument.
cepgen::python::ObjectPtr::call
ObjectPtr call(const ObjectPtr &) const
Call a python function with a tuple of arguments.
cepgen::python::ObjectPtr::is
bool is() const
Check if a Python object holds a given C++ type.
cepgen::python::ObjectPtr::defineModule
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.
cepgen::python
Definition
ConfigWriter.h:31
include
CepGenPython
ObjectPtr.h
Generated on Tue Apr 22 2025 for CepGen by
1.10.0