From cbc61d898ca93766b710eed0ed59f68db5cf3cc4 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 8 Nov 2009 16:30:12 +0000 Subject: [PATCH] Add "is_null" for boxed_values to see if they contain a null shared_ptr value --- include/chaiscript/dispatchkit/bootstrap.hpp | 1 + .../chaiscript/dispatchkit/boxed_value.hpp | 32 ++++++++++++++++--- src/example.cpp | 4 ++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 5cf0f48..9205a83 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -709,6 +709,7 @@ namespace chaiscript m->eval("def Dynamic_Object::clone() { var new_o := Dynamic_Object(this.get_type_name()); for_each(this.get_attrs(), bind(fun(new_o, x) { new_o.get_attr(x.first) = x.second; }, new_o, _) ); return new_o; }"); m->add(fun(&Boxed_Value::is_undef), "is_undef"); + m->add(fun(&Boxed_Value::is_null), "is_null"); basic_constructors("bool", m); oper_assign(m); diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index e4b5c3b..beeb06d 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -49,12 +49,20 @@ namespace chaiscript return ptr->unique(); } + template + static bool is_null(boost::any *a) + { + boost::shared_ptr *ptr = boost::any_cast >(a); + return ptr->get() == 0; + } + Data(const Type_Info &ti, const boost::any &to, bool tr, - const boost::function &t_unique = boost::function()) + const boost::function &t_unique = boost::function(), + const boost::function &t_is_null = boost::function()) : m_type_info(ti), m_obj(to), - m_is_ref(tr), m_unique(t_unique) + m_is_ref(tr), m_unique(t_unique), m_is_null(t_is_null) { } @@ -64,6 +72,7 @@ namespace chaiscript m_obj = rhs.m_obj; m_is_ref = rhs.m_is_ref; m_unique = rhs.m_unique; + m_is_null = rhs.m_is_null; return *this; } @@ -72,6 +81,7 @@ namespace chaiscript boost::any m_obj; bool m_is_ref; boost::function m_unique; + boost::function m_is_null; }; /** @@ -111,7 +121,8 @@ namespace chaiscript detail::Get_Type_Info::get(), boost::any(obj), false, - &Data::unique) + &Data::unique, + &Data::is_null) ); std::map, Data>::iterator itr @@ -170,7 +181,8 @@ namespace chaiscript detail::Get_Type_Info::get(), boost::any(boost::shared_ptr(new T(t))), false, - &Data::unique) + &Data::unique, + &Data::is_null) ); boost::shared_ptr *ptr = boost::any_cast >(&data->m_obj); @@ -217,7 +229,7 @@ namespace chaiscript } } - std::map, Data > m_ptrs; + std::map, Data> m_ptrs; int m_cullcount; }; @@ -298,6 +310,16 @@ namespace chaiscript return m_data->m_type_info.is_undef(); } + bool is_null() const + { + if (m_data->m_is_null) + { + return m_data->m_is_null(&m_data->m_obj); + } else { + return false; + } + } + boost::any get() const { return m_data->m_obj; diff --git a/src/example.cpp b/src/example.cpp index 237673f..5670283 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -110,7 +110,7 @@ int main(int argc, char *argv[]) { //Finally, it is possible to register any boost::function as a system function, in this //way, we can, for instance add a bound member function to the system - chai.add(fun(&System::do_callbacks, boost::ref(system), "Bound Test"), "do_callbacks"); + chai.add(fun(&System::do_callbacks, boost::ref(system), std::string("Bound Test")), "do_callbacks"); //Call bound version of do_callbacks chai("do_callbacks()"); @@ -143,6 +143,8 @@ int main(int argc, char *argv[]) { log("Functor test output", boost::lexical_cast(x)); + chai.add(var(boost::shared_ptr()), "nullvar"); + chai("print(\"This should be true.\"); print(nullvar.is_null())"); //Ability to create our own container types when needed. std::vector and std::map are //mostly supported currently