script_(const_cast<class script*>(&s)) {}
// check the type of the value
- bool is_boolean() const
- {
- return (bool)lua_isboolean(script_->state_, index);
- }
- bool is_imported_function() const
- {
- return (bool)lua_iscfunction(script_->state_, index);
- }
- bool is_function() const
- {
- return (bool)lua_isfunction(script_->state_, index);
- }
- bool is_nil() const
- {
- return (bool)lua_isnil(script_->state_, index);
- }
- bool is_none() const
- {
- return (bool)lua_isnone(script_->state_, index);
- }
- bool is_none_or_nil() const
- {
- return (bool)lua_isnoneornil(script_->state_, index);
- }
- bool is_number() const
- {
- return (bool)lua_isnumber(script_->state_, index);
- }
- bool is_string() const
- {
- return (bool)lua_isstring(script_->state_, index);
- }
- bool is_table() const
- {
- return (bool)lua_istable(script_->state_, index);
- }
- bool is_thread() const
- {
- return (bool)lua_isthread(script_->state_, index);
- }
- bool is_data() const
- {
- return (bool)lua_isuserdata(script_->state_, index);
- }
- bool is_light_data() const
- {
- return (bool)lua_islightuserdata(script_->state_, index);
- }
+#define IS_TYPE(T, N) \
+ bool is_##N() const \
+ { \
+ return (bool)lua_is##T(script_->state_, index); \
+ }//
+ IS_TYPE(boolean, boolean);
+ IS_TYPE(cfunction, imported_function);
+ IS_TYPE(function, function);
+ IS_TYPE(lightuserdata, light_data);
+ IS_TYPE(nil, nil);
+ IS_TYPE(none, none);
+ IS_TYPE(noneornil, none_or_nil);
+ IS_TYPE(number, number);
+ IS_TYPE(string, string);
+ IS_TYPE(table, table);
+ IS_TYPE(thread, thread);
+ IS_TYPE(userdata, data);
+#undef IS_TYPE
/**
* Check the value and throw an error if its the wrong type.
return *this;
}
- const slot&
- require_boolean(const std::string& what = "boolean") const
- {
- if (!is_boolean()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_number(const std::string& what = "number") const
- {
- if (!is_number()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_string(const std::string& what = "string") const
- {
- if (!is_string()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_table(const std::string& what = "table") const
- {
- if (!is_table()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_function(const std::string& what = "function") const
- {
- if (!is_function()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_data(const std::string& what = "userdata") const
- {
- if (!is_data()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_nil(const std::string& what = "nil") const
- {
- if (!is_nil()) raise_type_error(what);
- return *this;
- }
- const slot&
- require_thread(const std::string& what = "thread") const
- {
- if (!is_thread()) raise_type_error(what);
- return *this;
- }
+#define REQUIRE_TYPE(T) \
+ const slot& require_##T(const std::string& what = #T) const \
+ { \
+ if (!is_##T()) raise_type_error(what); \
+ return *this; \
+ }//
+ REQUIRE_TYPE(boolean);
+ REQUIRE_TYPE(number);
+ REQUIRE_TYPE(string);
+ REQUIRE_TYPE(table);
+ REQUIRE_TYPE(function);
+ REQUIRE_TYPE(data);
+ REQUIRE_TYPE(nil);
+ REQUIRE_TYPE(thread);
+#undef REQUIRE_TYPE
template <class T>
const slot&
/**
* Convert the underlying value to a C++ type.
*/
- template <class T>
- bool get(T& value) const
- {
- if (is_number())
- {
- value = (T)lua_tointeger(script_->state_, index);
- return true;
- }
- return false;
- }
+
+#define GET_INT_OF_TYPE(T) \
+ bool get(T& value) const \
+ { \
+ if (is_number()) \
+ { \
+ value = static_cast<T>(lua_tointeger(script_->state_, index)); \
+ return true; \
+ } \
+ return false; \
+ }//
+ GET_INT_OF_TYPE(char);
+ GET_INT_OF_TYPE(unsigned char);
+ GET_INT_OF_TYPE(short);
+ GET_INT_OF_TYPE(unsigned short);
+ GET_INT_OF_TYPE(int);
+ GET_INT_OF_TYPE(unsigned int);
+ GET_INT_OF_TYPE(long);
+ GET_INT_OF_TYPE(unsigned long);
+ GET_INT_OF_TYPE(long long);
+ GET_INT_OF_TYPE(unsigned long long);
+#undef GET_INT_OF_TYPE
bool get(float& value) const
{
}
return false;
}
-
bool get(std::string& value) const
{
const char* str;
}
return false;
}
+ template <class T>
+ bool get(T& value) const
+ {
+ if (is_data())
+ {
+ value = *reinterpret_cast<T*>(lua_touserdata(script_->state_, index));
+ return true;
+ }
+ return false;
+ }
void* get_data() const
{
return lua_touserdata(script_->state_, index);
*/
void pop()
{
- if
- (index < 0) script_->pop(-index);
- else
- script_->pop(script_->stack_size() -
- index + 1);
+ if (index < 0) script_->pop(-index);
+ else script_->pop(script_->stack_size() - index + 1);
}
/**
std::ostringstream stream;
stream << "bad argument " << index << " to '" << func
- << "' (" << message << ")";
+ << "' (" << message << ")";
throw std::invalid_argument(stream.str());
return 0;
mutable class script* script_;
};
+
script() :
state_(0)
{
pop();
}
- void import_base_library()
- {
- push(luaopen_base);
- push(LUA_COLIBNAME);
- call(1, 0);
- }
- void import_package_library()
- {
- push(luaopen_package);
- push(LUA_LOADLIBNAME);
- call(1, 0);
- }
- void import_string_library()
- {
- push(luaopen_string);
- push(LUA_STRLIBNAME);
- call(1, 0);
- }
- void import_table_library()
- {
- push(luaopen_table);
- push(LUA_TABLIBNAME);
- call(1, 0);
- }
- void import_math_library()
- {
- push(luaopen_math);
- push(LUA_MATHLIBNAME);
- call(1, 0);
- }
- void import_io_library()
- {
- push(luaopen_io);
- push(LUA_IOLIBNAME);
- call(1, 0);
- }
- void import_os_library()
- {
- push(luaopen_os);
- push(LUA_OSLIBNAME);
- call(1, 0);
- }
- void import_debug_library()
- {
- push(luaopen_debug);
- push(LUA_DBLIBNAME);
- call(1, 0);
- }
+#define IMPORT_LIBRARY(L, K) \
+ void import_##L##_library() \
+ { \
+ push(luaopen_##L); \
+ push(LUA_##K##LIBNAME); \
+ call(1, 0); \
+ }//
+ IMPORT_LIBRARY(base, CO);
+ IMPORT_LIBRARY(debug, DB);
+ IMPORT_LIBRARY(io, IO);
+ IMPORT_LIBRARY(math, MATH);
+ IMPORT_LIBRARY(os, OS);
+ IMPORT_LIBRARY(package, LOAD);
+ IMPORT_LIBRARY(string, STR);
+ IMPORT_LIBRARY(table, TAB);
+#undef IMPORT_LIBRARY
void
import_function(const std::string& name, const function& function)
* Push some values onto the stack.
*/
- slot push(char value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(unsigned char value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(short value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(unsigned short value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(int value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(unsigned int value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(long value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(unsigned long value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(long long value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
- slot push(unsigned long long value)
- {
- lua_pushinteger(state_, lua_Integer(value));
- return top();
- }
+#define PUSH_INT_OF_TYPE(T) \
+ slot push(T value) \
+ { \
+ lua_pushinteger(state_, static_cast<lua_Integer>(value)); \
+ return top(); \
+ }//
+ PUSH_INT_OF_TYPE(char);
+ PUSH_INT_OF_TYPE(unsigned char);
+ PUSH_INT_OF_TYPE(short);
+ PUSH_INT_OF_TYPE(unsigned short);
+ PUSH_INT_OF_TYPE(int);
+ PUSH_INT_OF_TYPE(unsigned int);
+ PUSH_INT_OF_TYPE(long);
+ PUSH_INT_OF_TYPE(unsigned long);
+ PUSH_INT_OF_TYPE(long long);
+ PUSH_INT_OF_TYPE(unsigned long long);
+#undef PUSH_INT_OF_TYPE
slot push(bool value)
{