Merge c7cddbe20b2caf5f9609d2ea357dd88eb8c5ba6b into 3bf72420c2a66531e1ff2f76352542da895b356a

This commit is contained in:
Rob Loach 2018-03-17 20:22:12 +00:00 committed by GitHub
commit 00bafdb8f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 13 deletions

View File

@ -219,6 +219,51 @@ namespace chaiscript
} }
/// Add random_access_advance_container concept to the given ContainerType, using advance.
/// http://www.sgi.com/tech/stl/RandomAccessContainer.html
template<typename ContainerType>
void random_access_advance_container_type(const std::string &/*type*/, Module& m)
{
//In the interest of runtime safety for the m, we prefer the at() method for [] access,
//to throw an exception in an out of bounds condition.
m.add(
fun(
[](ContainerType &c, int index) -> typename ContainerType::reference {
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
/// during dispatch. reevaluate
auto itr = c.begin();
auto end = c.end();
if (index < 0 || std::distance(itr, end) < index)
{
throw std::range_error("Desired index out of range");
}
std::advance(itr, index);
return *itr;
}), "[]");
m.add(
fun(
[](const ContainerType &c, int index) -> typename ContainerType::const_reference {
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
/// during dispatch. reevaluate
auto itr = c.begin();
auto end = c.end();
if (index < 0 || std::distance(itr, end) < index)
{
throw std::range_error("Desired index out of range");
}
std::advance(itr, index);
return *itr;
}), "[]");
}
template<typename ContainerType>
ModulePtr random_access_advance_container_type(const std::string &type)
{
auto m = std::make_shared<Module>();
random_access_advance_container_type<ContainerType>(type, *m);
return m;
}
/// Add assignable concept to the given ContainerType /// Add assignable concept to the given ContainerType
/// http://www.sgi.com/tech/stl/Assignable.html /// http://www.sgi.com/tech/stl/Assignable.html
@ -591,6 +636,7 @@ namespace chaiscript
default_constructible_type<ListType>(type, m); default_constructible_type<ListType>(type, m);
assignable_type<ListType>(type, m); assignable_type<ListType>(type, m);
input_range_type<ListType>(type, m); input_range_type<ListType>(type, m);
random_access_advance_container_type<ListType>(type, m);
} }
template<typename ListType> template<typename ListType>
ModulePtr list_type(const std::string &type) ModulePtr list_type(const std::string &type)

View File

@ -7,6 +7,5 @@ x.push_back("A")
assert_equal(3, x.front()); assert_equal(3, x.front());
assert_equal("A", x.back()); assert_equal("A", x.back());
// Random access with List objects.
assert_equal("A", x[1])