diff options
-rwxr-xr-x | system/include/emscripten/bind.h | 81 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 12 |
2 files changed, 84 insertions, 9 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index 1828d95c..6e420a29 100755 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -367,21 +367,38 @@ namespace emscripten { typedef typename MemberBinding::WireType WireType; template<typename Getter> - static WireType propertyGet( + static WireType getByFunction( const Getter& getter, const ClassType& ptr ) { return MemberBinding::toWireType(getter(ptr)); } + template<typename Getter> + static WireType getByMemberFunction( + const Getter& getter, + const ClassType& ptr + ) { + return MemberBinding::toWireType((ptr.*getter)()); + } + template<typename Setter> - static void propertySet( + static void setByFunction( const Setter& setter, ClassType& ptr, WireType value ) { setter(ptr, MemberBinding::fromWireType(value)); } + + template<typename Setter> + static void setByMemberFunction( + const Setter& setter, + ClassType& ptr, + WireType value + ) { + (ptr.*setter)(MemberBinding::fromWireType(value)); + } }; // TODO: This could do a reinterpret-cast if sizeof(T) === sizeof(void*) @@ -435,6 +452,31 @@ namespace emscripten { typename SetterArgumentType, typename SetterThisType> value_tuple& element( + GetterReturnType (GetterThisType::*getter)() const, + void (SetterThisType::*setter)(SetterArgumentType) + ) { + using namespace internal; + _embind_register_tuple_element( + TypeID<ClassType>::get(), + TypeID<GetterReturnType>::get(), + reinterpret_cast<GenericFunction>( + &PropertyAccess<ClassType, GetterReturnType> + ::template getByMemberFunction<decltype(getter)>), + getContext(getter), + TypeID<SetterArgumentType>::get(), + reinterpret_cast<GenericFunction>( + &PropertyAccess<ClassType, SetterArgumentType> + ::template setByMemberFunction<decltype(setter)>), + getContext(setter)); + return *this; + } + + template< + typename GetterReturnType, + typename GetterThisType, + typename SetterArgumentType, + typename SetterThisType> + value_tuple& element( GetterReturnType (*getter)(const GetterThisType&), void (*setter)(SetterThisType&, SetterArgumentType) ) { @@ -444,12 +486,12 @@ namespace emscripten { TypeID<GetterReturnType>::get(), reinterpret_cast<GenericFunction>( &PropertyAccess<ClassType, GetterReturnType> - ::template propertyGet<decltype(getter)>), + ::template getByFunction<decltype(getter)>), getContext(getter), TypeID<SetterArgumentType>::get(), reinterpret_cast<GenericFunction>( &PropertyAccess<ClassType, SetterArgumentType> - ::template propertySet<decltype(setter)>), + ::template setByFunction<decltype(setter)>), getContext(setter)); return *this; } @@ -497,6 +539,33 @@ namespace emscripten { typename SetterThisType> value_struct& field( const char* fieldName, + GetterReturnType (GetterThisType::*getter)() const, + void (SetterThisType::*setter)(SetterArgumentType) + ) { + using namespace internal; + _embind_register_struct_field( + TypeID<ClassType>::get(), + fieldName, + TypeID<GetterReturnType>::get(), + reinterpret_cast<GenericFunction>( + &PropertyAccess<ClassType, GetterReturnType> + ::template getByMemberFunction<decltype(getter)>), + getContext(getter), + TypeID<SetterArgumentType>::get(), + reinterpret_cast<GenericFunction>( + &PropertyAccess<ClassType, SetterArgumentType> + ::template setByMemberFunction<decltype(setter)>), + getContext(setter)); + return *this; + } + + template< + typename GetterReturnType, + typename GetterThisType, + typename SetterArgumentType, + typename SetterThisType> + value_struct& field( + const char* fieldName, GetterReturnType (*getter)(const GetterThisType&), void (*setter)(SetterThisType&, SetterArgumentType) ) { @@ -507,12 +576,12 @@ namespace emscripten { TypeID<GetterReturnType>::get(), reinterpret_cast<GenericFunction>( &PropertyAccess<ClassType, GetterReturnType> - ::template propertyGet<decltype(getter)>), + ::template getByFunction<decltype(getter)>), getContext(getter), TypeID<SetterArgumentType>::get(), reinterpret_cast<GenericFunction>( &PropertyAccess<ClassType, SetterArgumentType> - ::template propertySet<decltype(setter)>), + ::template setByFunction<decltype(setter)>), getContext(setter)); return *this; } diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index 229d30d1..470309aa 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -722,6 +722,13 @@ std::map<std::string, int> embind_test_get_string_int_map() { struct Vector {
float x, y, z;
+
+ float getY() const {
+ return y;
+ }
+ void setY(float _y) {
+ y = _y;
+ }
};
struct DummyDataToTestPointerAdjustment {
@@ -1343,8 +1350,7 @@ EMSCRIPTEN_BINDINGS(tests) { value_tuple<TupleVector>("TupleVector")
.element(&TupleVector::x)
- .element(&TupleVector::y)
- //.element(&TupleVector::z)
+ .element(&Vector::getY, &Vector::setY)
.element(&readVectorZ, &writeVectorZ)
;
@@ -1359,7 +1365,7 @@ EMSCRIPTEN_BINDINGS(tests) { value_struct<StructVector>("StructVector")
.field("x", &StructVector::x)
- .field("y", &StructVector::y)
+ .field("y", &Vector::getY, &Vector::setY)
.field("z", &readVectorZ, &writeVectorZ)
;
|