About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility and modern design in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Due to an increased release schedule, and the nature of ChaiScript being a header-only library, binary releases will be made less often. Previous releases can be found on github.

Version: 4.2.0 Released: 12/1/2012
(Requires Boost)
Source (tar.bz2)
Source (zip)

Version: 5.2.0 Released: 12/1/2012
(Requires C++11 Compiler)
Source (tar.bz2)
Source (zip)

std::vector<std::string> to a chaiscript vector

Hi,i try to get data from c++ function by a vector, but how set std::vector to a chaiscript vector ?

c++ code :

std::vector<std::string> getVect()
{
    std::vector<std::string> vect;
    vect.push_back("test 1");
    vect.push_back("test 2");
    vect.push_back("test 3");
    vect.push_back("test 4");
    return vect;
}
chai.add(fun(&getVect),"getVect());

chaiscript code :

     var vect = Vector();
     vect = getVect(); // probleme

Thank.

There's no built in way to do

There's no built in way to do conversions between typed vectors and vectors of Boxed_Value. We tried to implement that, but it introduced too much overhead in the code.

So, you'll need to do it manually in your code. You need to either just return a vector<Boxed_Value> or provide a function that converts to it:

vector<Boxed_Value> getVect()
{
  vector<Boxed_Value> v;
  v.push_back(var(1));
  v.push_back(var(string("hello world"))); // mixed chaiscript types are allowed
  return v;
}

Or convert:

vector<Boxed_Value> toVec(const vector<int> &v)
{
  vector<Boxed_Value> retval;
  for (vector<int>::const_iterator itr = v.begin();
       itr != v.end();
       ++v)
  {
    retval.push_back(var(*itr));
  }
 
  return retval;
}
 
ChaiScript chai;
chai.add(fun(&toVec), "toVec");
 
chai.eval("var v = toVec(getVect())");

I've been pondering adding something like the above into the utils library.

Don't forget to tell your friends about ChaiScript.

-Jason

Support ChaiScript by sharing it with your friends: