8000 Add pair_conversion registration helper with unit test by totalgee · Pull Request #482 · ChaiScript/ChaiScript · GitHub
[go: up one dir, main page]

Skip to content

Add pair_conversion registration helper with unit test #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/chaiscript/dispatchkit/type_conversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,23 @@ namespace chaiscript

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);
}

template<typename Left, typename Right>
Type_Conversion pair_conversion()
{
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
const std::pair<Boxed_Value, Boxed_Value> &from_pair = detail::Cast_Helper<const std::pair<Boxed_Value, Boxed_Value> &>::cast(t_bv, nullptr);

auto pair = std::make_pair(
detail::Cast_Helper<Left>::cast(from_pair.first, nullptr),
detail::Cast_Helper<Right>::cast(from_pair.second, nullptr)
);

return Boxed_Value(std::move(pair));
};

return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::pair<Boxed_Value, Boxed_Value>>(), user_type<std::pair<Left, Right>>(), func);
}
}


Expand Down
23 changes: 23 additions & 0 deletions unittests/compiled_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,29 @@ TEST_CASE("Map conversions")
}


TEST_CASE("Pair conversions")
{
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
chai.add(chaiscript::pair_conversion<std::string, std::string>());
chai.add(chaiscript::pair_conversion<int, double>());

{
const auto p = chai.eval<std::pair<std::string, std::string>>(R"cs(
Pair("chai", "script");
)cs");
CHECK(p.first == std::string{"chai"});
CHECK(p.second == "script");
}
{
const auto p = chai.eval<std::pair<int, double>>(R"cs(
Pair(5, 3.14);
)cs");
CHECK(p.first == 5);
CHECK(p.second == Approx(3.14));
}
}


TEST_CASE("Parse floats with non-posix locale")
{
#ifdef CHAISCRIPT_MSVC
Expand Down
0