Giter VIP home page Giter VIP logo

Comments (10)

satoren avatar satoren commented on August 19, 2024

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.

Flamefire avatar Flamefire commented on August 19, 2024

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.

satoren avatar satoren commented on August 19, 2024

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.

Flamefire avatar Flamefire commented on August 19, 2024

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.

satoren avatar satoren commented on August 19, 2024

type checking is done once for element since 4a6f5ab
table type check is twice for avoid lua API panic.

from kaguya.

satoren avatar satoren commented on August 19, 2024

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.

Flamefire avatar Flamefire commented on August 19, 2024

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.

satoren avatar satoren commented on August 19, 2024

kaguya_api_benchmark______::table_to_vector min time:0.076
kaguya_api_benchmark______::table_to_vector_with_typecheck min time:0.208

from kaguya.

Flamefire avatar Flamefire commented on August 19, 2024

Ouch... Almost 3 times slower :/
I can try to come up with something to make it faster, if you like

from kaguya.

satoren avatar satoren commented on August 19, 2024

Please try it.

from kaguya.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.