Explorar o código

Apply nameing conventions to Value<T>

Ole Krüger %!s(int64=10) %!d(string=hai) anos
pai
achega
0b5474b8d5
Modificáronse 7 ficheiros con 52 adicións e 44 borrados
  1. 2 2
      README.md
  2. 3 3
      examples/stack.cpp
  3. 5 5
      examples/types.cpp
  4. 2 2
      lib/luwra/functions.hpp
  5. 2 2
      lib/luwra/stack.hpp
  6. 33 25
      lib/luwra/types.hpp
  7. 5 5
      lib/luwra/usertypes.hpp

+ 2 - 2
README.md

@@ -30,13 +30,13 @@ struct Value<T> {
 	 * Read T value at index n.
 	 */
 	static inline
-	T read(State* state, int n);
+	T Read(State* state, int n);
 
 	/**
 	 * Push T value onto the stack and return how many values you have pushed.
 	 */
 	static inline
-	int push(State* state, T value);
+	int Push(State* state, T value);
 };
 ```
 

+ 3 - 3
examples/stack.cpp

@@ -20,9 +20,9 @@ int main() {
 	lua_pushnumber(state, 42);
 
 	// Each value can be retrieved individually.
-	std::cout << "a = " << Value<int>::read(state, 1) << std::endl;
-	std::cout << "b = " << Value<int>::read(state, 2) << std::endl;
-	std::cout << "c = " << Value<double>::read(state, 3) << std::endl;
+	std::cout << "a = " << Value<int>::Read(state, 1) << std::endl;
+	std::cout << "b = " << Value<int>::Read(state, 2) << std::endl;
+	std::cout << "c = " << Value<double>::Read(state, 3) << std::endl;
 
 	// ... which is a little cumbersome. Instead we might apply a fitting function to our stack.
 	std::cout << "(a + b) * c = "

+ 5 - 5
examples/types.cpp

@@ -12,8 +12,8 @@ struct Value<char> {
 	 * Retrieve the value at position `n`.
 	 */
 	static inline
-	char read(State* state, int n) {
-		auto str = Value<std::string>::read(state, n);
+	char Read(State* state, int n) {
+		auto str = Value<std::string>::Read(state, n);
 
 		if (str.length() < 1) {
 			luaL_argerror(state, n, "Given empty string instead of character");
@@ -26,7 +26,7 @@ struct Value<char> {
 	 * Push the value onto the stack.
 	 */
 	static inline
-	int push(State* state, char val) {
+	int Push(State* state, char val) {
 		if (val == 0)
 			return 0;
 
@@ -46,8 +46,8 @@ int main() {
 	luaL_openlibs(state);
 
 	// Build stack
-	Value<char>::push(state, 'H');
-	Value<char>::push(state, 'i');
+	Value<char>::Push(state, 'H');
+	Value<char>::Push(state, 'i');
 
 	// Apply function to stack values
 	apply(state, read_chars);

+ 2 - 2
lib/luwra/functions.hpp

@@ -35,7 +35,7 @@ namespace internal {
 	struct FunctionWrapper<R()> {
 		template <R(*FunctionPointer)()> static
 		int Invoke(State* state) {
-			return Value<R>::push(state, FunctionPointer());
+			return Value<R>::Push(state, FunctionPointer());
 		}
 	};
 
@@ -52,7 +52,7 @@ namespace internal {
 	struct FunctionWrapper<R(A...)> {
 		template <R (*FunctionPointer)(A...)> static
 		int Invoke(State* state) {
-			return Value<R>::push(
+			return Value<R>::Push(
 				state,
 				apply(state, FunctionPointer)
 			);

+ 2 - 2
lib/luwra/stack.hpp

@@ -25,7 +25,7 @@ namespace internal {
 		R Direct(State* state, int n, F hook, A&&... args) {
 			return hook(
 				std::forward<A>(args)...,
-				Value<T>::read(state, n)
+				Value<T>::Read(state, n)
 			);
 		}
 	};
@@ -39,7 +39,7 @@ namespace internal {
 				n + 1,
 				hook,
 				std::forward<A>(args)...,
-				Value<T1>::read(state, n)
+				Value<T1>::Read(state, n)
 			);
 		}
 	};

+ 33 - 25
lib/luwra/types.hpp

@@ -34,15 +34,23 @@ struct Value {
 	 * Retrieve the value at position `n`.
 	 */
 	static
-	T read(State*, int);
+	T Read(State*, int);
 
 	/**
 	 * Push the value onto the stack.
 	 */
 	static
-	int push(State*, T);
+	int Push(State*, T);
 };
 
+/**
+ * Convenient wrapped for `Value<T>::push`.
+ */
+template <typename T> static inline
+int Push(State* state, T value) {
+	return Value<T>::push(state, value);
+}
+
 /**
  * Define a template specialization of `Value` for `type` with a `retrf(State*, int)` which
  * extracts it from the stack and a `pushf(State*, type)` which pushes the value on the stack again.
@@ -52,12 +60,12 @@ struct Value {
 	template <>                                                      \
 	struct Value<type> {                                             \
 		static inline                                                \
-		type read(State* state, int n) {                             \
+		type Read(State* state, int n) {                             \
 			return retrf(state, n);                                  \
 		}                                                            \
                                                                      \
 		static inline                                                \
-		int push(State* state, type value) {                         \
+		int Push(State* state, type value) {                         \
 			pushf(state, value);                                     \
 			return 1;                                                \
 		}                                                            \
@@ -87,12 +95,12 @@ namespace internal {
 	template <>
 	struct NumericTransportValue<Integer> {
 		static inline
-		Integer read(State* state, int index) {
+		Integer Read(State* state, int index) {
 			return luaL_checkinteger(state, index);
 		}
 
 		static inline
-		int push(State* state, Integer value) {
+		int Push(State* state, Integer value) {
 			lua_pushinteger(state, value);
 			return 1;
 		}
@@ -102,12 +110,12 @@ namespace internal {
 	template <>
 	struct NumericTransportValue<Number> {
 		static inline
-		Number read(State* state, int index) {
+		Number Read(State* state, int index) {
 			return luaL_checknumber(state, index);
 		}
 
 		static inline
-		int push(State* state, Number value) {
+		int Push(State* state, Number value) {
 			lua_pushnumber(state, value);
 			return 1;
 		}
@@ -123,20 +131,20 @@ namespace internal {
 			&& std::numeric_limits<I>::min() > std::numeric_limits<B>::min();
 
 		static inline
-		I read(State* state, int index) {
+		I Read(State* state, int index) {
 			return
 				std::max<B>(
 					std::numeric_limits<I>::min(),
 					std::min<B>(
 						std::numeric_limits<I>::max(),
-						NumericTransportValue<B>::read(state, index)
+						NumericTransportValue<B>::Read(state, index)
 					)
 				);
 		}
 
 		static inline
-		int push(State* state, I value) {
-			NumericTransportValue<B>::push(state, static_cast<B>(value));
+		int Push(State* state, I value) {
+			NumericTransportValue<B>::Push(state, static_cast<B>(value));
 			return 1;
 		}
 	};
@@ -146,12 +154,12 @@ namespace internal {
 	template <typename I, typename B>
 	struct NumericTruncatingValueBase {
 		static inline
-		I read(State* state, int index) {
-			return static_cast<I>(NumericTransportValue<B>::read(state, index));
+		I Read(State* state, int index) {
+			return static_cast<I>(NumericTransportValue<B>::Read(state, index));
 		}
 
 		static inline
-		int push(State*, I) {
+		int Push(State*, I) {
 			static_assert(
 				sizeof(I) == -1,
 				"You shold not use 'Value<I>::push' specializations which inherit from NumericTruncatingValueBase"
@@ -218,7 +226,7 @@ struct Arbitrary {
 template <>
 struct Value<Arbitrary> {
 	static inline
-	Arbitrary read(State* state, int index) {
+	Arbitrary Read(State* state, int index) {
 		if (index < 0)
 			index = lua_gettop(state) + (index + 1);
 
@@ -226,7 +234,7 @@ struct Value<Arbitrary> {
 	}
 
 	static inline
-	int push(State* state, const Arbitrary& value) {
+	int Push(State* state, const Arbitrary& value) {
 		lua_pushvalue(value.state, value.index);
 
 		if (value.state != state)
@@ -243,17 +251,17 @@ namespace internal {
 	template <size_t I>
 	struct StackPusher<std::index_sequence<I>> {
 		template <typename T> static inline
-		void push(State* state, const T& package) {
-			Value<typename std::tuple_element<I, T>::type>::push(state, std::get<I>(package));
+		void Push(State* state, const T& package) {
+			Value<typename std::tuple_element<I, T>::type>::Push(state, std::get<I>(package));
 		}
 	};
 
 	template <size_t I, size_t... Is>
 	struct StackPusher<std::index_sequence<I, Is...>> {
 		template <typename T> static inline
-		void push(State* state, const T& package) {
-			Value<typename std::tuple_element<I, T>::type>::push(state, std::get<I>(package));
-			StackPusher<std::index_sequence<Is...>>::push(state, package);
+		void Push(State* state, const T& package) {
+			Value<typename std::tuple_element<I, T>::type>::Push(state, std::get<I>(package));
+			StackPusher<std::index_sequence<Is...>>::Push(state, package);
 		}
 	};
 }
@@ -264,13 +272,13 @@ namespace internal {
 template <typename... A>
 struct Value<std::tuple<A...>> {
 	static inline
-	std::tuple<A...> read(State*, int) {
+	std::tuple<A...> Read(State*, int) {
 		static_assert(sizeof(std::tuple<A...>) == -1, "std::tuples cannot be read from the stack");
 	}
 
 	static inline
-	int push(State* state, const std::tuple<A...>& value) {
-		internal::StackPusher<std::make_index_sequence<sizeof...(A)>>::push(state, value);
+	int Push(State* state, const std::tuple<A...>& value) {
+		internal::StackPusher<std::make_index_sequence<sizeof...(A)>>::Push(state, value);
 		return sizeof...(A);
 	}
 };

+ 5 - 5
lib/luwra/usertypes.hpp

@@ -28,19 +28,19 @@ namespace internal {
 	template <typename T, typename... A>
 	int UserTypeConstructor(State* state) {
 		return apply(state, std::function<int(A...)>([state](A... args) {
-			return Value<T&>::push(state, args...);
+			return Value<T&>::Push(state, args...);
 		}));
 	}
 
 	template <typename T>
 	int UserTypeDestructor(State* state) {
-		Value<T&>::read(state, 1).~T();
+		Value<T&>::Read(state, 1).~T();
 		return 0;
 	}
 
 	template <typename T>
 	int UserTypeToString(State* state) {
-		return Value<std::string>::push(
+		return Value<std::string>::Push(
 			state,
 			internal::UserTypeName<T>
 		);
@@ -74,7 +74,7 @@ namespace internal {
 template <typename T>
 struct Value<T&> {
 	static inline
-	T& read(State* state, int n) {
+	T& Read(State* state, int n) {
 		assert(!internal::UserTypeName<T>.empty());
 
 		return *static_cast<T*>(
@@ -83,7 +83,7 @@ struct Value<T&> {
 	}
 
 	template <typename... A> static inline
-	int push(State* state, A&&... args) {
+	int Push(State* state, A&&... args) {
 		assert(!internal::UserTypeName<T>.empty());
 
 		void* mem = lua_newuserdata(state, sizeof(T));