From 49ef5306a9c7b7f9c7c5dde37c2f52b9be85366d Mon Sep 17 00:00:00 2001 From: ELynx Date: Fri, 15 Apr 2016 13:15:30 +0300 Subject: [PATCH] Add resize to stl list and vector; add reserve to stl vector --- .../chaiscript/dispatchkit/bootstrap_stl.hpp | 24 +++++++++++++++++++ unittests/list_resize.chai | 13 ++++++++++ unittests/vector_reserve.chai | 7 ++++++ unittests/vector_resize.chai | 13 ++++++++++ 4 files changed, 57 insertions(+) create mode 100644 unittests/list_resize.chai create mode 100644 unittests/vector_reserve.chai create mode 100644 unittests/vector_resize.chai diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 7ba588b7..3c93c919 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -292,6 +292,27 @@ namespace chaiscript } + /// Add container resize concept to the given ContainerType + /// http://www.cplusplus.com/reference/stl/ + template + ModulePtr resizable_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + { + m->add(fun([](ContainerType *a, ContainerType::size_type n, ContainerType::value_type val) { return a->resize(n, val); } ), "resize"); + m->add(fun([](ContainerType *a, ContainerType::size_type n) { return a->resize(n, ContainerType::value_type()); } ), "resize"); + return m; + } + + + /// Add container reserve concept to the given ContainerType + /// http://www.cplusplus.com/reference/stl/ + template + ModulePtr reservable_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + { + m->add(fun([](ContainerType *a, ContainerType::size_type n) { return a->reserve(n); } ), "reserve"); + return m; + } + + /// Add container concept to the given ContainerType /// http://www.sgi.com/tech/stl/Container.html template @@ -581,6 +602,7 @@ namespace chaiscript front_insertion_sequence_type(type, m); back_insertion_sequence_type(type, m); sequence_type(type, m); + resizable_type(type, m); container_type(type, m); default_constructible_type(type, m); assignable_type(type, m); @@ -612,6 +634,8 @@ namespace chaiscript back_insertion_sequence_type(type, m); sequence_type(type, m); random_access_container_type(type, m); + resizable_type(type, m); + reservable_type(type, m); container_type(type, m); default_constructible_type(type, m); assignable_type(type, m); diff --git a/unittests/list_resize.chai b/unittests/list_resize.chai new file mode 100644 index 00000000..f34e77aa --- /dev/null +++ b/unittests/list_resize.chai @@ -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[5], "AAA"); + +list.resize(0); +assert_equal(list.size(), 0); + diff --git a/unittests/vector_reserve.chai b/unittests/vector_reserve.chai new file mode 100644 index 00000000..764002af --- /dev/null +++ b/unittests/vector_reserve.chai @@ -0,0 +1,7 @@ +load_module("stl_extra"); + +auto uint16v = u16vector(); + +uint16v.reserve(5); +assert_true(uint16v.size() >= 5); + diff --git a/unittests/vector_resize.chai b/unittests/vector_resize.chai new file mode 100644 index 00000000..577ae6df --- /dev/null +++ b/unittests/vector_resize.chai @@ -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); +