Add "is_null" for boxed_values to see if they contain a null shared_ptr value

This commit is contained in:
Jason Turner 2009-11-08 16:30:12 +00:00
parent 3a37ceedb7
commit cbc61d898c
3 changed files with 31 additions and 6 deletions

View File

@ -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->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_undef), "is_undef");
m->add(fun(&Boxed_Value::is_null), "is_null");
basic_constructors<bool>("bool", m); basic_constructors<bool>("bool", m);
oper_assign<std::string>(m); oper_assign<std::string>(m);

View File

@ -49,12 +49,20 @@ namespace chaiscript
return ptr->unique(); return ptr->unique();
} }
template<typename T>
static bool is_null(boost::any *a)
{
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(a);
return ptr->get() == 0;
}
Data(const Type_Info &ti, Data(const Type_Info &ti,
const boost::any &to, const boost::any &to,
bool tr, bool tr,
const boost::function<bool (boost::any*)> &t_unique = boost::function<bool (boost::any*)>()) const boost::function<bool (boost::any*)> &t_unique = boost::function<bool (boost::any*)>(),
const boost::function<bool (boost::any*)> &t_is_null = boost::function<bool (boost::any*)>())
: m_type_info(ti), m_obj(to), : 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_obj = rhs.m_obj;
m_is_ref = rhs.m_is_ref; m_is_ref = rhs.m_is_ref;
m_unique = rhs.m_unique; m_unique = rhs.m_unique;
m_is_null = rhs.m_is_null;
return *this; return *this;
} }
@ -72,6 +81,7 @@ namespace chaiscript
boost::any m_obj; boost::any m_obj;
bool m_is_ref; bool m_is_ref;
boost::function<bool (boost::any*)> m_unique; boost::function<bool (boost::any*)> m_unique;
boost::function<bool (boost::any*)> m_is_null;
}; };
/** /**
@ -111,7 +121,8 @@ namespace chaiscript
detail::Get_Type_Info<T>::get(), detail::Get_Type_Info<T>::get(),
boost::any(obj), boost::any(obj),
false, false,
&Data::unique<T>) &Data::unique<T>,
&Data::is_null<T>)
); );
std::map<std::pair<const void *, bool>, Data>::iterator itr std::map<std::pair<const void *, bool>, Data>::iterator itr
@ -170,7 +181,8 @@ namespace chaiscript
detail::Get_Type_Info<T>::get(), detail::Get_Type_Info<T>::get(),
boost::any(boost::shared_ptr<T>(new T(t))), boost::any(boost::shared_ptr<T>(new T(t))),
false, false,
&Data::unique<T>) &Data::unique<T>,
&Data::is_null<T>)
); );
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(&data->m_obj); boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(&data->m_obj);
@ -217,7 +229,7 @@ namespace chaiscript
} }
} }
std::map<std::pair<const void *, bool>, Data > m_ptrs; std::map<std::pair<const void *, bool>, Data> m_ptrs;
int m_cullcount; int m_cullcount;
}; };
@ -298,6 +310,16 @@ namespace chaiscript
return m_data->m_type_info.is_undef(); 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 boost::any get() const
{ {
return m_data->m_obj; return m_data->m_obj;

View File

@ -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 //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 //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 //Call bound version of do_callbacks
chai("do_callbacks()"); chai("do_callbacks()");
@ -143,6 +143,8 @@ int main(int argc, char *argv[]) {
log("Functor test output", boost::lexical_cast<std::string>(x)); log("Functor test output", boost::lexical_cast<std::string>(x));
chai.add(var(boost::shared_ptr<int>()), "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 //Ability to create our own container types when needed. std::vector and std::map are
//mostly supported currently //mostly supported currently