Browse Source

examples: Dont use namespace luwra

Ole Krüger 10 years ago
parent
commit
0ac95f9001
4 changed files with 50 additions and 56 deletions
  1. 6 8
      examples/functions.cpp
  2. 7 9
      examples/stack.cpp
  3. 31 31
      examples/types.cpp
  4. 6 8
      examples/usertypes.cpp

+ 6 - 8
examples/functions.cpp

@@ -3,8 +3,6 @@
 
 #include <iostream>
 
-using namespace luwra;
-
 static
 void my_function_1(float num, const char* str) {
 	std::cout << "my_function_1(" << num << ", " << str << ")" << std::endl;
@@ -25,18 +23,18 @@ int main() {
 	luaL_openlibs(state);
 
 	// Register 'my_function_1'
-	auto wrapped_1 = wrap_function<void(float, const char*), my_function_1>;
-	push(state, wrapped_1);
+	auto wrapped_1 = luwra::wrap_function<void(float, const char*), my_function_1>;
+	luwra::push(state, wrapped_1);
 	lua_setglobal(state, "my_function_1");
 
 	// Register 'my_function_2'
-	auto wrapped_2 = wrap_function<std::string(), my_function_2>;
-	push(state, wrapped_2);
+	auto wrapped_2 = luwra::wrap_function<std::string(), my_function_2>;
+	luwra::push(state, wrapped_2);
 	lua_setglobal(state, "my_function_2");
 
 	// Register 'my_function_3'
-	auto wrapped_3 = wrap_function<int(int, int), my_function_3>;
-	push(state, wrapped_3);
+	auto wrapped_3 = luwra::wrap_function<int(int, int), my_function_3>;
+	luwra::push(state, wrapped_3);
 	lua_setglobal(state, "my_function_3");
 
 	// Invoke the attached script

+ 7 - 9
examples/stack.cpp

@@ -3,8 +3,6 @@
 
 #include <iostream>
 
-using namespace luwra;
-
 static
 double sum3(int a, int b, double c) {
 	return (a + b) * c;
@@ -15,18 +13,18 @@ int main() {
 	luaL_openlibs(state);
 
 	// Build stack
-	push(state, 13);
-	push(state, 37);
-	push(state, 42.2);
+	luwra::push(state, 13);
+	luwra::push(state, 37);
+	luwra::push(state, 42.2);
 
 	// 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 = " << 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;
 
 	// ... which is a little cumbersome. Instead we might apply a fitting function to our stack.
 	std::cout << "(a + b) * c = "
-	          << apply(state, sum3) // Equivalent to apply(state, 1, sum3) or apply(state, -3, sum3)
+	          << luwra::apply(state, sum3)
 	          << std::endl;
 
 	lua_close(state);

+ 31 - 31
examples/types.cpp

@@ -3,38 +3,38 @@
 
 #include <iostream>
 
-using namespace luwra;
-
-// You may add custom specializations of luwra::Value in order to retrieve them from the stack.
-template <>
-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);
-
-		if (str.length() < 1) {
-			luaL_argerror(state, n, "Given empty string instead of character");
+namespace luwra {
+	// You may add custom specializations of luwra::Value in order to retrieve them from the stack.
+	template <>
+	struct Value<char> {
+		/**
+		 * Retrieve the value at position `n`.
+		 */
+		static inline
+		char read(lua_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");
+			}
+
+			return str[0];
 		}
 
-		return str[0];
-	}
+		/**
+		 * push the value onto the stack.
+		 */
+		static inline
+		int push(lua_State* state, char val) {
+			if (val == 0)
+				return 0;
 
-	/**
-	 * push the value onto the stack.
-	 */
-	static inline
-	int push(State* state, char val) {
-		if (val == 0)
-			return 0;
+			lua_pushlstring(state, &val, 1);
 
-		lua_pushlstring(state, &val, 1);
-
-		return 1;
-	}
-};
+			return 1;
+		}
+	};
+}
 
 static
 void read_chars(char a, char b) {
@@ -46,11 +46,11 @@ int main() {
 	luaL_openlibs(state);
 
 	// Build stack
-	push(state, 'H');
-	push(state, 'i');
+	luwra::push(state, 'H');
+	luwra::push(state, 'i');
 
 	// apply function to stack values
-	apply(state, read_chars);
+	luwra::apply(state, read_chars);
 
 	lua_close(state);
 	return 0;

+ 6 - 8
examples/usertypes.cpp

@@ -3,8 +3,6 @@
 
 #include <iostream>
 
-using namespace luwra;
-
 struct Point {
 	double x, y;
 
@@ -35,24 +33,24 @@ int main() {
 	// Register our user type.
 	// This function also registers a garbage-collector hook and a string representation function.
 	// Both can be overwritten using the third parameter, which lets you add custom meta methods.
-	register_user_type<Point>(
+	luwra::register_user_type<Point>(
 		state,
 		// Methods which shall be availabe in the Lua user data, need to be declared here
 		{
-			{"scale", wrap_method<Point, void(double), &Point::scale>},
-			{"x",     wrap_property<Point, double, &Point::x>},
-			{"y",     wrap_property<Point, double, &Point::y>}
+			{"scale", luwra::wrap_method<Point, void(double), &Point::scale>},
+			{"x",     luwra::wrap_property<Point, double, &Point::x>},
+			{"y",     luwra::wrap_property<Point, double, &Point::y>}
 		},
 		// Meta methods may be registered aswell
 		{
-			{"__tostring", wrap_method<Point, std::string(), &Point::toString>}
+			{"__tostring", luwra::wrap_method<Point, std::string(), &Point::toString>}
 		}
 	);
 
 	// What's left, is registering a constructor for our type.
 	// We have to specify which parameters our constructor takes, because there might be more than
 	// one constructor to deal with.
-	push(state, wrap_constructor<Point, double, double>);
+	luwra::push(state, luwra::wrap_constructor<Point, double, double>);
 	lua_setglobal(state, "Point");
 
 	// Invoke the attached script