Skip to content

Commit b8cba7d

Browse files
xiao chengxiao cheng
xiao cheng
authored and
xiao cheng
committed
Mapleton
1 parent b0db226 commit b8cba7d

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if(Boost_FOUND)
1313

1414
add_subdirectory(googletest)
1515
add_subdirectory(Singleton)
16+
add_subdirectory(Mapleton)
1617
add_subdirectory(DependencyInjection)
1718
add_subdirectory(BuildPattern)
1819
add_subdirectory(Proto)

Mapleton/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)

Mapleton/Mapleton.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

0 commit comments

Comments
 (0)