Initial support for container ranges. Still half baked, but works.

This commit is contained in:
Jason Turner 2009-06-14 16:55:09 +00:00
parent ca20bf7eb5
commit a952bcd066
3 changed files with 69 additions and 0 deletions

View File

@ -2,9 +2,56 @@
#define __bootstrap_stl_hpp__
#include "dispatchkit.hpp"
#include "register_function.hpp"
namespace dispatchkit
{
template<typename Container>
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<typename Container::iterator>::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<typename ContainerType>
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
{
system.register_function(build_constructor<Input_Range<ContainerType>, ContainerType &>(), "range");
register_function(system, &Input_Range<ContainerType>::empty, "empty");
register_function(system, &Input_Range<ContainerType>::popFront, "popFront");
register_function(system, &Input_Range<ContainerType>::front, "front");
}
template<typename ContainerType>
void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type)
{
@ -46,6 +93,7 @@ namespace dispatchkit
template<typename ContainerType>
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
{
bootstrap_input_range<ContainerType>(system, type);
bootstrap_container<ContainerType>(system, type);
}

View File

@ -45,6 +45,12 @@ namespace dispatchkit
{
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
}
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
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<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
}
}
#endif

15
samples/range.chai Normal file
View File

@ -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)); } );