forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionModule.hxx
205 lines (171 loc) · 8.53 KB
/
ExtensionModule.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//-----------------------------------------------------------------------------
//
// Copyright (c) 1998 - 2007, The Regents of the University of California
// Produced at the Lawrence Livermore National Laboratory
// All rights reserved.
//
// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
// full copyright notice is contained in the file COPYRIGHT located at the root
// of the PyCXX distribution.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the disclaimer below.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the disclaimer (as noted below) in the
// documentation and/or materials provided with the distribution.
// - Neither the name of the UC/LLNL nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF
// CALIFORNIA, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
//-----------------------------------------------------------------------------
#ifndef __CXX_ExtensionModule__h
#define __CXX_ExtensionModule__h
namespace Py
{
class ExtensionModuleBase
{
public:
ExtensionModuleBase( const char *name );
virtual ~ExtensionModuleBase();
Module module( void ) const; // only valid after initialize() has been called
Dict moduleDictionary( void ) const; // only valid after initialize() has been called
virtual Object invoke_method_noargs( void *method_def ) = 0;
virtual Object invoke_method_keyword( void *method_def, const Tuple &_args, const Dict &_keywords ) = 0;
virtual Object invoke_method_varargs( void *method_def, const Tuple &_args ) = 0;
const std::string &name() const;
const std::string &fullName() const;
// what is returned from PyInit_<module> function
Object moduleObject( void ) const;
protected:
// Initialize the module
void initialize( const char *module_doc );
const std::string m_module_name;
const std::string m_full_module_name;
MethodTable m_method_table;
PyObject *m_module;
private:
//
// prevent the compiler generating these unwanted functions
//
ExtensionModuleBase( const ExtensionModuleBase & ); //unimplemented
void operator=( const ExtensionModuleBase & ); //unimplemented
};
// Note: Python calls noargs as varargs buts args==NULL
extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, PyObject * );
extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args );
extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords );
extern "C" void do_not_dealloc( void * );
template<TEMPLATE_TYPENAME T>
class ExtensionModule : public ExtensionModuleBase
{
public:
ExtensionModule( const char *name )
: ExtensionModuleBase( name )
{}
virtual ~ExtensionModule()
{}
protected:
typedef Object (T::*method_noargs_function_t)();
typedef Object (T::*method_varargs_function_t)( const Tuple &args );
typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws );
typedef std::map<std::string, MethodDefExt<T> *> method_map_t;
static void add_noargs_method( const char *name, method_noargs_function_t function, const char *doc="" )
{
method_map_t &mm = methods();
mm[ std::string( name ) ] = new MethodDefExt<T>( name, function, method_noargs_call_handler, doc );
}
static void add_varargs_method( const char *name, method_varargs_function_t function, const char *doc="" )
{
method_map_t &mm = methods();
mm[ std::string( name ) ] = new MethodDefExt<T>( name, function, method_varargs_call_handler, doc );
}
static void add_keyword_method( const char *name, method_keyword_function_t function, const char *doc="" )
{
method_map_t &mm = methods();
mm[ std::string( name ) ] = new MethodDefExt<T>( name, function, method_keyword_call_handler, doc );
}
void initialize( const char *module_doc="" )
{
ExtensionModuleBase::initialize( module_doc );
Dict dict( moduleDictionary() );
//
// put each of the methods into the modules dictionary
// so that we get called back at the function in T.
//
method_map_t &mm = methods();
EXPLICIT_TYPENAME method_map_t::const_iterator i = mm.begin();
EXPLICIT_TYPENAME method_map_t::const_iterator i_end = mm.end();
for ( ; i != i_end; ++i )
{
MethodDefExt<T> *method_def = (*i).second;
static PyObject *self = PyCObject_FromVoidPtr( this, do_not_dealloc );
Tuple args( 2 );
args[0] = Object( self, true );
args[1] = Object( PyCObject_FromVoidPtr( method_def, do_not_dealloc ), true );
PyObject *func = PyCFunction_New
(
&method_def->ext_meth_def,
new_reference_to( args )
);
method_def->py_method = Object( func, true );
dict[ (*i).first ] = method_def->py_method;
}
}
protected: // Tom Malcolmson reports that derived classes need access to these
static method_map_t &methods( void )
{
static method_map_t *map_of_methods = NULL;
if( map_of_methods == NULL )
map_of_methods = new method_map_t;
return *map_of_methods;
}
// this invoke function must be called from within a try catch block
virtual Object invoke_method_noargs( void *method_def )
{
// cast up to the derived class, method_def and call
T *self = static_cast<T *>( this );
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( method_def );
return (self->*meth_def->ext_noargs_function)();
}
// this invoke function must be called from within a try catch block
virtual Object invoke_method_varargs( void *method_def, const Tuple &args )
{
// cast up to the derived class, method_def and call
T *self = static_cast<T *>( this );
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( method_def );
return (self->*meth_def->ext_varargs_function)( args );
}
// this invoke function must be called from within a try catch block
virtual Object invoke_method_keyword( void *method_def, const Tuple &args, const Dict &keywords )
{
// cast up to the derived class, method_def and call
T *self = static_cast<T *>( this );
MethodDefExt<T> *meth_def = reinterpret_cast<MethodDefExt<T> *>( method_def );
return (self->*meth_def->ext_keyword_function)( args, keywords );
}
private:
//
// prevent the compiler generating these unwanted functions
//
ExtensionModule( const ExtensionModule<T> & ); //unimplemented
void operator=( const ExtensionModule<T> & ); //unimplemented
};
} // Namespace Py
// End of __CXX_ExtensionModule__h
#endif