Parcourir la Source

Replace FieldVector with MemberMap (aka std::map<Pushable, Pushable>)

Ole il y a 9 ans
Parent
commit
5ddae2d8b4

+ 1 - 1
examples/tables.cpp

@@ -8,7 +8,7 @@ using namespace luwra;
 int main() {
 	StateWrapper state;
 
-	state["t1"] = FieldVector {};
+	state["t1"] = MemberMap {};
 
 	for (int i = 0; i < 10000; i++) {
 		state["t1"]["value"] = i;

+ 1 - 1
examples/usertypes.cpp

@@ -43,7 +43,7 @@ int main() {
 			LUWRA_MEMBER(Point, scale),
 			LUWRA_MEMBER(Point, x),
 			LUWRA_MEMBER(Point, y),
-			{"magic", luwra::FieldVector {
+			{"magic", luwra::MemberMap {
 				{"number", 1337},
 				{"string", "Hello World"}
 			}}

+ 12 - 19
lib/luwra/auxiliary.hpp

@@ -103,9 +103,9 @@ void setFields(State* state, int index, R&&... args) {
 }
 
 /**
- * A collection of key-value pairs.
+ * Map of members
  */
-using FieldVector = std::vector<std::pair<Pushable, Pushable>>;
+using MemberMap = std::map<Pushable, Pushable>;
 
 /**
  * Apply key-value pairs to a table.
@@ -114,30 +114,23 @@ using FieldVector = std::vector<std::pair<Pushable, Pushable>>;
  * \param fields Table fields
  */
 static inline
-void setFields(State* state, int index, const FieldVector& fields) {
+void setFields(State* state, int index, const MemberMap& fields) {
 	if (index < 0)
 		index = lua_gettop(state) + (index + 1);
 
-	for (const auto& pair: fields) {
-		pair.first.push(state);
-		pair.second.push(state);
+	for (const auto& entry: fields) {
+		size_t pushedKeys = luwra::push(state, entry.first);
+		if (pushedKeys > 1)
+			lua_pop(state, static_cast<int>(pushedKeys - 1));
+
+		size_t pushedValues = luwra::push(state, entry.second);
+		if (pushedValues > 1)
+			lua_pop(state, static_cast<int>(pushedValues - 1));
+
 		lua_rawset(state, index);
 	}
 }
 
-template <>
-struct Value<FieldVector> {
-	/**
-	 * Pushing a FieldVector will create a new table with the given fields.
-	 */
-	static inline
-	size_t push(State* state, const FieldVector& fields) {
-		lua_newtable(state);
-		setFields(state, -1, fields);
-		return 1;
-	}
-};
-
 /**
  * Retrieve a field from a table.
  */

+ 2 - 2
lib/luwra/state.hpp

@@ -65,8 +65,8 @@ struct StateWrapper: Table {
 	template <typename T> inline
 	void registerUserType(
 		const char* ctor_name,
-		const FieldVector& methods = FieldVector(),
-		const FieldVector& meta_methods = FieldVector()
+		const MemberMap& methods = MemberMap(),
+		const MemberMap& meta_methods = MemberMap()
 	) {
 		::luwra::registerUserType<T>(state, ctor_name, methods, meta_methods);
 	}

+ 1 - 1
lib/luwra/tables.hpp

@@ -144,7 +144,7 @@ struct Table {
 	}
 
 	inline
-	void update(const FieldVector& fields) const {
+	void update(const MemberMap& fields) const {
 		State* state = ref.impl->state;
 
 		push(state, ref);

+ 9 - 9
lib/luwra/usertypes.hpp

@@ -202,8 +202,8 @@ struct Value<U*> {
 template <typename U> static inline
 void registerUserType(
 	State* state,
-	const FieldVector& methods = FieldVector(),
-	const FieldVector& meta_methods = FieldVector()
+	const MemberMap& methods = MemberMap(),
+	const MemberMap& meta_methods = MemberMap()
 ) {
 	using Wrapper = internal::UserTypeWrapper<U>;
 
@@ -211,11 +211,11 @@ void registerUserType(
 	luaL_newmetatable(state, Wrapper::Reg::name.c_str());
 
 	// Insert methods
-	setFields(state, -1, {
-		{"__index",    methods},
-		{"__gc",       &Wrapper::destruct},
-		{"__tostring", &Wrapper::stringify}
-	});
+	setFields(state, -1,
+		"__index",    methods,
+		"__gc",       &Wrapper::destruct,
+		"__tostring", &Wrapper::stringify
+	);
 
 	// Insert meta methods
 	setFields(state, -1, meta_methods);
@@ -252,8 +252,8 @@ template <typename S> static inline
 void registerUserType(
 	State* state,
 	const char* ctor_name,
-	const FieldVector& methods = FieldVector(),
-	const FieldVector& meta_methods = FieldVector()
+	const MemberMap& methods = MemberMap(),
+	const MemberMap& meta_methods = MemberMap()
 ) {
 	using T = typename internal::UserTypeSignature<S>::T;
 	registerUserType<T>(state, methods, meta_methods);

+ 0 - 15
tests/auxiliary.cpp

@@ -74,18 +74,3 @@ TEST_CASE("getField") {
 	REQUIRE(state.runString("return {hello = 123}") == LUA_OK);
 	REQUIRE(luwra::getField<int>(state, -1, "hello") == 123);
 }
-
-TEST_CASE("FieldVector") {
-	luwra::StateWrapper state;
-
-	luwra::push<luwra::FieldVector>(state, {
-		{"test", 7331},
-		{7331,   123}
-	});
-
-	lua_setglobal(state, "test");
-
-	REQUIRE(state.runString("return test.test, test[test.test]") == LUA_OK);
-	REQUIRE(luwra::read<int>(state, -2) == 7331);
-	REQUIRE(luwra::read<int>(state, -1) == 123);
-}