Kaynağa Gözat

Implement Table::has

This method allows the user to check if a table contains a field which
does not have the value nil.
Ole 9 yıl önce
ebeveyn
işleme
25f66441f1
1 değiştirilmiş dosya ile 16 ekleme ve 0 silme
  1. 16 0
      lib/luwra/tables.hpp

+ 16 - 0
lib/luwra/tables.hpp

@@ -49,6 +49,22 @@ struct Table {
 		lua_pop(state, 1);
 	}
 
+	template <typename K> inline
+	bool has(K&& key) {
+		State* state = ref.impl->state;
+
+		push(state, ref);
+
+		size_t pushedKeys = push(state, key);
+		if (pushedKeys > 1) lua_pop(state, pushedKeys - 1);
+
+		lua_rawget(state, -2);
+		bool isNil = lua_isnil(state, -1);
+
+		lua_pop(state, 2);
+		return !isNil;
+	}
+
 	template <typename V, typename K> inline
 	void set(K&& key, V&& value) {
 		State* state = ref.impl->state;