Selaa lähdekoodia

Add template shortcut 'read<T>' for 'Value<T>::read'

Ole 10 vuotta sitten
vanhempi
commit
729dc398da
2 muutettua tiedostoa jossa 11 lisäystä ja 3 poistoa
  1. 3 3
      examples/stack.cpp
  2. 8 0
      lib/luwra/types.hpp

+ 3 - 3
examples/stack.cpp

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

+ 8 - 0
lib/luwra/types.hpp

@@ -42,6 +42,14 @@ int push(State* state, T value) {
 	return Value<T>::push(state, value);
 }
 
+/**
+ * Convenient wrapper for `Value<T>::read`.
+ */
+template <typename T> static inline
+T read(State* state, int index) {
+	return Value<T>::read(state, index);
+}
+
 /**
  * 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.