bind.h 中的 C++ API 定義
此 API 的指南文件可在 Embind 中找到。
目前僅支援 allow_raw_pointers
原則。最終,我們希望能實作 Boost.Python 類型的原始指標原則,以管理物件的所有權。
allow_raw_pointer
¶select_overload
(typename std::add_pointer<Signature>::type fn)¶// Prototype
template<typename Signature>
typename std::add_pointer<Signature>::type select_overload(typename std::add_pointer<Signature>::type fn)
typename std::add_pointer<Signature>::type fn –
select_overload
()¶// Prototype
template<typename Signature, typename ClassType>
typename internal::MemberFunctionType<ClassType, Signature>::type select_overload(Signature (ClassType::*fn))
Signature (ClassType::*fn) –
select_const
()¶// Prototype
template<typename ClassType, typename ReturnType, typename... Args>
auto select_const(ReturnType (ClassType::*method)(Args...) const)
ReturnType (ClassType::*method)(Args...) const –
optional_override
(const LambdaType &fp)¶// Prototype
template<typename LambdaType>
typename internal::CalculateLambdaSignature<LambdaType>::type optional_override(const LambdaType& fp)
const LambdaType& fp –
function
()¶//prototype
template<typename ReturnType, typename... Args, typename... Policies>
void function(const char* name, ReturnType (*fn)(Args...), Policies...)
註冊一個要匯出到 JavaScript 的函式。這是從 EMSCRIPTEN_BINDINGS()
區塊內呼叫。
例如,要匯出函式 lerp()
// quick_example.cpp
#include <emscripten/bind.h>
using namespace emscripten;
float lerp(float a, float b, float t) {
return (1 - t) * a + t * b;
}
EMSCRIPTEN_BINDINGS(my_module) {
function("lerp", &lerp);
}
const char* name – 要匯出的函式名稱 (例如 "lerp"
)。
ReturnType (*fn)(Args...) – 匯出函式的函式指標位址 (例如 &lerp
)。
Policies... – 用於管理原始指標物件所有權的原則。目前必須是 allow_raw_pointers
。
value_array
: public internal::noncopyable¶class_type
¶一個 ClassType
的 typedef,為該類別的模板類型名稱。
value_array
(const char *name)¶建構子。
const char* name –
~value_array
()¶解構子。
element
(ElementType InstanceType::*field)¶ElementType InstanceType::*field – 注意 ElementType
和 InstanceType
是類型名稱(模板類型)。
element
(Getter getter, Setter setter)¶Getter getter – 注意 Getter
是一個類型名稱(模板類型)。
Setter setter – 注意 Setter
是一個類型名稱(模板類型)。
element
(index<Index>)¶index<Index> – 注意 Index
是一個整數模板參數。
value_object
: public internal::noncopyable¶class_type
¶一個 ClassType
的 typedef,為該類別的模板類型名稱。
value_object
(const char *name)¶建構子。
const char* name –
~value_object
()¶解構子。
field
(const char *fieldName, FieldType InstanceType::*field)¶const char* fieldName –
FieldType InstanceType::*field –
field
(const char *fieldName, Getter getter, Setter setter)¶const char* fieldName –
Getter getter – 注意 Getter
是一個類型名稱(模板類型)。
Setter setter – 注意 Setter
是一個類型名稱(模板類型)。
field
(const char *fieldName, index<Index>)¶const char* fieldName –
index<Index> – 注意 Index
是一個整數模板參數。
default_smart_ptr_trait
¶//prototype
template<typename PointerType>
struct default_smart_ptr_trait
get_sharing_policy
()¶void* v –
construct_null
()¶請注意,返回的 PointerType
是一個類型名稱(模板類型)。
smart_ptr_trait
¶//prototype
template<typename PointerType>
struct smart_ptr_trait : public default_smart_ptr_trait<PointerType>
element_type
¶//prototype
typedef typename PointerType::element_type element_type;
PointerType::element_type
的 typedef,其中 PointerType
是一個類型名稱(模板類型)。
get
(const PointerType &ptr)¶const PointerType& ptr – 注意 PointerType
是一個類型名稱(模板類型)
//prototype
template<typename PointeeType>
struct smart_ptr_trait<std::shared_ptr<PointeeType>>
一個指向 std::shared_ptr<PointeeType> 的 typedef,其中 PointeeType
是一個類型名稱(模板類型)。
PointerType::element_type
的 typedef。
const PointerType& ptr –
PointeeType* p – 注意 PointeeType
是一個類型名稱(模板類型)。
EM_VAL v –
wrapper
: public T, public internal::WrapperBase¶//prototype
template<typename T>
class wrapper : public T, public internal::WrapperBase
class_type
¶一個 T
的 typedef,為該類別的模板類型名稱。
wrapper
(val &&wrapped, Args&&... args)¶//prototype
template<typename... Args>
explicit wrapper(val&& wrapped, Args&&... args)
: T(std::forward<Args>(args)...)
, wrapped(std::forward<val>(wrapped))
建構子。
val&& wrapped –
Args&&... args – 注意 Args
是一個類型名稱(模板類型)。
~wrapper
()¶解構子。
call
(const char *name, Args&&... args) const¶建構子。
const char* name –
Args&&... args – 注意 Args
是一個類型名稱(模板類型)。
請注意,ReturnType
是一個類型名稱(模板類型)。
EMSCRIPTEN_WRAPPER
(T)¶T –
base
¶class_type
¶BaseClass
的 typedef,即類別的模板類型名稱。
verify
()¶請注意,這是一個模板函式,它接受類型名稱 ClassType
。
get
()¶template<typename ClassType>
using Upcaster = BaseClass* (*)(ClassType*);
template<typename ClassType>
using Downcaster = ClassType* (*)(BaseClass*);
getUpcaster
()¶//prototype
template<typename ClassType>
static Upcaster<ClassType> getUpcaster()
getDowncaster
()¶//prototype
template<typename ClassType>
static Downcaster<ClassType> getDowncaster()
convertPointer
(From *ptr)¶//prototype
template<typename From, typename To>
static To* convertPointer(From* ptr)
From* ptr –
pure_virtual
¶constructor
¶請注意,這是一個模板結構,採用類型名稱 ... ConstructorArgs
。
class_
¶請注意,這是一個模板類別,帶有類型名稱參數 ClassType
和 BaseSpecifier
。
class_type
¶ClassType
的 typedef(類別的類型名稱)。
base_specifier
¶BaseSpecifier
的 typedef(類別的類型名稱)。
HELPNEEDEDHERE
¶class_() = delete;
class_
(const char *name)¶//prototype
EMSCRIPTEN_ALWAYS_INLINE explicit class_(const char* name)
建構子。
const char* name –
smart_ptr
(const char *name) const¶//prototype
template<typename PointerType>
EMSCRIPTEN_ALWAYS_INLINE const class_& smart_ptr(const char* name) const
const char* name –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
constructor
() const¶//prototype
template<typename... ConstructorArgs, typename... Policies>
EMSCRIPTEN_ALWAYS_INLINE const class_& constructor(Policies... policies) const
類別建構子的零引數形式。這會使用範本中指定的引數來叫用自然建構子。如需更多資訊,請參閱外部建構子。
Policies... policies – 管理原始指標物件擁有權的原則。目前必須是 allow_raw_pointers
。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
constructor
(Callable callable, Policies...) const¶//prototype
template<typename Signature = internal::DeduceArgumentsTag, typename Callable, typename... Policies>
EMSCRIPTEN_ALWAYS_INLINE const class_& constructor(Callable callable, Policies...) const
用於使用工廠函式建立物件的物件的類別建構子。此方法會接受函式指標、std::function
物件或函式物件,這些物件將傳回新建立的物件。當 Callable
是函式物件時,函式簽章必須在 Signature
範本參數中以 ReturnType (Args...)
的格式明確指定。對於函式物件以外的 Callable
類型,將會推斷方法簽章。
以下都是對 constructor
的有效呼叫
using namespace std::placeholders;
myClass1.constructor(&my_factory);
myClass2.constructor(std::function<ClassType2(float, float)>(&class2_factory));
myClass3.constructor<ClassType3(const val&)>(std::bind(Class3Functor(), _1));
如需更多資訊,請參閱外部建構子。
Callable callable – 請注意,Callable
可以是成員函式指標、函式指標、std::function
或函式物件。
Policies... policies – 管理原始指標物件擁有權的原則。目前必須是 allow_raw_pointers
。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
smart_ptr_constructor
() const¶//prototype
template<typename SmartPtr, typename... Args, typename... Policies>
EMSCRIPTEN_ALWAYS_INLINE const class_& smart_ptr_constructor(const char* smartPtrName, SmartPtr (*factory)(Args...), Policies...) const
const char* smartPtrName –
SmartPtr (*factory)(Args...) –
Policies... policies – 管理原始指標物件擁有權的原則。目前必須是 allow_raw_pointers
。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
allow_subclass
() const¶//prototype
template<typename WrapperType, typename PointerType, typename... ConstructorArgs>
EMSCRIPTEN_ALWAYS_INLINE const class_& allow_subclass(
const char* wrapperClassName,
const char* pointerName,
::emscripten::constructor<ConstructorArgs...> = ::emscripten::constructor<>()
) const
const char* wrapperClassName –
const char* pointerName –
emscripten::constructor<ConstructorArgs...> constructor) –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
allow_subclass
(const char *wrapperClassName, emscripten::constructor<ConstructorArgs...> constructor) const¶//prototype
template<typename WrapperType, typename... ConstructorArgs>
EMSCRIPTEN_ALWAYS_INLINE const class_& allow_subclass(
const char* wrapperClassName,
::emscripten::constructor<ConstructorArgs...> constructor = ::emscripten::constructor<>()
) const
const char* wrapperClassName –
::emscripten::constructor<ConstructorArgs...> constructor) –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
function
() const¶//prototype
template<typename Signature = internal::DeduceArgumentsTag, typename Callable, typename... Policies>
EMSCRIPTEN_ALWAYS_INLINE const class_& function(const char* methodName, Callable callable, Policies...) const
此方法用於宣告屬於類別的方法。
在 JavaScript 端,這是一個會綁定為原型屬性的函式。例如,.function("myClassMember", &MyClass::myClassMember)
會將 myClassMember
綁定到 JavaScript 中的 MyClass.prototype.myClassMember
。此方法將接受成員函式指標、函式指標、std::function
物件或函式物件。當 Callable
不是成員函式指標時,它必須接受 ClassType
作為第一個(this
)參數。當 Callable
是一個函式物件時,函式簽章必須以 ReturnType (Args...)
的格式在 Signature
樣板參數中明確指定。對於函式物件以外的 Callable
類型,將會推導出方法簽章。
以人類可讀的知名符號格式(例如,@@iterator
)指定的方法名稱會使用 JavaScript 的具名 Symbol
(例如,Symbol.iterator
)來綁定。
以下是對 function
的所有有效呼叫
using namespace std::placeholders;
myClass.function("myClassMember", &MyClass::myClassMember)
.function("myFreeFunction", &my_free_function)
.function("myStdFunction", std::function<float(ClassType&, float, float)>(&my_function))
.function<val(const MyClass&)>("myFunctor", std::bind(&my_functor_taking_this, _1));
const char* methodName –
Callable callable – 請注意,Callable
可以是成員函式指標、函式指標、std::function
或函式物件。
typename... Policies – 用於管理原始指標物件所有權的Policy。目前必須是 allow_raw_pointers
。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
property
() const¶//prototype
template<typename FieldType, typename = typename std::enable_if<!std::is_function<FieldType>::value>::type>
EMSCRIPTEN_ALWAYS_INLINE const class_& property(const char* fieldName, const FieldType ClassType::*field) const
const char* fieldName –
const FieldType ClassType::*field –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
property
(const char *fieldName, FieldType ClassType::*field) const¶//prototype
template<typename FieldType, typename = typename std::enable_if<!std::is_function<FieldType>::value>::type>
EMSCRIPTEN_ALWAYS_INLINE const class_& property(const char* fieldName, FieldType ClassType::*field) const
const char* fieldName –
FieldType ClassType::*field –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
property
(const char *fieldName, Getter getter) const¶//prototype
template<typename PropertyType = internal::DeduceArgumentsTag, typename Getter>
EMSCRIPTEN_ALWAYS_INLINE const class_& property(const char* fieldName, Getter getter) const;
使用指定的 getter
在類別上宣告一個具有指定 fieldName
的唯讀屬性。 Getter
可以是類別方法、函式、std::function
或函式物件。當 Getter
不是成員函式指標時,它必須接受 ClassType
的實例作為 this
引數。當 Getter
是一個函式物件時,屬性類型必須指定為樣板參數,因為它無法被推導出來,例如:myClass.property<int>("myIntProperty", MyIntGetterFunctor());
const char* fieldName –
Getter getter – 請注意,Getter
是一個函式樣板 typename。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
property
(const char *fieldName, Getter getter, Setter setter) const¶//prototype
template<typename PropertyType = internal::DeduceArgumentsTag, typename Getter, typename Setter>
EMSCRIPTEN_ALWAYS_INLINE const class_& property(const char* fieldName, Getter getter, Setter setter) const
這是一個接受 typename Setter
和 Getter
的函式樣板:template<typename Getter, typename Setter>
,它宣告類別上具有指定 fieldName
的讀寫屬性。Getter
和 Setter
可以是類別方法、函式、std::function
或函式物件。當 Getter
或 Setter
不是成員函式指標時,它必須接受 ClassType
的實例作為 this
引數。當 Getter
或 Setter
是一個函式物件時,屬性類型必須指定為樣板參數,因為它無法被推導出來,例如:myClass.property<int>("myIntProperty", MyIntGetterFunctor(), MyIntSetterFunctor());
const char* fieldName –
Getter getter – 請注意,Getter
是一個函式樣板 typename。
Setter setter – 請注意,Setter
是一個函式樣板 typename。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
class_function
() const¶//prototype
template<typename ReturnType, typename... Args, typename... Policies>
EMSCRIPTEN_ALWAYS_INLINE const class_& class_function(const char* methodName, ReturnType (*classMethod)(Args...), Policies...) const
此方法用於宣告屬於類別的靜態函式。
在 JavaScript 端,這是一個會綁定為建構函式屬性的函式。例如,.class_function("myStaticFunction", &MyClass::myStaticFunction)
會將 myStaticFunction
綁定到 MyClass.myStaticFunction
。
以人類可讀的知名符號格式(例如,@@species
)指定的方法名稱會使用 JavaScript 的具名 Symbol
(例如,Symbol.species
)來綁定。
const char* methodName –
ReturnType (*classMethod)(Args...) –
Policies... – 用於管理原始指標物件所有權的原則。目前必須是 allow_raw_pointers
。
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
class_property
(const char *fieldName, FieldType *field) const¶//prototype
template<typename FieldType>
EMSCRIPTEN_ALWAYS_INLINE const class_& property(const char* fieldName, FieldType *field) const
const char* fieldName –
FieldType ClassType::*field –
目前物件的 const
參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結定義繫結的 class_
函式。
enum_
¶//prototype
template<typename EnumType>
class enum_
註冊要匯出到 JavaScript 的列舉。這是從 EMSCRIPTEN_BINDINGS()
區塊內呼叫,並且適用於 C++98 列舉和 C++11 的「列舉類別」。有關更多資訊,請參閱列舉。
enum_type
¶EnumType
(類別的 typename) 的 typedef。
enum_
(const char *name)¶建構子。
const char* name –
value
(const char *name, EnumType value)¶註冊一個列舉值。
const char* name – 列舉值的名稱。
EnumType value – 列舉值的類型。
對目前物件的參考。這允許在 EMSCRIPTEN_BINDINGS()
區塊中鏈結多個列舉值。
constant
(const char *name, const ConstantType &v)¶//prototype
template<typename ConstantType>
void constant(const char* name, const ConstantType& v)
註冊一個要匯出到 JavaScript 的常數。這是從 EMSCRIPTEN_BINDINGS()
區塊內呼叫。
EMSCRIPTEN_BINDINGS(my_constant_example) {
constant("SOME_CONSTANT", SOME_CONSTANT);
}
const char* name – 常數的名稱。
const ConstantType& v – 常數的型別。這可以是任何 embind 已知的型別。