mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-02-07 18:26:49 +08:00
Merge c7cddbe20b2caf5f9609d2ea357dd88eb8c5ba6b into 3bf72420c2a66531e1ff2f76352542da895b356a
This commit is contained in:
commit
00bafdb8f2
@ -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)
|
||||||
|
|||||||
@ -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])
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user