aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2014-05-13 12:51:08 -0700
committerBruce Mitchener <bruce.mitchener@gmail.com>2014-05-21 23:08:06 +0700
commit29839c4b7c6803c4741641e5ba8158516da2f680 (patch)
treee6b21e6f2a50f7733d2c5ca55b7fcef95e12c4d4
parent121cb8f58671140441eec21780f97fef4f8cb667 (diff)
Moarrrr templates! Remove some boilerplate in embind. Thanks imran and andy!
-rw-r--r--system/include/emscripten/bind.h27
-rw-r--r--tests/embind/embind_test.cpp2
2 files changed, 28 insertions, 1 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h
index 1be35826..7bc28ff6 100644
--- a/system/include/emscripten/bind.h
+++ b/system/include/emscripten/bind.h
@@ -282,6 +282,33 @@ namespace emscripten {
return method;
}
+ namespace internal {
+ // this should be in <type_traits>, but alas, it's not
+ template<typename T> struct remove_class;
+ template<typename C, typename R, typename... A>
+ struct remove_class<R(C::*)(A...)> { using type = R(A...); };
+ template<typename C, typename R, typename... A>
+ struct remove_class<R(C::*)(A...) const> { using type = R(A...); };
+ template<typename C, typename R, typename... A>
+ struct remove_class<R(C::*)(A...) volatile> { using type = R(A...); };
+ template<typename C, typename R, typename... A>
+ struct remove_class<R(C::*)(A...) const volatile> { using type = R(A...); };
+
+ template<typename LambdaType>
+ struct CalculateLambdaSignature {
+ using type = typename std::add_pointer<
+ typename remove_class<
+ decltype(&LambdaType::operator())
+ >::type
+ >::type;
+ };
+ }
+
+ template<typename LambdaType>
+ typename internal::CalculateLambdaSignature<LambdaType>::type optional_override(const LambdaType& fp) {
+ return fp;
+ }
+
////////////////////////////////////////////////////////////////////////////////
// Invoker
////////////////////////////////////////////////////////////////////////////////
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index a0cb858d..30267994 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -1226,7 +1226,7 @@ EMSCRIPTEN_BINDINGS(interface_tests) {
.function("abstractMethod", &AbstractClass::abstractMethod, pure_virtual())
// The select_overload is necessary because, otherwise, the C++ compiler
// cannot deduce the signature of the lambda function.
- .function("optionalMethod", select_overload<std::string(AbstractClass&, std::string)>(
+ .function("optionalMethod", optional_override(
[](AbstractClass& this_, std::string s) {
return this_.AbstractClass::optionalMethod(s);
}