Add "type_name" function and port the "new" in prelude to use it instead of clone/clear.

This commit is contained in:
Jason Turner
2009-07-24 04:01:28 +00:00
parent 2f591a25a6
commit 0d3c90a245
3 changed files with 9 additions and 2 deletions

View File

@@ -463,7 +463,7 @@ namespace chaiscript
/** /**
* return true if the Boxed_Value matches the registered type by name * return true if the Boxed_Value matches the registered type by name
*/ */
static bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r) bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r)
{ {
try { try {
return e.get_type(user_typename) == r.get_type_info(); return e.get_type(user_typename) == r.get_type_info();
@@ -472,6 +472,11 @@ namespace chaiscript
} }
} }
std::string type_name(const Dispatch_Engine &e, Boxed_Value obj)
{
return e.get_type_name(obj.get_type_info());
}
} }
#endif #endif

View File

@@ -148,6 +148,8 @@ namespace chaiscript
engine.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(engine), _2, _1))), engine.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(engine), _2, _1))),
"is_type"); "is_type");
engine.add(fun(boost::function<std::string (Boxed_Value)>(boost::bind(&chaiscript::type_name, boost::ref(engine), _1))),
"type_name");
engine.add(vector_type<std::vector<Boxed_Value> >("Vector")); engine.add(vector_type<std::vector<Boxed_Value> >("Vector"));
engine.add(string_type<std::string>("string")); engine.add(string_type<std::string>("string"));

View File

@@ -12,7 +12,7 @@
#define CODE_STRING(x, y) #x ", " #y #define CODE_STRING(x, y) #x ", " #y
#define chaiscript_prelude CODE_STRING(\ #define chaiscript_prelude CODE_STRING(\
def new(x) { var retval = clone(x); clear(retval); retval; } \ def new(x) { eval(type_name(x))(); } \
# to_string for Pair()\n\ # to_string for Pair()\n\
def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \
"<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \