Skip to content

Commit bb5bb15

Browse files
author
sajith
committed
Improved
1 parent b755d97 commit bb5bb15

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ set(CMAKE_CXX_STANDARD 20)
2020
#add_executable(lsp LSP/lsp_better_example.cpp)
2121
#add_executable(lspcovarient LSP/lsp_covariance_example.cpp)
2222
#add_executable(lspqueryset LSP/lsp_query_set.cpp)
23-
add_executable(isp_door ISP/isp_door_example.cpp)
23+
#add_executable(isp_door ISP/isp_door_example.cpp)
24+
add_executable(isp_door_better ISP/isp_door_example_better.cpp)

ISP/isp_door_example_better.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
//
3+
// Created by sajith on 4/18/21.
4+
//
5+
6+
#include <iostream>
7+
#include <optional>
8+
#include <stdexcept>
9+
10+
class TimerClient
11+
{
12+
public:
13+
virtual ~TimerClient() = default;
14+
virtual void timeout() = 0;
15+
};
16+
17+
class Door
18+
{
19+
public:
20+
virtual ~Door() = default;
21+
virtual void open() = 0;
22+
virtual void close() = 0;
23+
};
24+
25+
class Timer
26+
{
27+
TimerClient &client;
28+
29+
public:
30+
Timer(TimerClient &client) : client{client}
31+
{
32+
client.timeout();
33+
}
34+
};
35+
36+
37+
class TimedDoor : public Door, public TimerClient
38+
{
39+
std::optional<Timer> timer;
40+
public:
41+
void open()
42+
{
43+
std::cout << "Open\n";
44+
timer.emplace(Timer(*this));
45+
}
46+
47+
void close()
48+
{
49+
std::cout << "Close\n";
50+
}
51+
52+
void timeout()
53+
{
54+
std::cout << "Timeout\n";
55+
close();
56+
timer = std::nullopt;
57+
}
58+
};
59+
60+
61+
class NormalDoor : public Door
62+
{
63+
public:
64+
void open()
65+
{
66+
std::cout << "Open\n";
67+
}
68+
69+
void close()
70+
{
71+
std::cout << "Close\n";
72+
}
73+
};
74+
75+
int main()
76+
{
77+
TimedDoor door;
78+
door.open();
79+
door.open();
80+
81+
return 0;
82+
}
83+

0 commit comments

Comments
 (0)