-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfunctionwrapper.h
134 lines (117 loc) · 4.26 KB
/
functionwrapper.h
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
#ifndef MICROPYTHON_WRAP_FUNCTIONWRAPPER
#define MICROPYTHON_WRAP_FUNCTIONWRAPPER
#include "classwrapper.h"
namespace upywrap
{
//Main logic for registering functions.
//Usage:
//
//int Foo( int );
//double Bar( const std::string& );
//
//struct Funcs
//{
// func_name_def( Foo )
// func_name_def( Bar )
//};
//
//FunctionWrapper wrap( dict );
//wrap.Def< Funcs::Foo >( Foo );
//wrap.Def< Funcs::Bar >( Bar, Kwargs( "c", "str" );
//
//This will register given functions in dict,
//so if dict is the global dict of a module "mod"
//the functions can be used in uPy like this:
//
//import mod;
//mod.Foo();
//mod.Bar(c="bar");
//
//For supported arguments and return values see FromPyObj and ToPyObj classes.
class FunctionWrapper
{
public:
FunctionWrapper( mp_obj_module_t* mod ) :
FunctionWrapper( mod->globals )
{
}
FunctionWrapper( mp_obj_dict_t* globals ) :
globals( globals )
{
}
template< index_type name, class Ret, class... A >
void Def( Ret( *f ) ( A... ), Arguments arguments, typename SelectRetvalConverter< Ret >::type conv = nullptr )
{
typedef NativeCall< name, Ret, A... > call_type;
auto callerObject = call_type::CreateCaller( f );
callerObject->convert_retval = conv;
callerObject->arguments = std::move( arguments );
functionPointers[ (void*) name ] = callerObject;
mp_obj_dict_store( globals, new_qstr( name() ), call_type::CreateUPyFunction( *callerObject ) );
}
template< index_type name, class Ret, class... A >
void Def( Ret ( *f )( A... ), typename SelectRetvalConverter< Ret >::type conv = nullptr )
{
Def< name, Ret, A... >( f, Arguments(), conv );
}
private:
//wrap native call in function with uPy compatible mp_obj_t( mp_obj_t.... ) signature
template< index_type index, class Ret, class... A >
struct NativeCall
{
typedef FunctionCall< Ret, A... > call_type;
typedef typename call_type::func_type func_type;
static call_type* CreateCaller( func_type f )
{
return new call_type( f );
}
static mp_obj_t CreateUPyFunction( const call_type& caller )
{
if( caller.arguments.HasArguments() )
{
if( caller.arguments.NumberOfArguments() != sizeof...( A ) )
{
RaiseTypeException( ( std::string( "Wrong number of arguments in definition of " ) + index() ).data() );
}
return MakeFunction( caller.arguments.MimimumNumberOfArguments(), CallKw );
}
return CreateFunction< A... >::Create( Call, CallN );
}
static mp_obj_t Call( typename project2nd< A, mp_obj_t >::type... args )
{
auto f = (call_type*) FunctionWrapper::functionPointers[ (void*) index ];
return CallReturn< Ret, A... >::Call( f, args... );
}
static mp_obj_t CallN( mp_uint_t nargs, const mp_obj_t* args )
{
if( nargs != sizeof...( A ) )
{
RaiseTypeException( "Wrong number of arguments" );
}
auto f = (call_type*) FunctionWrapper::functionPointers[ (void*) index ];
return CallVar( f, args, make_index_sequence< sizeof...( A ) >() );
}
static mp_obj_t CallKw( size_t n_args, const mp_obj_t* pos_args, mp_map_t* kw_args )
{
auto f = (call_type*) FunctionWrapper::functionPointers[ (void*) index ];
Arguments::parsed_obj_t parsedArgs{};
f->arguments.Parse( n_args, pos_args, kw_args, parsedArgs );
return CallVar( f, parsedArgs.data(), make_index_sequence< sizeof...( A ) >() );
}
template< size_t... Indices >
static mp_obj_t CallVar( call_type* f, const mp_obj_t* args, index_sequence< Indices... > )
{
(void) args;
return CallReturn< Ret, A... >::Call( f, args[ Indices ]... );
}
};
mp_obj_dict_t* globals;
static function_ptrs functionPointers;
};
//we want a header only library but that yields multiply defined symbols when including this file more then once
//so as a simple hack: define MMICROPYTHON_WRAP_FUNCTIONWRAPPER_DEFINED in all but one include location
#ifndef MICROPYTHON_WRAP_FUNCTIONWRAPPER_DEFINED
function_ptrs FunctionWrapper::functionPointers;
#endif
}
#endif //#ifndef MICROPYTHON_WRAP_FUNCTIONWRAPPER