Initial support for container ranges. Still half baked, but works.
This commit is contained in:
parent
ca20bf7eb5
commit
a952bcd066
@ -2,9 +2,56 @@
|
|||||||
#define __bootstrap_stl_hpp__
|
#define __bootstrap_stl_hpp__
|
||||||
|
|
||||||
#include "dispatchkit.hpp"
|
#include "dispatchkit.hpp"
|
||||||
|
#include "register_function.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace dispatchkit
|
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>
|
template<typename ContainerType>
|
||||||
void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type)
|
void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type)
|
||||||
{
|
{
|
||||||
@ -46,6 +93,7 @@ namespace dispatchkit
|
|||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
||||||
{
|
{
|
||||||
|
bootstrap_input_range<ContainerType>(system, type);
|
||||||
bootstrap_container<ContainerType>(system, type);
|
bootstrap_container<ContainerType>(system, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
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
|
#endif
|
||||||
|
15
samples/range.chai
Normal file
15
samples/range.chai
Normal 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)); } );
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user