parent
7f4ef8d8fd
commit
bde2a45384
@ -90,6 +90,13 @@ A helper function exists for strongly typed and ChaiScript `Vector` function con
|
|||||||
chai.add(chaiscript::vector_conversion<std::vector<int>>());
|
chai.add(chaiscript::vector_conversion<std::vector<int>>());
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A helper function also exists for strongly typed and ChaiScript `Map` function conversion definition:
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
This allows you to pass a ChaiScript function to a function requiring `std::vector<int>`
|
This allows you to pass a ChaiScript function to a function requiring `std::vector<int>`
|
||||||
|
|
||||||
## Adding Objects
|
## Adding Objects
|
||||||
|
@ -592,6 +592,24 @@ namespace chaiscript
|
|||||||
return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::vector<Boxed_Value>>(), user_type<To>(), func);
|
return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::vector<Boxed_Value>>(), user_type<To>(), func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename To>
|
||||||
|
Type_Conversion map_conversion()
|
||||||
|
{
|
||||||
|
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
|
||||||
|
const std::map<std::string, Boxed_Value> &from_map = detail::Cast_Helper<const std::map<std::string, Boxed_Value> &>::cast(t_bv, nullptr);
|
||||||
|
|
||||||
|
To map;
|
||||||
|
for (const std::pair<std::string, Boxed_Value> &p : from_map) {
|
||||||
|
map.insert(std::make_pair(p.first, detail::Cast_Helper<typename To::mapped_type>::cast(p.second, nullptr)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Boxed_Value(std::move(map));
|
||||||
|
};
|
||||||
|
|
||||||
|
return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::map<std::string, Boxed_Value>>(), user_type<To>(), func);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -872,6 +872,7 @@ struct Returned_Converted_Config
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("Return of converted type from script")
|
TEST_CASE("Return of converted type from script")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript chai;
|
||||||
@ -914,3 +915,27 @@ TEST_CASE("Return of converted type from script")
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int get_value_a(const std::map<std::string, int> &t_m)
|
||||||
|
{
|
||||||
|
return t_m.at("a");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("Map conversions")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript chai;
|
||||||
|
chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
||||||
|
chai.add(chaiscript::fun(&get_value_a), "get_value_a");
|
||||||
|
|
||||||
|
const auto c = chai.eval<int>(R"(
|
||||||
|
var m = ["a": 42];
|
||||||
|
get_value_a(m);
|
||||||
|
)");
|
||||||
|
|
||||||
|
CHECK(c == 42);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user