Return vector of vector(s) from script


#1

Is it possible to return a vector<vector<double>> from chaiscript?

chai:

def sequence() {
  return [[1.2, 5.7], [5.0, 3.1]];
}

c++:


chai = new chaiscript::ChaiScript();
//
chai->add (chaiscript::type_conversion<vector<chaiscript::Boxed_Value>, vector<vector<double>>> ([](const vector<chaiscript::Boxed_Value> &t_bvs) {
  vector<std::vector<double>> ret;
  for (const auto& bv : t_bvs) {
    ret.emplace_back (chaiscript::boxed_cast<vector<double>> (bv));
  }
  return ret;
}));
//
function<vector<std::vector<double>>()> sequence = chai->eval<function<vector<vector<double>>()>> ("sequence");
auto return = sequence();

The above fails with ```chaiscript::detail::exception::bad_any_cast()``;`

All of the following code works though:


// The vector of vectors
//
vector<vector<double>> vd = {
  { 1.0, 5.0 },
  { 5.0, 6.5 },
  { 7.0, 9.0 }
};
//
// Testing vector of doubles
//
chaiscript::Boxed_Value bv1 (vd [0]);
auto cv1 = chaiscript::boxed_cast<vector<double>> (bv1);
//
// Testing vector of vector of doubles
//
chaiscript::Boxed_Value bv2 (vd);
auto cv2 = chaiscript::boxed_cast<vector<vector<double>>> (bv2);
//
// Testing vector of Boxed_Value(s) with vector of doubles
//
std::vector<chaiscript::Boxed_Value> bv3 = {
  chaiscript::Boxed_Value (vd [0]),
  chaiscript::Boxed_Value (vd [1]),
  chaiscript::Boxed_Value (vd [2])
};
//  
for (const auto& bv : bv3) {
  auto cv = chaiscript::boxed_cast<vector<double>> (bv);
}

Not sure what else I’m missing…


#2

ok. got it to work.

Just had to use the ChaiScript instance’s boxed_cast() chai->boxed_cast since it has the added vector<double> conversion needed.

Makes sense.