Permalink
Browse files

Eradicate global base_class registrations to prevent problems with th…

…reading and general memory management issues with knowing how and when to clean them up.
  • Loading branch information...
lefticus committed Feb 25, 2013
1 parent 1858885 commit 2afc09dad488dc8736a41b52b1824a918da291da
@@ -273,21 +273,6 @@ namespace chaiscript
std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
}
- /**
- * Returns true if a call can be made that consists of the first parameter
- * (the function) with the remaining parameters as its arguments.
- */
- static Boxed_Value call_exists(const std::vector<Boxed_Value> &params)
- {
- if (params.size() < 1)
- {
- throw exception::arity_error(static_cast<int>(params.size()), 1);
- }
-
- Const_Proxy_Function f = boxed_cast<Const_Proxy_Function>(params[0]);
-
- return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
- }
static bool has_guard(const Const_Proxy_Function &t_pf)
{
@@ -383,7 +368,6 @@ namespace chaiscript
m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation");
- m->add(fun(&dispatch::Proxy_Function_Base::operator()), "call");
m->add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
@@ -476,9 +460,6 @@ namespace chaiscript
m->add(fun(&ptr_assign<boost::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
m->add(fun(&ptr_assign<boost::add_const<dispatch::Proxy_Function_Base>::type>), "=");
- m->add(Proxy_Function(new dispatch::Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
- "call_exists");
-
m->add(fun(&type_match), "type_match");
return m;
@@ -68,10 +68,10 @@ namespace chaiscript
/// assert(i == 5);
/// \endcode
template<typename Type>
- typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv)
+ typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv, const Dynamic_Cast_Conversions &t_conversions = Dynamic_Cast_Conversions())
{
try {
- return detail::Cast_Helper<Type>::cast(bv);
+ return detail::Cast_Helper<Type>::cast(bv, t_conversions);
} catch (const boost::bad_any_cast &) {
#ifdef BOOST_MSVC
@@ -86,7 +86,7 @@ namespace chaiscript
try {
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
// either way, we are not responsible if it doesn't work
- return detail::Cast_Helper<Type>::cast(detail::boxed_dynamic_cast<Type>(bv));
+ return detail::Cast_Helper<Type>::cast(t_conversions.boxed_dynamic_cast<Type>(bv), t_conversions);
} catch (const boost::bad_any_cast &) {
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
}
@@ -17,6 +17,8 @@
namespace chaiscript
{
+ class Dynamic_Cast_Conversions;
+
namespace detail
{
// Cast_Helper_Inner helper classes
@@ -29,7 +31,7 @@ namespace chaiscript
{
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
if (ob.is_ref())
{
@@ -71,7 +73,7 @@ namespace chaiscript
{
typedef const Result * Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
if (ob.is_ref())
{
@@ -100,7 +102,7 @@ namespace chaiscript
{
typedef Result * Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
if (ob.is_ref())
{
@@ -119,7 +121,7 @@ namespace chaiscript
{
typedef typename boost::reference_wrapper<Result> Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
if (ob.is_ref())
{
@@ -138,7 +140,7 @@ namespace chaiscript
{
typedef typename boost::shared_ptr<Result> Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
}
@@ -152,7 +154,7 @@ namespace chaiscript
{
typedef typename boost::shared_ptr<const Result> Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
if (!ob.get_type_info().is_const())
{
@@ -200,7 +202,7 @@ namespace chaiscript
{
typedef const Boxed_Value & Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
return ob;
}
@@ -261,9 +263,9 @@ namespace chaiscript
{
typedef typename Cast_Helper_Inner<T>::Result_Type Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &t_conversions)
{
- return Cast_Helper_Inner<T>::cast(ob);
+ return Cast_Helper_Inner<T>::cast(ob, t_conversions);
}
};
}
@@ -820,7 +820,7 @@ namespace chaiscript
{
typedef Boxed_Number Result_Type;
- static Result_Type cast(const Boxed_Value &ob)
+ static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions &)
{
return Boxed_Number(ob);
}
@@ -222,9 +222,9 @@ namespace chaiscript
public:
Dispatch_Function(const std::vector<Proxy_Function> &t_funcs)
: Proxy_Function_Base(build_type_infos(t_funcs)),
- m_funcs(t_funcs)
- {
- }
+ m_funcs(t_funcs)
+ {
+ }
virtual bool operator==(const dispatch::Proxy_Function_Base &rhs) const
{
@@ -274,7 +274,7 @@ namespace chaiscript
return -1; // unknown arity
}
- virtual bool call_match(const std::vector<Boxed_Value> &vals) const
+ virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{
typedef std::vector<Proxy_Function> function_vec;
@@ -283,7 +283,7 @@ namespace chaiscript
while (begin != end)
{
- if ((*begin)->call_match(vals))
+ if ((*begin)->call_match(vals, t_conversions))
{
return true;
} else {
@@ -300,9 +300,9 @@ namespace chaiscript
}
protected:
- virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const
+ virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{
- return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params);
+ return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params, t_conversions);
}
private:
@@ -389,16 +389,21 @@ namespace chaiscript
~Dispatch_Engine()
{
- detail::Dynamic_Conversions::get().cleanup(m_conversions.begin(), m_conversions.end());
}
+ /// \brief casts an object while applying any Dynamic_Conversion available
+ template<typename Type>
+ typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
+ {
+ return chaiscript::boxed_cast<Type>(bv, m_conversions);
+ }
+
/**
* Add a new conversion for upcasting to a base class
*/
void add(const Dynamic_Cast_Conversion &d)
{
- m_conversions.push_back(d);
- return detail::Dynamic_Conversions::get().add_conversion(d);
+ m_conversions.add_conversion(d);
}
/**
@@ -771,11 +776,16 @@ namespace chaiscript
m_state.m_reserved_words.insert(name);
}
+ const Dynamic_Cast_Conversions &conversions() const
+ {
+ return m_conversions;
+ }
+
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> &params) const
{
std::vector<Proxy_Function> functions = get_function(t_name);
- return dispatch::dispatch(functions.begin(), functions.end(), params);
+ return dispatch::dispatch(functions.begin(), functions.end(), params, m_conversions);
}
Boxed_Value call_function(const std::string &t_name) const
@@ -844,6 +854,22 @@ namespace chaiscript
std::cout << ") " << std::endl;
}
+ /**
+ * Returns true if a call can be made that consists of the first parameter
+ * (the function) with the remaining parameters as its arguments.
+ */
+ Boxed_Value call_exists(const std::vector<Boxed_Value> &params)
+ {
+ if (params.size() < 1)
+ {
+ throw exception::arity_error(static_cast<int>(params.size()), 1);
+ }
+
+ Const_Proxy_Function f = this->boxed_cast<Const_Proxy_Function>(params[0]);
+
+ return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end()), m_conversions));
+ }
+
/**
* Dump all system info to stdout
*/
@@ -1146,7 +1172,7 @@ namespace chaiscript
int call_depth;
};
- std::vector<Dynamic_Cast_Conversion> m_conversions;
+ Dynamic_Cast_Conversions m_conversions;
chaiscript::detail::threading::Thread_Storage<Stack_Holder> m_stack_holder;
Oops, something went wrong.

0 comments on commit 2afc09d

Please sign in to comment.