Added scoped_set_adder.
David Blume

David Blume commited on 2016-01-10 11:09:51
Showing 5 changed files, with 52 additions and 5 deletions.

... ...
@@ -1,5 +1,5 @@
1 1
 CXX=g++
2
-CPPFLAGS=-c -Wall -I../include
2
+CPPFLAGS=-c -Wall -std=c++11 -I../include
3 3
 LDFLAGS=
4 4
 OBJDIR=obj
5 5
 LIBDIR=../lib
... ...
@@ -17,10 +17,10 @@ $(OBJDIR):
17 17
 $(LIBDIR):
18 18
 	mkdir $@
19 19
 
20
-$(LIB): $(OBJECTS) $(LIBDIR)
20
+$(LIB): $(OBJECTS) | $(LIBDIR)
21 21
 	ar -rcs $@ $<
22 22
 
23
-$(OBJDIR)/%.o: %.cpp $(OBJDIR)
23
+$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
24 24
 	$(CXX) $(CPPFLAGS) $< -o $@
25 25
 
26 26
 clean:
... ...
@@ -1,10 +1,10 @@
1 1
 CXX=g++
2
-CPPFLAGS=-c -Wall -std=c++0x -I../include
2
+CPPFLAGS=-c -Wall -std=c++11 -I../include
3 3
 LIBDIR=../lib
4 4
 LDFLAGS=-L $(LIBDIR) -lmy_lib
5 5
 OBJDIR=obj
6 6
 
7
-SOURCES=main.cpp main_helper.cpp ui/widget.cpp
7
+SOURCES=main.cpp main_helper.cpp ui/widget.cpp scoped_set_adder.cpp
8 8
 # //OBJECTS=$(OBJDIR)$(SOURCES:.cpp=.o)
9 9
 OBJECTS=$(foreach bname, $(basename $(SOURCES)), $(OBJDIR)/$(bname).o)
10 10
 EXECUTABLE=main
... ...
@@ -5,6 +5,7 @@
5 5
 #include <algorithm>
6 6
 #include <memory>
7 7
 #include "main_helper.hpp"
8
+#include "scoped_set_adder.hpp"
8 9
 #include "ui/widget.hpp"
9 10
 #include "../include/my_lib.hpp"
10 11
  
... ...
@@ -0,0 +1,27 @@
1
+#include <iostream>
2
+#include <mutex>
3
+#include <set>
4
+
5
+#include "scoped_set_adder.hpp"
6
+ 
7
+namespace {
8
+    std::mutex g_mutex;
9
+    std::set<scoped_set_adder *> g_items;
10
+}
11
+ 
12
+scoped_set_adder::scoped_set_adder(std::string const& name) : name_(name) {
13
+    std::lock_guard<std::mutex> lock(g_mutex);
14
+    g_items.insert(this);
15
+}
16
+ 
17
+scoped_set_adder::~scoped_set_adder() {
18
+    std::lock_guard<std::mutex> lock(g_mutex);
19
+    g_items.erase(this);
20
+}
21
+ 
22
+bool scoped_set_adder::dump() const {
23
+    std::lock_guard<std::mutex> lock(g_mutex);
24
+    bool ret = (g_items.size() > 0);
25
+    std::cout << "suppressed " << ret << " count " << g_items.size() << std::endl;
26
+    return ret;
27
+}
... ...
@@ -0,0 +1,19 @@
1
+#ifndef scoped_set_adder_h_
2
+#define scoped_set_adder_h_
3
+
4
+#include <string>
5
+ 
6
+class scoped_set_adder {
7
+public:
8
+    scoped_set_adder(const std::string& name);
9
+    ~scoped_set_adder();
10
+    scoped_set_adder(const scoped_set_adder&) = delete;
11
+    scoped_set_adder& operator=(const scoped_set_adder&) = delete;
12
+
13
+	bool dump() const;
14
+ 
15
+private:
16
+    std::string name_;
17
+};
18
+ 
19
+#endif
0 20