ソースを参照

Rename several core features

Ole 10 年 前
コミット
863bf68a37
共有9 個のファイルを変更した44 個の追加45 個の削除を含む
  1. 3 3
      examples/functions.cpp
  2. 1 4
      examples/state.cpp
  3. 1 1
      examples/usertypes.cpp
  4. 20 6
      lib/luwra/stack.hpp
  5. 4 16
      lib/luwra/state.hpp
  6. 4 4
      lib/luwra/usertypes.hpp
  7. 4 4
      tests/functions.cpp
  8. 4 4
      tests/stack.cpp
  9. 3 3
      tests/usertypes.cpp

+ 3 - 3
examples/functions.cpp

@@ -24,15 +24,15 @@ int main() {
 
 	// Register 'my_function_1'
 	auto wrapped_1 = LUWRA_WRAP_FUNCTION(my_function_1);
-	luwra::register_global(state, "my_function_1", wrapped_1);
+	luwra::setGlobal(state, "my_function_1", wrapped_1);
 
 	// Register 'my_function_2'
 	auto wrapped_2 = LUWRA_WRAP_FUNCTION(my_function_2);
-	luwra::register_global(state, "my_function_2", wrapped_2);
+	luwra::setGlobal(state, "my_function_2", wrapped_2);
 
 	// Register 'my_function_3'
 	auto wrapped_3 = LUWRA_WRAP_FUNCTION(my_function_3);
-	luwra::register_global(state, "my_function_3", wrapped_3);
+	luwra::setGlobal(state, "my_function_3", wrapped_3);
 
 	// Load Lua code
 	luaL_loadstring(

+ 1 - 4
examples/state.cpp

@@ -13,7 +13,7 @@ int main() {
 	state["foo"] = 1337;
 
 	int r = state.runString(
-		"foo = foo + 1"
+		"print(foo)"
 	);
 
 	if (r != LUA_OK) {
@@ -21,8 +21,5 @@ int main() {
 		return 1;
 	}
 
-	int value = state["foo"];
-	std::cout << value << std::endl;
-
 	return 0;
 }

+ 1 - 1
examples/usertypes.cpp

@@ -50,7 +50,7 @@ int main() {
 	// 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.
-	luwra::register_global(state, "Point", LUWRA_WRAP_CONSTRUCTOR(Point, double, double));
+	luwra::setGlobal(state, "Point", LUWRA_WRAP_CONSTRUCTOR(Point, double, double));
 
 	// Load Lua code
 	luaL_loadstring(

+ 20 - 6
lib/luwra/stack.hpp

@@ -13,6 +13,7 @@
 #include <cassert>
 #include <utility>
 #include <functional>
+#include <string>
 
 LUWRA_NS_BEGIN
 
@@ -136,17 +137,30 @@ bool equal(State* state, int index1, int index2) {
 /**
  * Register a value as a global.
  */
-template <typename T> static inline
-void register_global(State* state, const char* name, T value) {
+template <typename V> static inline
+void setGlobal(State* state, const std::string& name, V value) {
 	assert(1 == push(state, value));
-	lua_setglobal(state, name);
+	lua_setglobal(state, name.c_str());
+}
+
+/**
+ * Retrieve a global value.
+ */
+template <typename V> static inline
+V getGlobal(State* state, const std::string& name) {
+	lua_getglobal(state, name.c_str());
+
+	V instance = read<V>(state, -1);
+	lua_pop(state, 1);
+
+	return instance;
 }
 
 /**
  * Set multiple fields at once. Allows you to provide multiple key-value pairs.
  */
 template <typename... R> static inline
-void set_fields(State* state, int index, R&&... args) {
+void setFields(State* state, int index, R&&... args) {
 	static_assert(sizeof...(R) % 2 == 0, "Field parameters must appear in pairs");
 	internal::EntryPusher<R...>::push(state, index, std::forward<R>(args)...);
 }
@@ -155,9 +169,9 @@ void set_fields(State* state, int index, R&&... args) {
  * Create a new table and set its fields.
  */
 template <typename... R> static inline
-void new_table(State* state, R&&... args) {
+void newTable(State* state, R&&... args) {
 	lua_newtable(state);
-	set_fields(state, lua_gettop(state), std::forward<R>(args)...);
+	setFields(state, lua_gettop(state), std::forward<R>(args)...);
 }
 
 LUWRA_NS_END

+ 4 - 16
lib/luwra/state.hpp

@@ -30,8 +30,7 @@ struct GlobalAccessor {
 	 */
 	template <typename V> inline
 	GlobalAccessor& set(V value) {
-		assert(1 == push(state, value));
-		lua_setglobal(state, key.c_str());
+		setGlobal(state, key, value);
 		return *this;
 	}
 
@@ -48,12 +47,7 @@ struct GlobalAccessor {
 	 */
 	template <typename V> inline
 	V get() {
-		lua_getglobal(state, key.c_str());
-
-		V instance = read<V>(state, -1);
-		lua_pop(state, 1);
-
-		return instance;
+		return getGlobal<V>(state, key);
 	}
 
 	/**
@@ -105,12 +99,7 @@ struct StateWrapper {
 	 */
 	template <typename V> inline
 	V getGlobal(const std::string& key) const {
-		lua_getglobal(state, key.c_str());
-
-		V instance = read<V>(state, -1);
-		lua_pop(state, 1);
-
-		return instance;
+		return getGlobal<V>(state, key);
 	}
 
 	/**
@@ -118,8 +107,7 @@ struct StateWrapper {
 	 */
 	template <typename V> inline
 	void setGlobal(const std::string& key, V value) const {
-		assert(1 == push(state, value));
-		lua_setglobal(state, key.c_str());
+		setGlobal(state, key, value);
 	}
 
 	/**

+ 4 - 4
lib/luwra/usertypes.hpp

@@ -416,7 +416,7 @@ void register_user_type(
 		lua_newtable(state);
 
 		for (auto& method: methods) {
-			set_fields(state, -1, method.first, method.second);
+			setFields(state, -1, method.first, method.second);
 		}
 
 		lua_rawset(state, -3);
@@ -424,17 +424,17 @@ void register_user_type(
 
 	// Register garbage-collection hook
 	if (meta_methods.count("__gc") == 0) {
-		set_fields(state, -1, "__gc", &internal::destruct_user_type<T>);
+		setFields(state, -1, "__gc", &internal::destruct_user_type<T>);
 	}
 
 	// Register string representation function
 	if (meta_methods.count("__tostring") == 0) {
-		set_fields(state, -1, "__tostring", &internal::stringify_user_type<T>);
+		setFields(state, -1, "__tostring", &internal::stringify_user_type<T>);
 	}
 
 	// Insert meta methods
 	for (const auto& metamethod: meta_methods) {
-		set_fields(state, -1, metamethod.first, metamethod.second);
+		setFields(state, -1, metamethod.first, metamethod.second);
 	}
 
 	// Pop metatable off the stack

+ 4 - 4
tests/functions.cpp

@@ -23,7 +23,7 @@ TEST_CASE("wrap_function_noret_noparams") {
 	REQUIRE(cfun != nullptr);
 
 	// Register function
-	luwra::register_global(state, "test_function_noret_noparams", cfun);
+	luwra::setGlobal(state, "test_function_noret_noparams", cfun);
 
 	// Invoke function
 	REQUIRE(luaL_dostring(state, "test_function_noret_noparams()") == 0);
@@ -51,7 +51,7 @@ TEST_CASE("wrap_function_noret") {
 	REQUIRE(cfun != nullptr);
 
 	// Register function
-	luwra::register_global(state, "test_function_noret", cfun);
+	luwra::setGlobal(state, "test_function_noret", cfun);
 
 	// Invoke function
 	REQUIRE(luaL_dostring(state, "test_function_noret(13, 37)") == 0);
@@ -75,7 +75,7 @@ TEST_CASE("wrap_function_noparams") {
 	REQUIRE(cfun != nullptr);
 
 	// Register function
-	luwra::register_global(state, "test_function_noparams", cfun);
+	luwra::setGlobal(state, "test_function_noparams", cfun);
 
 	// Invoke function
 	REQUIRE(luaL_dostring(state, "return test_function_noparams()") == 0);
@@ -99,7 +99,7 @@ TEST_CASE("wrap_function") {
 	REQUIRE(cfun != nullptr);
 
 	// Register function
-	luwra::register_global(state, "test_function", cfun);
+	luwra::setGlobal(state, "test_function", cfun);
 
 	// Invoke function
 	REQUIRE(luaL_dostring(state, "return test_function(37, 13)") == 0);

+ 4 - 4
tests/stack.cpp

@@ -42,10 +42,10 @@ TEST_CASE("stack_interaction") {
 	lua_close(state);
 }
 
-TEST_CASE("stack_register_global") {
+TEST_CASE("stack_setGlobal") {
 	lua_State* state = luaL_newstate();
 
-	luwra::register_global(state, "test", 1338);
+	luwra::setGlobal(state, "test", 1338);
 
 	REQUIRE(luaL_dostring(state, "return test") == LUA_OK);
 	REQUIRE(luwra::read<int>(state, -1) == 1338);
@@ -53,11 +53,11 @@ TEST_CASE("stack_register_global") {
 	lua_close(state);
 }
 
-TEST_CASE("stack_set_fields") {
+TEST_CASE("stack_setFields") {
 	lua_State* state = luaL_newstate();
 
 	lua_newtable(state);
-	luwra::set_fields(
+	luwra::setFields(
 		state, -1,
 		"test", 1338,
 		123, 456

+ 3 - 3
tests/usertypes.cpp

@@ -34,7 +34,7 @@ TEST_CASE("usertypes_ctor") {
 
 	// Registration
 	luwra::register_user_type<A>(state);
-	luwra::register_global(state, "A", LUWRA_WRAP_CONSTRUCTOR(A, int));
+	luwra::setGlobal(state, "A", LUWRA_WRAP_CONSTRUCTOR(A, int));
 
 	// Construction
 	REQUIRE(luaL_dostring(state, "return A(73)") == 0);
@@ -77,7 +77,7 @@ TEST_CASE("usertypes_wrap_fields") {
 
 	// Instantiation
 	B value(1338);
-	luwra::register_global(state, "val", &value);
+	luwra::setGlobal(state, "val", &value);
 
 	// Unqualified get
 	REQUIRE(luaL_dostring(state, "return val:n()") == 0);
@@ -154,7 +154,7 @@ TEST_CASE("usertypes_wrap_methods") {
 
 	// Instantiation
 	C value(1337);
-	luwra::register_global(state, "value", &value);
+	luwra::setGlobal(state, "value", &value);
 
 	// Unqualified method
 	REQUIRE(luaL_dostring(state, "return value:foo1(63)") == 0);