Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global function as member function. #29

Closed
aster2013 opened this issue Jun 29, 2016 · 8 comments
Closed

Global function as member function. #29

aster2013 opened this issue Jun 29, 2016 · 8 comments

Comments

@aster2013
Copy link
Contributor

When a global function with first argument is Object& or Object*, Can I treat is as a member function for usertype. e.g.

float Length(Vector2& vec)
{
    return sqrtf(vec.x * vec.x + vec.y * vec.y);
}

In Lua:

local vec = Vector2:new(10, 10)
local length = vec:Length()

Now I can register it as a static function for usertype.

@satoren
Copy link
Owner

satoren commented Jun 29, 2016

Yes.
Can use lambda,functor,static function if first argument is same type.

state["A"].setClass(kaguya::UserdataMetatable<A>()
    .setConstructors<A()>()
    .addStaticFunction("lambdafn", [](A* self,int v){self->setValue(v);})
    );
local a = ABC.new()
a:lambdafn(3) --equal setValue(3)
a.lambdafn(a,3) --equal to above

@aster2013
Copy link
Contributor Author

addStaticFunction may not the best name for it situation. I prefer to addMemberFunction.

@satoren
Copy link
Owner

satoren commented Jun 30, 2016

If implement addMemberFunction for global function, need first argument type check.
but it implement for C++03 is difficult.

@aster2013
Copy link
Contributor Author

aster2013 commented Jun 30, 2016

Yes, another question:
When class has setter and getter function, can I treat it as property?

class Class
{
    void SetValue(int value);
    int GetValue() const;
};

.addProperty("value", &Class::GetValue, &Class::SetValue)

@satoren
Copy link
Owner

satoren commented Jun 30, 2016

Can not now. Try to make because it is easy.

@aster2013
Copy link
Contributor Author

Thinks, but as convention, it is better make getter as first argumention.

template<typename GetType, typename SetType>
        UserdataMetatable& addProperty(const char* name, GetType(class_type::*getter)()const, void (class_type::*setter)(SetType) ) {}

satoren added a commit that referenced this issue Jul 1, 2016
@satoren
Copy link
Owner

satoren commented Jul 1, 2016

It's changed.

@aster2013
Copy link
Contributor Author

aster2013 commented Jul 2, 2016

Great!, but

template<typename SetType, typename GetType>
UserdataMetatable& addProperty(const char* name, GetType(class_type::*getter)()const, void (class_type::*setter)(SetType))

First template arguments is SetType, It is misleading.

satoren added a commit that referenced this issue Jul 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants