From 78548810ba0ec6386fc4c7ab1342fe55321dad40 Mon Sep 17 00:00:00 2001 From: Chad Austin Date: Mon, 25 Feb 2013 18:58:11 -0800 Subject: [PATCH] Allow const non-member functions --- system/include/emscripten/bind.h | 43 +++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index d99dd7638..33e1cc43a 100755 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -440,7 +440,7 @@ namespace emscripten { template struct ArrayAccess { - static ElementType get(ClassType& ptr, IndexType index) { + static ElementType get(const ClassType& ptr, IndexType index) { return ptr[index]; } @@ -448,6 +448,18 @@ namespace emscripten { ptr[index] = value; } }; + + template + struct MapAccess { + static ValueType get(const std::map& m, const KeyType& k) { + auto i = m.find(k); + if (i == m.end()) { + return ValueType(); + } else { + return i->second; + } + } + }; } //////////////////////////////////////////////////////////////////////////////// @@ -735,6 +747,22 @@ namespace emscripten { return *this; } + template + class_& method(const char* methodName, ReturnType (*function)(const ClassType& ptr, Args...), Policies...) { + using namespace internal; + + typename WithPolicies::template ArgTypeList args; + _embind_register_class_method( + TypeID::get(), + methodName, + args.count, + args.types, + reinterpret_cast(&FunctionInvoker::invoke), + sizeof(function), + &function); + return *this; + } + template class_& field(const char* fieldName, FieldType ClassType::*field) { using namespace internal; @@ -795,7 +823,7 @@ namespace emscripten { void (VecType::*push_back)(const T&) = &VecType::push_back; const T& (VecType::*at)(size_t) const = &VecType::at; - auto c = class_>(name) + return class_>(name) .template constructor<>() .method("push_back", push_back) .method("at", at) @@ -803,8 +831,6 @@ namespace emscripten { .template arrayoperatorget() .template arrayoperatorset() ; - - return c; } //////////////////////////////////////////////////////////////////////////////// @@ -815,13 +841,12 @@ namespace emscripten { using namespace std; typedef map MapType; - auto c = class_(name) + return class_(name) .method("size", &MapType::size) - .template arrayoperatorget() + // make this map_get? + .method("array_get", internal::MapAccess::get) .template arrayoperatorset() - ; - - return c; + ; }