Drop boost::optional requirement

This commit is contained in:
Jason Turner 2011-09-11 06:56:15 -06:00
parent 0a9cb0cbe9
commit cd97880d70
3 changed files with 56 additions and 48 deletions

View File

@ -7,7 +7,6 @@
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_ #ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
#define CHAISCRIPT_DYNAMIC_OBJECT_HPP_ #define CHAISCRIPT_DYNAMIC_OBJECT_HPP_
#include <boost/optional.hpp>
namespace chaiscript namespace chaiscript
{ {
@ -66,12 +65,22 @@ namespace chaiscript
class Dynamic_Object_Function : public Proxy_Function_Base class Dynamic_Object_Function : public Proxy_Function_Base
{ {
public: public:
Dynamic_Object_Function(
const std::string &t_type_name,
const Proxy_Function &t_func)
: Proxy_Function_Base(t_func->get_param_types()),
m_type_name(t_type_name), m_func(t_func)
{
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
}
Dynamic_Object_Function( Dynamic_Object_Function(
const std::string &t_type_name, const std::string &t_type_name,
const Proxy_Function &t_func, const Proxy_Function &t_func,
const boost::optional<Type_Info> &t_ti = boost::optional<Type_Info>()) const Type_Info &t_ti)
: Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti)), : Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti)),
m_type_name(t_type_name), m_func(t_func), m_ti(t_ti) m_type_name(t_type_name), m_func(t_func), m_ti(new Type_Info(t_ti))
{ {
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0) assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)"); && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
@ -137,23 +146,18 @@ namespace chaiscript
private: private:
static std::vector<Type_Info> build_param_types( static std::vector<Type_Info> build_param_types(
const std::vector<Type_Info> &t_inner_types, boost::optional<Type_Info> t_objectti) const std::vector<Type_Info> &t_inner_types, const Type_Info& t_objectti)
{
if (t_objectti)
{ {
std::vector<Type_Info> types(t_inner_types); std::vector<Type_Info> types(t_inner_types);
assert(types.size() > 1); assert(types.size() > 1);
assert(types[1].bare_equal(user_type<Boxed_Value>())); assert(types[1].bare_equal(user_type<Boxed_Value>()));
types[1] = *t_objectti; types[1] = t_objectti;
return types; return types;
} else {
return t_inner_types;
}
} }
static bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name, static bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
const boost::optional<Type_Info> &ti) const std::shared_ptr<Type_Info> &ti)
{ {
static Type_Info doti = user_type<Dynamic_Object>(); static Type_Info doti = user_type<Dynamic_Object>();
if (bv.get_type_info().bare_equal(doti)) if (bv.get_type_info().bare_equal(doti))
@ -176,7 +180,7 @@ namespace chaiscript
} }
static bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name, static bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
const boost::optional<Type_Info> &ti) const std::shared_ptr<Type_Info> &ti)
{ {
if (bvs.size() > 0) if (bvs.size() > 0)
{ {
@ -188,7 +192,7 @@ namespace chaiscript
std::string m_type_name; std::string m_type_name;
Proxy_Function m_func; Proxy_Function m_func;
boost::optional<Type_Info> m_ti; std::shared_ptr<Type_Info> m_ti;
}; };

View File

@ -1117,19 +1117,23 @@ namespace chaiscript
} }
else { else {
boost::optional<chaiscript::Type_Info> ti;
try { try {
ti = t_ss.get_type(class_name); // Do know type name
} catch (const std::range_error &) {
// No biggie, the type name is just not known
}
t_ss.add(Proxy_Function t_ss.add(Proxy_Function
(new dispatch::detail::Dynamic_Object_Function(class_name, Proxy_Function (new dispatch::detail::Dynamic_Object_Function(class_name, Proxy_Function
(new dispatch::Dynamic_Proxy_Function(std::bind(chaiscript::eval::detail::eval_function, (new dispatch::Dynamic_Proxy_Function(std::bind(chaiscript::eval::detail::eval_function,
std::ref(t_ss), this->children.back(), std::ref(t_ss), this->children.back(),
t_param_names, std::placeholders::_1), static_cast<int>(numparams), this->children.back(), t_param_names, std::placeholders::_1), static_cast<int>(numparams), this->children.back(),
l_annotation, guard)), ti)), function_name); l_annotation, guard)), t_ss.get_type(class_name))), function_name);
} catch (const std::range_error &) {
// Do not know type name
t_ss.add(Proxy_Function
(new dispatch::detail::Dynamic_Object_Function(class_name, Proxy_Function
(new dispatch::Dynamic_Proxy_Function(std::bind(chaiscript::eval::detail::eval_function,
std::ref(t_ss), this->children.back(),
t_param_names, std::placeholders::_1), static_cast<int>(numparams), this->children.back(),
l_annotation, guard)))), function_name);
}
} }
} }
catch (const exception::reserved_word_error &e) { catch (const exception::reserved_word_error &e) {

View File

@ -9,7 +9,7 @@
#include <list> #include <list>
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
#include <boost/thread.hpp> #include <thread>
void do_work(chaiscript::ChaiScript &c) void do_work(chaiscript::ChaiScript &c)
{ {
@ -25,11 +25,11 @@ int main(int argc, char *argv[]) {
//chai.add_shared_object(chaiscript::Boxed_Value(10000), "num_iterations"); //chai.add_shared_object(chaiscript::Boxed_Value(10000), "num_iterations");
std::vector<std::shared_ptr<boost::thread> > threads; std::vector<std::shared_ptr<std::thread> > threads;
for (int i = 0; i < argc - 1; ++i) for (int i = 0; i < argc - 1; ++i)
{ {
threads.push_back(std::shared_ptr<boost::thread>(new boost::thread(std::bind(do_work, std::ref(chai))))); threads.push_back(std::shared_ptr<std::thread>(new std::thread(std::bind(do_work, std::ref(chai)))));
} }
for (int i = 0; i < argc - 1; ++i) for (int i = 0; i < argc - 1; ++i)