ATLAS Offline Software
PyStoreCnvUtils.h
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // PyStoreCnvUtils.h
8 // Header file for a few C++->Py meta cnv utils
9 // Author: S.Binet<binet@cern.ch>
11 #ifndef PERFMONEVENT_PYSTORECNVUTILS_H
12 #define PERFMONEVENT_PYSTORECNVUTILS_H
13 
14 // Python includes
15 // Work around a compilation warning.
16 #ifdef _POSIX_C_SOURCE
17 # undef _POSIX_C_SOURCE
18 #endif
19 #include "Python.h"
20 
21 // STL includes
22 #include <string>
23 #include <vector>
24 #include <list>
25 #include <utility> // for std::pair
26 
27 // #include <iostream>
28 
29 namespace Py {
30 
32 template<typename CxxType>
33 struct cnv
34 {
35 // static PyObject* toPy( CxxType /*t*/ )
36 // {
37 // std::cerr << "ERR: no matching converter found for ["
38 // << typeid(CxxType).name()
39 // << "]\n";
40 // return NULL;
41 // }
42 };
43 
44 template<typename T, typename U>
45 struct cnv< std::pair<T,U> >
46 {
47  static PyObject* toPy( const std::pair<T,U>& t )
48  {
49  PyObject* o = PyTuple_New( 2 );
50  if ( !o ) {
51  Py_XDECREF( o );
52  Py_INCREF( Py_None );
53  return Py_None;
54  }
55  PyTuple_SET_ITEM( o, 0, Py::cnv<T>::toPy( t.first ) );
56  PyTuple_SET_ITEM( o, 1, Py::cnv<T>::toPy( t.second ) );
57  return o;
58  }
59 
60 };
61 
62 template<typename T>
63 struct cnv< std::vector<T> >
64 {
65  static PyObject* toPy( const std::vector<T>& t )
66  {
67  const std::size_t iMax = t.size();
68  PyObject* o = PyList_New( iMax );
69  if ( !o ) {
70  Py_XDECREF( o );
71  Py_INCREF( Py_None );
72  return Py_None;
73  }
74  for ( std::size_t i = 0; i < iMax; ++i ){
75  PyList_SET_ITEM( o, i, Py::cnv<T>::toPy( t[i] ) );
76  }
77  return o;
78  }
79 
80 };
81 
82 template<typename T>
83 struct cnv< std::list<T> >
84 {
85  static PyObject* toPy( const std::list<T>& t )
86  {
87  const std::size_t iMax = t.size();
88  PyObject* o = PyList_New( iMax );
89  if ( !o ) {
90  Py_XDECREF( o );
91  Py_INCREF( Py_None );
92  return Py_None;
93  }
94  typename std::list<T>::const_iterator itr = t.begin();
95  for ( std::size_t i = 0; i < iMax; ++i ){
96  PyList_SET_ITEM( o, i, Py::cnv<T>::toPy( *(itr++) ) );
97  }
98  return o;
99  }
100 
101 };
102 
103 template<>
104 struct cnv< std::string >
105 {
106  static PyObject* toPy( const std::string& t )
107  {
108  return Py_BuildValue( const_cast<char*>("s#"),
109  const_cast<char*>(t.c_str()),
110  t.size() );
111  }
112 };
113 
114 template<>
115 struct cnv<bool>
116 {
117  static PyObject* toPy( bool t )
118  { return Py_BuildValue( const_cast<char*>("b"), t ); }
119 };
120 
121 template<>
122 struct cnv<double>
123 {
124  static PyObject* toPy( double t )
125  { return PyFloat_FromDouble(t); }
126 };
127 
128 template<>
129 struct cnv<float>
130 {
131  static PyObject* toPy( float t )
132  { return PyFloat_FromDouble(t); }
133 };
134 
135 template<>
136 struct cnv<int>
137 {
138  static PyObject* toPy( int t )
139  { return Py_BuildValue( const_cast<char*>("i"), t ); }
140 };
141 
142 template<>
143 struct cnv<short int>
144 {
145  static PyObject* toPy( short int t )
146  { return Py_BuildValue( const_cast<char*>("h"), t ); }
147 };
148 
149 template<>
150 struct cnv<unsigned int>
151 {
152  static PyObject* toPy( unsigned int t )
153  { return Py_BuildValue( const_cast<char*>("l"), t ); }
154 };
155 
156 template<>
157 struct cnv<long int>
158 {
159  static PyObject* toPy( long int t )
160  { return Py_BuildValue( const_cast<char*>("l"), t ); }
161 };
162 
163 template<>
164 struct cnv<unsigned long>
165 {
166  static PyObject* toPy( unsigned long t )
167  { return Py_BuildValue( const_cast<char*>("k"), t ); }
168 };
169 
170 } // end namespace Py
171 
172 
173 #endif // PERFMONEVENT_PYSTORECNVUTILS_H
xAOD::short
short
Definition: Vertex_v1.cxx:165
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Py::cnv< std::pair< T, U > >::toPy
static PyObject * toPy(const std::pair< T, U > &t)
Definition: PyStoreCnvUtils.h:47
Py::cnv
helper method to convert a type to a Py::Object
Definition: PyDataStore.h:28
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
xAOD::unsigned
unsigned
Definition: RingSetConf_v1.cxx:662
Py::cnv< unsigned int >::toPy
static PyObject * toPy(unsigned int t)
Definition: PyStoreCnvUtils.h:152
Py::cnv< int >::toPy
static PyObject * toPy(int t)
Definition: PyStoreCnvUtils.h:138
lumiFormat.i
int i
Definition: lumiFormat.py:92
vector
Definition: MultiHisto.h:13
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
Py::cnv< std::string >::toPy
static PyObject * toPy(const std::string &t)
Definition: PyStoreCnvUtils.h:106
Py::cnv< std::list< T > >::toPy
static PyObject * toPy(const std::list< T > &t)
Definition: PyStoreCnvUtils.h:85
Py::cnv< short int >::toPy
static PyObject * toPy(short int t)
Definition: PyStoreCnvUtils.h:145
Py
Definition: PyDataStore.h:24
Py::cnv< double >::toPy
static PyObject * toPy(double t)
Definition: PyStoreCnvUtils.h:124
Py::cnv< std::vector< T > >::toPy
static PyObject * toPy(const std::vector< T > &t)
Definition: PyStoreCnvUtils.h:65
Py::cnv< long int >::toPy
static PyObject * toPy(long int t)
Definition: PyStoreCnvUtils.h:159
Py::cnv< float >::toPy
static PyObject * toPy(float t)
Definition: PyStoreCnvUtils.h:131
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
PyObject
_object PyObject
Definition: IPyComponent.h:26
readCCLHist.float
float
Definition: readCCLHist.py:83
Py::cnv< bool >::toPy
static PyObject * toPy(bool t)
Definition: PyStoreCnvUtils.h:117
Py::cnv< unsigned long >::toPy
static PyObject * toPy(unsigned long t)
Definition: PyStoreCnvUtils.h:166