wrenbind17::ForeignKlassImpl

wrenbind17::ForeignKlassImpl #

Module: Wrenbind17

More…

#include <foreign.hpp>

Inherits from wrenbind17::ForeignKlass

Public Functions #

Name
ForeignKlassImpl(std::string name)
~ForeignKlassImpl() =default
ForeignKlassImpl(const ForeignKlassImpl< T > & other)
virtual void generate(std::ostream & os) const overrideGenerate Wren code for this class.
template <typename… Args> void ctor(const std::string & name ="new”)Add a constructor to this class.
template <auto Fn> void func(std::string name)Add a member function to this class.
template <auto Fn> void func(const ForeignMethodOperator name)Add a member operator function to this class that exists outside of the class.
template <auto Fn> void funcExt(std::string name)Add a member function via a static function.
template <auto Fn> void funcExt(const ForeignMethodOperator name)Add a member operator via a static function that exists outside of the class.
template <auto Fn> void funcStatic(std::string name)Add a static function to this class.
template <auto Fn> void funcStaticExt(std::string name)Add a static function to this class that exists outside of the class.
template <auto Var> void var(std::string name)Add a read-write variable to this class.
template <auto Var> void varReadonly(std::string name)Add a read-only variable to this class.
template <auto Getter,auto Setter> void prop(std::string name)Add a read-write property to this class via a getter and a setter.
template <auto Getter> void propReadonly(std::string name)Add a read-onlu property to this class via a getter.
template <auto Getter,auto Setter> void propExt(std::string name)Add a read-write property to this class via a getter and a setter.
template <auto Getter> void propReadonlyExt(std::string name)Add a read-only property to this class via a getter.

Additional inherited members #

Public Functions inherited from wrenbind17::ForeignKlass

Name
ForeignKlass(std::string name)
virtual ~ForeignKlass() =default
ForeignKlass(const ForeignKlass & other)
ForeignMethod & findFunc(const std::string & name, const bool isStatic)Looks up a foreign function that belongs to this class.
ForeignProp & findProp(const std::string & name, const bool isStatic)Looks up a foreign property that belongs to this class.
WrenForeignMethodFn findSignature(const std::string & signature, const bool isStatic)Finds a function based on the signature.
const std::string & getName() constReturns the name of this foreign class.
WrenForeignClassMethods & getAllocators()Returns a struct with pointers to the allocator and deallocator.

Protected Attributes inherited from wrenbind17::ForeignKlass

Name
std::string name
std::string ctorDef
std::unordered_map< std::string, std::unique_ptr< ForeignMethod > > methods
std::unordered_map< std::string, std::unique_ptr< ForeignProp > > props
WrenForeignClassMethods allocators

Detailed Description #

template <typename T >
class wrenbind17::ForeignKlassImpl;

@brtief Type specific implementation of foreign class

Public Functions Documentation #

function ForeignKlassImpl #

inline ForeignKlassImpl(
    std::string name
)

function ~ForeignKlassImpl #

~ForeignKlassImpl() =default

function ForeignKlassImpl #

ForeignKlassImpl(
    const ForeignKlassImpl< T > & other
)

function generate #

inline virtual void generate(
    std::ostream & os
) const override

Generate Wren code for this class.

Reimplements: wrenbind17::ForeignKlass::generate

function ctor #

template <typename... Args>
inline void ctor(
    const std::string & name ="new"
)

Add a constructor to this class.

function func #

template <auto Fn>
inline void func(
    std::string name
)

Add a member function to this class.

The number of arguments and what type of arguments this class needs is handled at compile time with metaprogramming. When this C++ function you are adding is called from Wren, it will check the types passed and will match the C++ signature. If the types do not match an exception is thrown that can be handled by Wren as a fiber.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}
    void bar() {...}
    int baz() const {...}
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add some methods
    cls.func<&Foo::bar>("bar");
    cls.func<&Foo::baz>("baz");
}

function func #

template <auto Fn>
inline void func(
    const ForeignMethodOperator name
)

Add a member operator function to this class that exists outside of the class.

See: ForeignMethodOperator

The number of arguments and what type of arguments this class needs is handled at compile time with metaprogramming. When this C++ function you are adding is called from Wren, it will check the types passed and will match the C++ signature. If the types do not match an exception is thrown that can be handled by Wren as a fiber.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}
    Foo& operator+(const std::string& other) {...}
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add some methods
    cls.func<&Foo::operator+>(wren::ForeignMethodOperator::OPERATOR_ADD);
}

function funcExt #

template <auto Fn>
inline void funcExt(
    std::string name
)

Add a member function via a static function.

The number of arguments and what type of arguments this class needs is handled at compile time with metaprogramming. When this C++ function you are adding is called from Wren, it will check the types passed and will match the C++ signature. If the types do not match an exception is thrown that can be handled by Wren as a fiber.

This function does not accept class methods, but instead it uses regular functions that have first parameter as “this” which is a reference to the class you are adding.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}

    std::string message;
};

static std::string fooBaz(Foo& self, int a, int b) {
    return self.message;
}

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add some methods
    cls.funcExt<&fooBaz>("bar");
}

function funcExt #

template <auto Fn>
inline void funcExt(
    const ForeignMethodOperator name
)

Add a member operator via a static function that exists outside of the class.

See: ForeignMethodOperator

Same as funcExt but instead it can accept an operator enumeration.

function funcStatic #

template <auto Fn>
inline void funcStatic(
    std::string name
)

Add a static function to this class.

See: func

The number of arguments and what type of arguments this class needs is handled at compile time with metaprogramming. When this C++ function you are adding is called from Wren, it will check the types passed and will match the C++ signature. If the types do not match an exception is thrown that can be handled by Wren as a fiber.

This only works if the function you are adding is static.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}
    static void bar() {...}
    static int baz() const {...}
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add some methods
    cls.funcStatic<&Foo::bar>("bar");
    cls.funcStatic<&Foo::baz>("baz");
}

function funcStaticExt #

template <auto Fn>
inline void funcStaticExt(
    std::string name
)

Add a static function to this class that exists outside of the class.

See: funcStatic

The number of arguments and what type of arguments this class needs is handled at compile time with metaprogramming. When this C++ function you are adding is called from Wren, it will check the types passed and will match the C++ signature. If the types do not match an exception is thrown that can be handled by Wren as a fiber.

This only works if the function you are adding is static.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}
};

static void fooBar() { ... }
static void fooBaz() { ... }

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add some methods
    cls.funcStatic<&fooBar>("bar");
    cls.funcStatic<&fooBaz>("baz");
}

function var #

template <auto Var>
inline void var(
    std::string name
)

Add a read-write variable to this class.

Example:

class Foo {
public:
    Foo(const std::string& msg) {...}

    std::string msg;
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add class variable as a read write Wren class property
    cls.var<&Foo::msg>("msg");
}

function varReadonly #

template <auto Var>
inline void varReadonly(
    std::string name
)

Add a read-only variable to this class.

This is exactly the same as var() but the variable can be only read from Wren. It cannot be reassigned.

function prop #

template <auto Getter,
auto Setter>
inline void prop(
    std::string name
)

Add a read-write property to this class via a getter and a setter.

This essentially creates the same thing as var() but instead of using pointer to the class field, it uses getter and setter functions.

class Foo {
public:
    Foo(const std::string& msg) {...}
    void setMsg(const std::string& msg) {...}
    const std::string& getMsg() const {...}
private:
    std::string msg;
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add class variable as a read write Wren class property
    cls.prop<&Foo::getMsg, &Foo::setMsg>("msg");
}

function propReadonly #

template <auto Getter>
inline void propReadonly(
    std::string name
)

Add a read-onlu property to this class via a getter.

This essentially creates the same thing as varReadonly() but instead of using pointer to the class field, it uses a getter. This property will be read only and cannot be reassigned from Wren.

class Foo {
public:
    Foo(const std::string& msg) {...}
    void setMsg(const std::string& msg) {...}
    const std::string& getMsg() const {...}
private:
    std::string msg;
};

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add class variable as a read only Wren class property
    cls.propReadonly<&Foo::getMsg>("msg");
}

function propExt #

template <auto Getter,
auto Setter>
inline void propExt(
    std::string name
)

Add a read-write property to this class via a getter and a setter.

See: prop

This essentially creates the same thing as var() but instead of using pointer to the class field, it uses a static getter and setter. The setter and getter do not have to belong to the class itself, but must be static functions, and must accept the class as a reference as a first parameter.

class Foo {
public:
    Foo(const std::string& msg) {...}
    std::string msg;
};

static void fooSetMsg(Foo& self, const std::string& msg) {
    self.msg = msg;
}

static const std::string& msg fooGetMsg(Foo& self) {
    return self.msg;
}

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add class variable as a read write Wren class property
    cls.propExt<&fooGetMsg, &fooSetMsg>("msg");
}

function propReadonlyExt #

template <auto Getter>
inline void propReadonlyExt(
    std::string name
)

Add a read-only property to this class via a getter.

See: propReadonly

This essentially creates the same thing as varReadonly() but instead of using pointer to the class field, it uses a static getter. The setter and does not have to belong to the class itself, but must be static function, and must accept the class as a reference as a first parameter.

class Foo {
public:
    Foo(const std::string& msg) {...}
    std::string msg;
};

static const std::string& msg fooGetMsg(Foo& self) {
    return self.msg;
}

int main() {
    ...
    wren::VM vm;
    auto& m = vm.module("mymodule");

    // Add class "Foo"
    auto& cls = m.klass<Foo>("Foo");

    // Define constructor (you can only specify one constructor)
    cls.ctor<const std::string&>();

    // Add class variable as a read only Wren class property
    cls.propReadonlyExt<&fooGetMsg>("msg");
}

Updated on 17 October 2023 at 12:26:25 UTC