@@ -352,6 +352,31 @@ namespace emscripten {
352352 );
353353 }
354354 };
355+
356+ template <typename FunctorType, typename ReturnType, typename ... Args>
357+ struct FunctorInvoker {
358+ static typename internal::BindingType<ReturnType>::WireType invoke (
359+ FunctorType& function,
360+ typename internal::BindingType<Args>::WireType... args
361+ ) {
362+ return internal::BindingType<ReturnType>::toWireType (
363+ function (
364+ internal::BindingType<Args>::fromWireType (args)...)
365+ );
366+ }
367+ };
368+
369+ template <typename FunctorType, typename ... Args>
370+ struct FunctorInvoker <FunctorType, void , Args...> {
371+ static void invoke (
372+ FunctorType& function,
373+ typename internal::BindingType<Args>::WireType... args
374+ ) {
375+ function (
376+ internal::BindingType<Args>::fromWireType (args)...);
377+ }
378+ };
379+
355380 }
356381
357382 // //////////////////////////////////////////////////////////////////////////////
@@ -487,37 +512,6 @@ namespace emscripten {
487512 }
488513 };
489514
490- template <typename FunctorType, typename ReturnType, typename ThisType, typename ... Args>
491- struct FunctorInvoker {
492-
493- static typename internal::BindingType<ReturnType>::WireType invoke (
494- FunctorType& function,
495- typename internal::BindingType<ThisType>::WireType wireThis,
496- typename internal::BindingType<Args>::WireType... args
497- ) {
498- return internal::BindingType<ReturnType>::toWireType (
499- function (
500- internal::BindingType<ThisType>::fromWireType (wireThis),
501- internal::BindingType<Args>::fromWireType (args)...)
502- );
503- }
504- };
505-
506- template <typename FunctorType, typename ThisType, typename ... Args>
507- struct FunctorInvoker <FunctorType, void , ThisType, Args...> {
508- using FunctionType = std::function<void (ThisType, Args...)>;
509-
510- static void invoke (
511- FunctorType& function,
512- typename internal::BindingType<ThisType>::WireType wireThis,
513- typename internal::BindingType<Args>::WireType... args
514- ) {
515- function (
516- internal::BindingType<ThisType>::fromWireType (wireThis),
517- internal::BindingType<Args>::fromWireType (args)...);
518- }
519- };
520-
521515 template <typename MemberPointer,
522516 typename ReturnType,
523517 typename ThisType,
@@ -1142,6 +1136,64 @@ namespace emscripten {
11421136
11431137 struct DeduceArgumentsTag {};
11441138
1139+ // //////////////////////////////////////////////////////////////////////////
1140+ // RegisterClassConstructor
1141+ // //////////////////////////////////////////////////////////////////////////
1142+
1143+ template <typename T>
1144+ struct RegisterClassConstructor ;
1145+
1146+ template <typename ReturnType, typename ... Args>
1147+ struct RegisterClassConstructor <ReturnType (*)(Args...)> {
1148+
1149+ template <typename ClassType, typename ... Policies>
1150+ static void invoke (ReturnType (*factory)(Args...)) {
1151+ typename WithPolicies<allow_raw_pointers, Policies...>::template ArgTypeList<ReturnType, Args...> args;
1152+ auto invoke = &Invoker<ReturnType, Args...>::invoke;
1153+ _embind_register_class_constructor (
1154+ TypeID<ClassType>::get (),
1155+ args.getCount (),
1156+ args.getTypes (),
1157+ getSignature (invoke),
1158+ reinterpret_cast <GenericFunction>(invoke),
1159+ reinterpret_cast <GenericFunction>(factory));
1160+ }
1161+ };
1162+
1163+ template <typename ReturnType, typename ... Args>
1164+ struct RegisterClassConstructor <std::function<ReturnType (Args...)>> {
1165+
1166+ template <typename ClassType, typename ... Policies>
1167+ static void invoke (std::function<ReturnType (Args...)> factory) {
1168+ typename WithPolicies<Policies...>::template ArgTypeList<ReturnType, Args...> args;
1169+ auto invoke = &FunctorInvoker<decltype (factory), ReturnType, Args...>::invoke;
1170+ _embind_register_class_constructor (
1171+ TypeID<ClassType>::get (),
1172+ args.getCount (),
1173+ args.getTypes (),
1174+ getSignature (invoke),
1175+ reinterpret_cast <GenericFunction>(invoke),
1176+ reinterpret_cast <GenericFunction>(getContext (factory)));
1177+ }
1178+ };
1179+
1180+ template <typename ReturnType, typename ... Args>
1181+ struct RegisterClassConstructor <ReturnType (Args...)> {
1182+
1183+ template <typename ClassType, typename Callable, typename ... Policies>
1184+ static void invoke (Callable& factory) {
1185+ typename WithPolicies<Policies...>::template ArgTypeList<ReturnType, Args...> args;
1186+ auto invoke = &FunctorInvoker<decltype (factory), ReturnType, Args...>::invoke;
1187+ _embind_register_class_constructor (
1188+ TypeID<ClassType>::get (),
1189+ args.getCount (),
1190+ args.getTypes (),
1191+ getSignature (invoke),
1192+ reinterpret_cast <GenericFunction>(invoke),
1193+ reinterpret_cast <GenericFunction>(getContext (factory)));
1194+ }
1195+ };
1196+
11451197 // //////////////////////////////////////////////////////////////////////////
11461198 // RegisterClassMethod
11471199 // //////////////////////////////////////////////////////////////////////////
@@ -1328,20 +1380,15 @@ namespace emscripten {
13281380 policies...);
13291381 }
13301382
1331- template <typename ... Args, typename ReturnType, typename ... Policies>
1332- EMSCRIPTEN_ALWAYS_INLINE const class_& constructor (ReturnType (*factory)(Args...), Policies...) const {
1333- using namespace internal ;
1383+ template <typename Signature = internal::DeduceArgumentsTag, typename Callable, typename ... Policies>
1384+ EMSCRIPTEN_ALWAYS_INLINE const class_& constructor (Callable callable, Policies...) const {
13341385
1335- // TODO: allows all raw pointers... policies need a rethink
1336- typename WithPolicies<allow_raw_pointers, Policies...>::template ArgTypeList<ReturnType, Args...> args;
1337- auto invoke = &Invoker<ReturnType, Args...>::invoke;
1338- _embind_register_class_constructor (
1339- TypeID<ClassType>::get (),
1340- args.getCount (),
1341- args.getTypes (),
1342- getSignature (invoke),
1343- reinterpret_cast <GenericFunction>(invoke),
1344- reinterpret_cast <GenericFunction>(factory));
1386+ using invoker = internal::RegisterClassConstructor<
1387+ typename std::conditional<std::is_same<Signature, internal::DeduceArgumentsTag>::value,
1388+ Callable,
1389+ Signature>::type>;
1390+
1391+ invoker::template invoke<ClassType, Policies...>(callable);
13451392 return *this ;
13461393 }
13471394
0 commit comments