Comments (10)
use typeTest or weakTypeTest.
If you want to distinguish between the string and the number, use typeTest.
std::vector<unsigned> GetValues()
{
kaguya::LuaRef getValues= lua["getValues"];
if(getValues.type() == LUA_TFUNCTION)
{
kaguya::LuaRef array = getValues();
if(array.typeTest<std::vector<unsigned> >())
{
return array;//valid
}
else
{
throw std::runtime_error("invalid array");
}
}
return std::vector<unsigned>();
}
If you want type check at per elements.
std::vector<unsigned> result;
std::vector<kaguya::LuaRef> array = getValues();
for(auto& elem : array)
{
if(elem.typeTest<unsigned>())
{
result.push_back(elem);
}
else
{
//invalid
}
}
return result;
from kaguya.
This is what I ended up with. However this is quite some boilerplate code (even the check, if it is a function) and I was hoping that there is some library support. E.g. getValues.call<std::vector<unsigned> >()
could result in a lua error if the type is wrong.
Also doing the check this way does it twice: Once for checking and once before it is converted. Maybe something like getValues().assertConvertible()
and/or getValues().checkConvertible(bool& wasValid)
?
from kaguya.
like this?
bool typevalid;
auto result = getValues().get<std::vector<unsigned> >(typevalid, false);
if(typevalid)
{
return result;//valid
}
else
{
return std::vector<unsigned>();
}
from kaguya.
Yes, pretty nice solution.
Does this really work? because getValues()
returns FunctionResults
for which you did not implement this get function or did I miss anything? Also the code needs to be duplicated in LuaRef, LuaTableRef and FuntionResults. maybe you can improve that (just style/maintainability issue though)
Lastly: What about performance? Type checking is done twice. Could you check the performance of the type check for getting a vector of some (~100?) ints that need to be converted from string (kind of bad, but still reasonable case)?
from kaguya.
type checking is done once for element since 4a6f5ab
table type check is twice for avoid lua API panic.
from kaguya.
Example of the type checking of performance cost.
bool was_valid;
std::vector<double> r = lua_table.get<std::vector<double>>(was_valid);
lua api | call count | reason |
---|---|---|
lua_type() | 2 | table type check |
lua_next() | table element count * 2 | element loop for type check and get value |
lua_pop() | table element count * 2 | element loop |
lua_isinteger() | table element count | type check for key |
lua_tonumber() | table element count | get value |
lua_isnumber() | table element count | type check for value |
lua_gettop | many | |
lua_settop | many |
from kaguya.
Looks quite good. I still think, that this can be improved by combining the type check and get value parts completely. So it actually gets the values but optionally sets the was_valid
variable to false as it iterates over the table. This would save the factor 2 in the call count and some instantiations for example of LuaStackRef
But well, do you already have benchmark results for the call with was_valid
vs without?
from kaguya.
kaguya_api_benchmark______::table_to_vector min time:0.076
kaguya_api_benchmark______::table_to_vector_with_typecheck min time:0.208
from kaguya.
Ouch... Almost 3 times slower :/
I can try to come up with something to make it faster, if you like
from kaguya.
Please try it.
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.