File tree 3 files changed +78
-0
lines changed
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ if(Boost_FOUND)
13
13
14
14
add_subdirectory (googletest)
15
15
add_subdirectory (Singleton)
16
+ add_subdirectory (Mapleton)
16
17
add_subdirectory (DependencyInjection)
17
18
add_subdirectory (BuildPattern)
18
19
add_subdirectory (Proto)
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.8)
2
+
3
+ project (Mapleton)
4
+
5
+ include_directories (${Boost_INCLUDE_DIRS} )
6
+
7
+ add_executable (${PROJECT_NAME} Mapleton.cpp)
8
+
9
+ target_link_libraries (${PROJECT_NAME} PUBLIC
10
+ gtest_main
11
+ ${Boost_LIBRARIES}
12
+ )
13
+
14
+ add_test (
15
+ NAME ${PROJECT_NAME}
16
+ COMMAND ${PROJECT_NAME}
17
+ )
18
+ install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ../bin)
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ #include < map>
4
+
5
+ class Mapleton {
6
+ public:
7
+ // Returns a reference to static instance of this class
8
+ // based on provided string identifier
9
+ static Mapleton& instance (const std::string& id) {
10
+ static std::map<std::string, Mapleton> instances;
11
+ auto it = instances.find (id);
12
+
13
+ if (it != instances.end ()) {
14
+ return it->second ;
15
+ }
16
+
17
+ Mapleton instance (id);
18
+ auto insertIt = instances.emplace (id, std::move (instance));
19
+ return insertIt.first ->second ;
20
+ }
21
+
22
+ // The big five. We don't want singleton instances to be
23
+ // copyable nor movable. They should always be accessed with
24
+ // ::instance.
25
+ ~Mapleton () = default ;
26
+ // Mapleton(Mapleton&) = delete;
27
+ // Mapleton(Mapleton&&) = delete;
28
+ // Mapleton(const Mapleton&) = delete;
29
+ // Mapleton(const Mapleton&&) = delete;
30
+ // Mapleton& operator=(const Mapleton&) = delete;
31
+ // Mapleton& operator=(const Mapleton&&) = delete;
32
+
33
+ void say_hello_id () {
34
+ std::cout << " Hello " << id << std::endl;
35
+ std::cout << " Number is " << i << std::endl;
36
+ }
37
+
38
+ void increment () {
39
+ i++;
40
+ }
41
+
42
+ private:
43
+ Mapleton (const std::string& id) : id{id}, i{0 } {};
44
+ std::string id;
45
+ int i;
46
+ };
47
+
48
+ int main ()
49
+ {
50
+ auto & minst = Mapleton::instance (" mapleton!" );
51
+ minst.say_hello_id ();
52
+ minst.increment ();
53
+
54
+ auto & mins2 = Mapleton::instance (" mapleton!" );
55
+ mins2.say_hello_id ();
56
+
57
+ auto & mins3 = Mapleton::instance (" PLOO" );
58
+ mins3.say_hello_id ();
59
+ }
You can’t perform that action at this time.
0 commit comments