Add future support, and fix returning of r-values
This commit is contained in:
@@ -19,6 +19,11 @@
|
|||||||
#include "dispatchkit/bootstrap_stl.hpp"
|
#include "dispatchkit/bootstrap_stl.hpp"
|
||||||
#include "dispatchkit/boxed_value.hpp"
|
#include "dispatchkit/boxed_value.hpp"
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_NO_THREADS
|
||||||
|
#include <future>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/// @file
|
/// @file
|
||||||
///
|
///
|
||||||
/// This file generates the standard library that normal ChaiScript usage requires.
|
/// 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::map_type<std::map<std::string, Boxed_Value> >("Map"));
|
||||||
lib->add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
|
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;
|
return lib;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -564,6 +564,25 @@ namespace chaiscript
|
|||||||
|
|
||||||
return m;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -37,6 +37,11 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return const_var(r);
|
return const_var(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Boxed_Value handle(Ret &&r)
|
||||||
|
{
|
||||||
|
return Boxed_Value(std::move(r));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
|
17
unittests/future.chai
Normal file
17
unittests/future.chai
Normal 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()}")
|
Reference in New Issue
Block a user