Merge pull request #264 from ELynx/develop

Expose std::vector and std::list resize, reserve and capacity methods
This commit is contained in:
Jason Turner
2016-04-15 08:18:13 -06:00
4 changed files with 70 additions and 0 deletions

View File

@@ -292,6 +292,40 @@ namespace chaiscript
}
/// Add container resize concept to the given ContainerType
/// http://www.cplusplus.com/reference/stl/
template<typename ContainerType>
void resizable_type(const std::string &/*type*/, Module& m)
{
m.add(fun([](ContainerType *a, typename ContainerType::size_type n, const typename ContainerType::value_type& val) { return a->resize(n, val); } ), "resize");
m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->resize(n); } ), "resize");
}
template<typename ContainerType>
ModulePtr resizable_type(const std::string &type)
{
auto m = std::make_shared<Module>();
resizable_type<ContainerType>(type, *m);
return m;
}
/// Add container reserve concept to the given ContainerType
/// http://www.cplusplus.com/reference/stl/
template<typename ContainerType>
void reservable_type(const std::string &/*type*/, Module& m)
{
m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->reserve(n); } ), "reserve");
m.add(fun([](const ContainerType *a) { return a->capacity(); } ), "capacity");
}
template<typename ContainerType>
ModulePtr reservable_type(const std::string &type)
{
auto m = std::make_shared<Module>();
reservable_type<ContainerType>(type, *m);
return m;
}
/// Add container concept to the given ContainerType
/// http://www.sgi.com/tech/stl/Container.html
template<typename ContainerType>
@@ -581,6 +615,7 @@ namespace chaiscript
front_insertion_sequence_type<ListType>(type, m);
back_insertion_sequence_type<ListType>(type, m);
sequence_type<ListType>(type, m);
resizable_type<ListType>(type, m);
container_type<ListType>(type, m);
default_constructible_type<ListType>(type, m);
assignable_type<ListType>(type, m);
@@ -612,6 +647,8 @@ namespace chaiscript
back_insertion_sequence_type<VectorType>(type, m);
sequence_type<VectorType>(type, m);
random_access_container_type<VectorType>(type, m);
resizable_type<VectorType>(type, m);
reservable_type<VectorType>(type, m);
container_type<VectorType>(type, m);
default_constructible_type<VectorType>(type, m);
assignable_type<VectorType>(type, m);

View File

@@ -0,0 +1,13 @@
load_module("stl_extra");
auto list = List();
list.resize(2);
assert_equal(list.size(), 2);
list.resize(6, "AAA");
assert_equal(list.back(), "AAA");
list.resize(0);
assert_equal(list.size(), 0);

View File

@@ -0,0 +1,7 @@
load_module("stl_extra");
auto uint16v = u16vector();
uint16v.reserve(5);
assert_true(uint16v.capacity() >= 5);

View File

@@ -0,0 +1,13 @@
load_module("stl_extra");
auto uint16v = u16vector();
uint16v.resize(2);
assert_equal(uint16v.size(), 2);
uint16v.resize(6, 3);
assert_equal(uint16v[5], 3);
uint16v.resize(0);
assert_equal(uint16v.size(), 0);