diff --git a/dispatchkit/bootstrap_stl.hpp b/dispatchkit/bootstrap_stl.hpp index 02e476b..f5fecb9 100644 --- a/dispatchkit/bootstrap_stl.hpp +++ b/dispatchkit/bootstrap_stl.hpp @@ -2,9 +2,56 @@ #define __bootstrap_stl_hpp__ #include "dispatchkit.hpp" +#include "register_function.hpp" + namespace dispatchkit { + template + struct Input_Range + { + Input_Range(Container &c) + : m_begin(c.begin()), m_end(c.end()) + { + } + + bool empty() const + { + return m_begin == m_end; + } + + void popFront() + { + if (empty()) + { + throw std::range_error("Range empty"); + } + ++m_begin; + } + + typename std::iterator_traits::reference front() const + { + if (empty()) + { + throw std::range_error("Range empty"); + } + return *m_begin; + } + + typename Container::iterator m_begin; + typename Container::iterator m_end; + }; + + template + void bootstrap_input_range(Dispatch_Engine &system, const std::string &type) + { + system.register_function(build_constructor, ContainerType &>(), "range"); + + register_function(system, &Input_Range::empty, "empty"); + register_function(system, &Input_Range::popFront, "popFront"); + register_function(system, &Input_Range::front, "front"); + } + template void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type) { @@ -46,6 +93,7 @@ namespace dispatchkit template void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type) { + bootstrap_input_range(system, type); bootstrap_container(system, type); } diff --git a/dispatchkit/register_function.hpp b/dispatchkit/register_function.hpp index b4d6dff..7daf4b9 100644 --- a/dispatchkit/register_function.hpp +++ b/dispatchkit/register_function.hpp @@ -45,6 +45,12 @@ namespace dispatchkit { s.register_function(boost::function(f), name); } + + template + void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const, const std::string &name) + { + s.register_function(boost::function(f), name); + } } #endif diff --git a/samples/range.chai b/samples/range.chai new file mode 100644 index 0000000..6043b1d --- /dev/null +++ b/samples/range.chai @@ -0,0 +1,15 @@ +def for_each(container, function) +{ + var range = range(container); + + while (!range.empty()) + { + function(range.front()); + range.popFront(); + } +} + +var vec = [1,2,3,4,5,6,7,8,9] + +for_each(vec, function(x) { print(to_string(x)); } ); +