From 2fbc377119ae802f644e19b5f67d816e23a0ab2e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 11 Jul 2015 18:36:07 -0600 Subject: [PATCH] More strongly typed handling of push_back wrapper Closes #192 --- .../chaiscript/dispatchkit/bootstrap_stl.hpp | 25 +++++++++++++++---- .../language/chaiscript_prelude.chai | 12 --------- src/stl_extra.cpp | 1 + 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index a763e48..1a03888 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -319,7 +319,7 @@ namespace chaiscript /// Add back insertion sequence concept to the given ContainerType /// http://www.sgi.com/tech/stl/BackInsertionSequence.html template - ModulePtr back_insertion_sequence_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared()) { typedef typename ContainerType::reference (ContainerType::*backptr)(); @@ -328,8 +328,16 @@ namespace chaiscript typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &); m->add(fun(static_cast(&ContainerType::push_back)), - []()->std::string{ - if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + [&]()->std::string{ + if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + m->eval( + "# Pushes the second value onto the container while making a clone of the value\n" + "def push_back(" + type + " container, x)\n" + "{ \n" + " container.push_back_ref(clone(x)) \n" + "} \n" + ); + return "push_back_ref"; } else { return "push_back"; @@ -345,7 +353,7 @@ namespace chaiscript /// Front insertion sequence /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html template - ModulePtr front_insertion_sequence_type(const std::string &, ModulePtr m = std::make_shared()) + ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared()) { typedef typename ContainerType::reference (ContainerType::*front_ptr)(); typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const; @@ -356,8 +364,15 @@ namespace chaiscript m->add(fun(static_cast(&ContainerType::front)), "front"); m->add(fun(static_cast(&ContainerType::push_front)), - []()->std::string{ + [&]()->std::string{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + m->eval( + "# Pushes the second value onto the front of container while making a clone of the value\n" + "def push_front(" + type + " container, x)\n" + "{ \n" + " container.push_front_ref(clone(x)) \n" + "} \n" + ); return "push_front_ref"; } else { return "push_front"; diff --git a/include/chaiscript/language/chaiscript_prelude.chai b/include/chaiscript/language/chaiscript_prelude.chai index 88835c5..0fe2a5a 100644 --- a/include/chaiscript/language/chaiscript_prelude.chai +++ b/include/chaiscript/language/chaiscript_prelude.chai @@ -127,18 +127,6 @@ def even(x) } -# Pushes the second value onto the container first value while making a clone of the value -def push_back(container, x) : call_exists(push_back_ref, container, x) -{ - container.push_back_ref(clone(x)) -} - -# Pushes the second value onto the front of the container first value while making a clone of the value -def push_front(container, x) : call_exists(push_front_ref, container, x) -{ - container.push_front_ref(clone(x)) -} - # Inserts the third value at the position of the second value into the container of the first # while making a clone. def insert_at(container, pos, x) diff --git a/src/stl_extra.cpp b/src/stl_extra.cpp index d991481..e391b18 100644 --- a/src/stl_extra.cpp +++ b/src/stl_extra.cpp @@ -26,6 +26,7 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extr auto module = chaiscript::bootstrap::standard_library::list_type >("List"); module->add(chaiscript::bootstrap::standard_library::vector_type >("u16vector")); + module->add(chaiscript::vector_conversion>()); return module; }