Add future support, and fix returning of r-values

This commit is contained in:
Jason Turner 2015-01-06 14:41:36 -07:00
parent a32a180a06
commit 52d03a66b1
4 changed files with 51 additions and 0 deletions

View File

@ -19,6 +19,11 @@
#include "dispatchkit/bootstrap_stl.hpp"
#include "dispatchkit/boxed_value.hpp"
#ifndef CHAISCRIPT_NO_THREADS
#include <future>
#endif
/// @file
///
/// This file generates the standard library that normal ChaiScript usage requires.
@ -40,6 +45,11 @@ namespace chaiscript
lib->add(standard_library::map_type<std::map<std::string, Boxed_Value> >("Map"));
lib->add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
#ifndef CHAISCRIPT_NO_THREADS
lib->add(standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future"));
lib->add(chaiscript::fun<std::future<Boxed_Value> (const std::function<chaiscript::Boxed_Value ()> &)>([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async");
#endif
return lib;
}

View File

@ -564,6 +564,25 @@ namespace chaiscript
return m;
}
/// Add a MapType container
/// http://www.sgi.com/tech/stl/Map.html
template<typename FutureType>
ModulePtr future_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
m->add(user_type<FutureType>(), type);
m->add(fun(&FutureType::valid), "valid");
m->add(fun(&FutureType::get), "get");
m->add(fun(&FutureType::wait), "wait");
return m;
}
}
}
}

View File

@ -37,6 +37,11 @@ namespace chaiscript
{
return const_var(r);
}
static Boxed_Value handle(Ret &&r)
{
return Boxed_Value(std::move(r));
}
};
template<typename Ret>

17
unittests/future.chai Normal file
View File

@ -0,0 +1,17 @@
var func = fun(){
var ret = 0;
for (var i = 0; i < 1000000; ++i) {
ret += i;
}
return ret;
}
var fut1 := async(func);
var fut2 := async(func);
var fut3 := async(func);
var fut4 := async(func);
// simply executing without crashing is good enough for this test
print(" ${fut1.get()} ${fut2.get()} ${fut3.get()} ${fut4.get()}")