ソースを参照

Update userdata further

Ole Krüger 10 年 前
コミット
d4b0977b77
共有2 個のファイルを変更した13 個の追加45 個の削除を含む
  1. 9 7
      examples/methods.cpp
  2. 4 38
      lib/luwra/userdata.hpp

+ 9 - 7
examples/methods.cpp

@@ -3,10 +3,12 @@
 
 #include <iostream>
 
+using namespace luwra;
+
 struct Point {
-	lua_Number x, y;
+	double x, y;
 
-	Point(lua_Number x, lua_Number y):
+	Point(double x, double y):
 		x(x), y(y)
 	{
 		std::cout << "Construct Point(" << x << ", " << y << ")" << std::endl;
@@ -16,7 +18,7 @@ struct Point {
 		std::cout << "Destruct Point(" << x << ", " << y << ")" << std::endl;
 	}
 
-	void scale(lua_Number f) {
+	void scale(double f) {
 		x *= f;
 		y *= f;
 	}
@@ -33,22 +35,22 @@ int main() {
 	// Register our user type.
 	// This function also registers a garbage-collector hook and a string representation function.
 	// Both can be overwritten using the third parameter, which lets you add custom meta methods.
-	luwra::register_user_type<Point>(
+	RegisterUserType<Point>(
 		state,
 		// Methods which shall be availabe in the Lua user data, need to be declared here
 		{
-			{"scale", luwra::WrapMethod<Point, void(lua_Number), &Point::scale>},
+			{"scale", WrapMethod<Point, void(double), &Point::scale>},
 		},
 		// Meta methods may be registered aswell
 		{
-			{"__tostring", luwra::WrapMethod<Point, std::string(), &Point::toString>}
+			{"__tostring", WrapMethod<Point, std::string(), &Point::toString>}
 		}
 	);
 
 	// What's left, is registering a constructor for our type.
 	// We have to specify which parameters our constructor takes, because there might be more than
 	// one constructor to deal with.
-	auto wrapped_ctor = luwra::WrapConstructor<Point, lua_Number, lua_Number>;
+	auto wrapped_ctor = WrapConstructor<Point, double, double>;
 	lua_pushcfunction(state, wrapped_ctor);
 	lua_setglobal(state, "Point");
 

+ 4 - 38
lib/luwra/userdata.hpp

@@ -50,6 +50,7 @@ namespace internal {
 /**
  * Instances of user types shall always be used as references, because Lua values can not be
  * referenced, hence this allows the compiler to differentiate between them.
+ * The life-time of such instance is determined by Lua.
  */
 template <typename T>
 struct Value<T&> {
@@ -85,13 +86,12 @@ struct Value<T&> {
 };
 
 /**
- * Generate the metatable for the userdata type `T`. This function allows you to register methods
+ * Register the metatable for the user type `T`. This function allows you to register methods
  * which are shared across all instances of this type. A garbage-collector hook is also inserted;
- * it destructs the underlying type when the garbage-collector says it is time to say good-bye
- * needed.
+ * it destructs the underlying type when the garbage-collector says it is time to say good-bye.
  */
 template <typename T> static inline
-void register_user_type(
+void RegisterUserType(
 	State* state,
 	std::initializer_list<std::pair<const char*, CFunction>> methods,
 	std::initializer_list<std::pair<const char*, CFunction>> meta_methods = {}
@@ -135,40 +135,6 @@ void register_user_type(
 	lua_pop(state, -1);
 }
 
-static inline
-void register_user_type_metatable(
-	State* state, const char* name,
-	std::initializer_list<std::pair<const char*, CFunction>> methods,
-	std::initializer_list<std::pair<const char*, CFunction>> meta_methods = {}
-) {
-	// Create meta table
-	luaL_newmetatable(state, name);
-
-	// Prepare for method registration
-	lua_pushstring(state, "__index");
-	lua_newtable(state);
-
-	// Insert methods
-	for (auto& method: methods) {
-		lua_pushstring(state, method.first);
-		lua_pushcfunction(state, method.second);
-		lua_rawset(state, -3);
-	}
-
-	// Commit '__index' field
-	lua_rawset(state, -3);
-
-	// Insert meta methods
-	for (auto& metamethod: meta_methods) {
-		lua_pushstring(state, metamethod.first);
-		lua_pushcfunction(state, metamethod.second);
-		lua_rawset(state, -3);
-	}
-
-	// Pop meta table off the stack
-	lua_pop(state, -1);
-}
-
 /**
  * Constructor function for a type `T`. Variadic arguments must be used to specify which parameters
  * to use during construction.