Explorar el Código

Move Field-related functionality to fields.hpp

Ole hace 10 años
padre
commit
a059446be5
Se han modificado 3 ficheros con 60 adiciones y 38 borrados
  1. 1 0
      lib/luwra.hpp
  2. 59 0
      lib/luwra/fields.hpp
  3. 0 38
      lib/luwra/usertypes.hpp

+ 1 - 0
lib/luwra.hpp

@@ -6,6 +6,7 @@
 #include "luwra/stack.hpp"
 #include "luwra/functions.hpp"
 #include "luwra/methods.hpp"
+#include "luwra/fields.hpp"
 #include "luwra/usertypes.hpp"
 #include "luwra/state.hpp"
 

+ 59 - 0
lib/luwra/fields.hpp

@@ -0,0 +1,59 @@
+/* Luwra
+ * Minimal-overhead Lua wrapper for C++
+ *
+ * Copyright (C) 2015, Ole Krüger <ole@vprsm.de>
+ */
+
+#ifndef LUWRA_FIELDS_H_
+#define LUWRA_FIELDS_H_
+
+#include "common.hpp"
+#include "types.hpp"
+
+LUWRA_NS_BEGIN
+
+namespace internal {
+	/**
+	 * Helper struct for wrapping user type fields
+	 */
+	template <typename T>
+	struct FieldWrapper {
+		static_assert(
+			sizeof(T) == -1,
+			"Parameter to FieldWrapper is not a property signature"
+		);
+	};
+
+	template <typename T, typename R>
+	struct FieldWrapper<R T::*> {
+		template <R T::* field_pointer> static inline
+		int invoke(State* state) {
+			if (lua_gettop(state) > 1) {
+				read<T*>(state, 1)->*field_pointer = read<R>(state, 2);
+				return 0;
+			} else {
+				return push(state, read<T*>(state, 1)->*field_pointer);
+			}
+		}
+	};
+
+	template <typename T, typename R>
+	struct FieldWrapper<const R T::*> {
+		template <const R T::* field_pointer> static inline
+		int invoke(State* state) {
+			return push(state, read<T*>(state, 1)->*field_pointer);
+		}
+	};
+}
+
+LUWRA_NS_END
+
+/**
+ * Generate a `lua_CFunction` get/set wrapper for a property accessor.
+ * \param field Fully qualified property name (Do not supply a pointer)
+ * \return Wrapped function as `lua_CFunction`
+ */
+#define LUWRA_WRAP_FIELD(field) \
+	(&luwra::internal::FieldWrapper<decltype(&field)>::invoke<&field>)
+
+#endif

+ 0 - 38
lib/luwra/usertypes.hpp

@@ -104,38 +104,6 @@ namespace internal {
 				+ std::to_string(uintptr_t(Value<T*>::read(state, 1)))
 		);
 	}
-
-	/**
-	 * Helper struct for wrapping user type fields
-	 */
-	template <typename T>
-	struct FieldWrapper {
-		static_assert(
-			sizeof(T) == -1,
-			"Parameter to FieldWrapper is not a property signature"
-		);
-	};
-
-	template <typename T, typename R>
-	struct FieldWrapper<R T::*> {
-		template <R T::* field_pointer> static inline
-		int invoke(State* state) {
-			if (lua_gettop(state) > 1) {
-				read<T*>(state, 1)->*field_pointer = read<R>(state, 2);
-				return 0;
-			} else {
-				return push(state, read<T*>(state, 1)->*field_pointer);
-			}
-		}
-	};
-
-	template <typename T, typename R>
-	struct FieldWrapper<const R T::*> {
-		template <const R T::* field_pointer> static inline
-		int invoke(State* state) {
-			return push(state, read<T*>(state, 1)->*field_pointer);
-		}
-	};
 }
 
 /**
@@ -203,12 +171,6 @@ struct Value<U*> {
 	}
 };
 
-/**
- * This macro allows you to wrap fields without supplying template parameters.
- */
-#define LUWRA_WRAP_FIELD(field) \
-	(&luwra::internal::FieldWrapper<decltype(&field)>::invoke<&field>)
-
 /**
  * Register the metatable for 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.