Comments (15)
I don't know about detect function have default parameter.
other way is use overload.
struct Foo {
void func(int param = 42) { std::cout << param<<std::endl; }
};
state["Foo"].setClass(kaguya::ClassMetatable<Foo>()
.addMemberFunction("func", &Foo::func)
.addStaticFunction("func", [](Foo& self) {self.func(); })
);
state["foo"] = Foo();
state("foo:func()");//42
state("foo:func(1)");//1
);
from kaguya.
When can support default parameters?
from kaguya.
When i hit upon a good idea.
from kaguya.
Added support for Default parameters
/**
* @name KAGUYA_FUNCTION_OVERLOADS
* @brief Generate wrapper function object for count based overloads. Include default arguments parameter function
* @param GENERATE_NAME generate function object name
* @param FNAME target function name
* @param MINARG minimum arguments count
* @param MAXARG maximum arguments count
*/
#define KAGUYA_FUNCTION_OVERLOADS(GENERATE_NAME,FNAME, MINARG, MAXARG) KAGUYA_FUNCTION_OVERLOADS_INTERNAL(GENERATE_NAME,FNAME, MINARG, MAXARG)
/**
* @name KAGUYA_FUNCTION_OVERLOADS
* @brief Generate wrapper function object for count based overloads. Include default arguments parameter function
* @param GENERATE_NAME generate function object name
* @param CLASS target class name
* @param FNAME target function name
* @param MINARG minimum arguments count
* @param MAXARG maximum arguments count
*/
#define KAGUYA_MEMBER_FUNCTION_OVERLOADS(GENERATE_NAME,CLASS,FNAME, MINARG, MAXARG) KAGUYA_MEMBER_FUNCTION_OVERLOADS_INTERNAL(GENERATE_NAME,CLASS,FNAME, MINARG, MAXARG)
example
//free function
int defargfn(int a = 3, int b = 2, int c = 1)
{
return a*b*c;
}
KAGUYA_FUNCTION_OVERLOADS(defargfn_wrapper, defargfn,0,3)
state["defarg"] = kaguya::function(defargfn_wrapper);
state.dostring("assert(defarg() == 6)");
state.dostring("assert(defarg(6) == 12)");
state.dostring("assert(defarg(6,5) == 30)");
state.dostring("assert(defarg(2,2,2) == 8)");
//member function
struct TestClass
{
int defargfn(int a = 3, int b = 2, int c = 1)
{
return a*b*c;
}
}
KAGUYA_MEMBER_FUNCTION_OVERLOADS(defargfn_wrapper, TestClass, default_arg, 0, 3)
state["TestClass"].setClass(kaguya::UserdataMetatable<TestClass>()
.setConstructors<TestClass()>()
.addFunction("defarg", defargfn_wrapper)
);
state.dostring("test = TestClass.new()");
state.dostring("assert(test:defargfn() == 6)");
state.dostring("assert(test:defargfn(6) == 12)");
state.dostring("assert(test:defargfn(6,5) == 30)");
state.dostring("assert(test:defargfn(2,2,2) == 8)");
from kaguya.
Build error on VS2015 when function return void.
'kaguya::util::push_args': function does not take 2 arguments
from kaguya.
Add void return version , because can not fix on quick.
I put together they, If it can created.
from kaguya.
When functions with different signatures, these macros can not work. for example.
struct Test
{
void add(int a, int b = 0);
void add(const std::string& a, const std::string& b = "");
};
from kaguya.
Here is my current solution:
static void addInt(Test* self, int a, int b = 0)
{
self->add(a, b);
}
static void addString(Test* self, const std::string& a, const std::string& b = "")
{
self->add(a, b);
}
...
KAGUYA_VOID_FUNCTION_OVERLOADS(addInt, addInt, 1, 2);
KAGUYA_VOID_FUNCTION_OVERLOADS(addString, addString, 1, 2);
...
.addOverloadedFunctions("add", addInt, addString)
from kaguya.
Hi, where you pushed, not on the master?
from kaguya.
I'm waited test pass on my CI.
Usage was little changed.
- KAGUYA_FUNCTION_OVERLOADS and KAGUYA_MEMBER_FUNCTION_OVERLOADS is unusable in function scope.
- KAGUYA_VOID_FUNCTION_OVERLOADS and KAGUYA_VOID_MEMBER_FUNCTION_OVERLOADS is removed.
- add KAGUYA_FUNCTION_OVERLOADS_WITH_SIGNATURE and KAGUYA_MEMBER_FUNCTION_OVERLOADS_WITH_SIGNATURE
- Need round brackets on use
example
KAGUYA_FUNCTION_OVERLOADS(defargfn_wrapper, defargfn, 0, 3);
KAGUYA_FUNCTION_OVERLOADS_WITH_SIGNATURE(defargfn_wrapper_with_sig, defargfn, 0, 3,int(int,int,int));
KAGUYA_MEMBER_FUNCTION_OVERLOADS_WITH_SIGNATURE(mem_defargfn_wrapper_with_sig, TestClass, default_arg, 0, 3,int(TestClass::*)(int,int,int));
...
state["defargfn"] = kaguya::function(defargfn_wrapper());
state["defargfn2"] = kaguya::function(defargfn_wrapper_with_sig());
state["TestClass"].setClass(kaguya::UserdataMetatable<TestClass>()
.addFunction("defargfn", mem_defargfn_wrapper_with_sig())
);
from kaguya.
Bug:
For example, a overloaded function with following arguments:
void Test(const std::string& a = "hello");
We need a const reference as input, but the lua_type_traits<std::string>::get(...)
returns std::string
.
So bug in following code:
namespace nativefunction
{
template<size_t INDEX,typename F>
typename util::ArgumentType<INDEX, F>::type getArgument(lua_State* state)
{
return lua_type_traits<typename util::ArgumentType<INDEX, F>::type>::get(state, INDEX + 1);
}
// explanded:
template<size_t INDEX,typename F>
const std::string& getArgument(lua_State* state)
{
std::string value = lua_type_traits<std::string>::get(state, INDEX + 1);
return value; // <--- Error, return a temporary variable.
}
}
from kaguya.
Thank you for report.
from kaguya.
And another bug:
When some function with const char* argument, kaguya will report error:
cannot convert argument 2 from 'kaguya::lua_type_traits<const char*,void>::get_type' to 'const char *'
from kaguya.
template<> struct lua_type_traits<const char*> {
// typedef std::string get_type;
typedef const char* get_type;
typedef const char* push_type;
from kaguya.
Thanks!
from kaguya.
Related Issues (20)
- Kaguya does not work properly with dynamic libraries
- Generate kaguya.hpp and check your error! HOT 2
- request: custom allocator support
- Result of checkErrorAndThrow is ignored
- Possible to add overloaded constructors at runtime?
- How to register class static member function with default arguments? HOT 1
- An object is not automatically casted into a function parameter HOT 1
- error: type mismatch in diferent dynamic lib HOT 1
- Can't bind functor returned by std::bind HOT 1
- About class inheritance
- std::function and self HOT 3
- Check range of integral when converting from number
- how can I check function has timeout HOT 1
- Reject extra arguments to .new(), etc.
- Returning reference to class needs a copy constructor?
- can not assign a table from lua to a class member attribute
- Returning nil in multiple returns HOT 2
- get Current File/Line of Lua Code HOT 3
- How do I use a variable number of parameters HOT 2
- kaguya call lua HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kaguya.