ソースを参照

Optimize Pushable further

Ole 9 年 前
コミット
4bea6b806e
共有3 個のファイルを変更した24 個の追加39 個の削除を含む
  1. 1 0
      Makefile
  2. 20 20
      examples/playground.cpp
  3. 3 19
      lib/luwra/types.hpp

+ 1 - 0
Makefile

@@ -67,6 +67,7 @@ examples: $(EXAMPLE_OBJS)
 	@for ex in $(EXAMPLE_OBJS); do echo "> Example '$$ex'"; ./$$ex || exit 1; done
 
 -include $(EXAMPLE_DEPS)
+-include $(PLAYGROUND_DEP)
 
 $(EXAMPLE_DIR)/%.out: $(EXAMPLE_DIR)/%.cpp Makefile
 	$(CXX) $(USECXXFLAGS) $(USELDFLAGS) -MMD -MF$(<:%.cpp=%.d) -MT$@ -o$@ $< $(USELDLIBS)

+ 20 - 20
examples/playground.cpp

@@ -1,31 +1,31 @@
 #include <luwra.hpp>
-
-#include <string>
 #include <iostream>
-#include <functional>
 
 using namespace luwra;
 
-static int foo(int a, int b) {
-	return a + b;
-}
+struct A {
+	A() {
+		std::cout << "A()" << std::endl;
+	}
+
+	A(const A&) {
+		std::cout << "A(const A&)" << std::endl;
+	}
+
+	A(A&&) {
+		std::cout << "A(A&&)" << std::endl;
+	}
+};
 
 int main() {
 	StateWrapper state;
-	push(state, 13);
-	push(state, 37);
-
-	std::function<int(int, int)> bar(&foo);
-	std::function<int(int, int)> baz([](int a, int b) -> int {
-		return a + b;
-	});
-
-	std::cout << apply(state, 1, &foo) << std::endl;
-	std::cout << apply(state, 1, bar) << std::endl;
-	std::cout << apply(state, 1, baz) << std::endl;
-	std::cout << apply(state, 1, [](int a, int b) -> int {
-		return a + b;
-	}) << std::endl;
+
+	// A a;
+
+	const int i = 1338;
+	Pushable m(i);
+
+	push(state, m);
 
 	return 0;
 }

+ 3 - 19
lib/luwra/types.hpp

@@ -426,30 +426,19 @@ namespace internal {
 	struct PushableI {
 		virtual
 		size_t push(State* state) const = 0;
-
-		virtual
-		PushableI* copy() const = 0;
-
-		virtual
-		~PushableI() {}
 	};
 
 	template <typename T>
 	struct PushableT: virtual PushableI {
 		T value;
 
-		inline
-		PushableT(T value): value(value) {}
+		template <typename P> inline
+		PushableT(P&& value): value(std::forward<P>(value)) {}
 
 		virtual
 		size_t push(State* state) const {
 			return luwra::push(state, value);
 		}
-
-		virtual
-		PushableI* copy() const {
-			return new PushableT<T>(value);
-		}
 	};
 }
 
@@ -457,18 +446,13 @@ namespace internal {
  * A value which may be pushed onto the stack.
  */
 struct Pushable {
-	std::unique_ptr<internal::PushableI> interface;
+	std::shared_ptr<internal::PushableI> interface;
 
 	template <typename T> inline
 	Pushable(T&& value):
 		interface(new internal::PushableT<T>(std::forward<T>(value)))
 	{}
 
-	inline
-	Pushable(const Pushable& other):
-		interface(other.interface->copy())
-	{}
-
 	inline
 	bool operator <(const Pushable& other) const {
 		return interface < other.interface;