Explorar o código

add ScopeCleanup

ithewei %!s(int64=7) %!d(string=hai) anos
pai
achega
06edbc1835
Modificáronse 2 ficheiros con 21 adicións e 3 borrados
  1. 3 3
      Makefile
  2. 18 0
      hscope.h

+ 3 - 3
Makefile

@@ -3,9 +3,9 @@ CXX = g++
 MKDIR = mkdir -p
 RM = rm -r
 
-CPPFLAGS += -g -O2
+CPPFLAGS +=
 CFLAGS +=
-CXXFLAGS += -std=c++11
+CXXFLAGS += -g -Wall -O3 -std=c++11
 
 INCDIR = include
 SRCDIR = src
@@ -19,7 +19,7 @@ endif
 
 INCFLAGS += -I.
 INCFLAGS += $(addprefix -I, $(INCDIR))
-CPPFLAGS += $(INCFLAGS)
+CXXFLAGS += $(INCFLAGS)
 
 LDFLAGS += $(addprefix -L, $(LIBDIR))
 LDFLAGS += -lpthread

+ 18 - 0
hscope.h

@@ -2,6 +2,24 @@
 #define H_SCOPE_H
 
 #include "hdef.h"
+#include <functional>
+
+class ScopeCleanup{
+public:
+    typedef std::function<void()> FT;
+
+    template<typename Fn, typename... Args>
+    ScopeCleanup(Fn&& fn, Args&&... args) {
+        cleanup_ = std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...);
+    }
+
+    ~ScopeCleanup() {
+        cleanup_();
+    }
+
+private:
+    FT cleanup_;
+};
 
 template<typename T>
 class ScopeFree{