-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosableFacility.h
180 lines (152 loc) · 6.51 KB
/
ClosableFacility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Created by david on 25.11.16.
//
#ifndef IMS_PROJ_CLOSABLEFACILITY_H
#define IMS_PROJ_CLOSABLEFACILITY_H
#include "simlib.h"
class ClosableFacility;
/**
* Class modeling closing condition for ClosableFacility.
* Usage: inherit from this class and specify the abstract method as a closing condition you need
*/
class ClosableFacilityCondition {
/**
* specifies a closing condition for the facility
* @param fac facility, to which this condition belongs
* @return
*/
virtual bool closingCondition(ClosableFacility * fac) = 0;
public:
/**
* Object is used as a functor
* @param fac
* @return
*/
bool operator()(ClosableFacility * fac) {
return this->closingCondition(fac);
}
};
/**
* Special condition that always returns true
*/
class AlwaysTrue : public ClosableFacilityCondition{
virtual bool closingCondition(ClosableFacility * fac){
return true;
}
};
/**
* Special condition that always returns false
*/
class AlwaysFalse : public ClosableFacilityCondition{
virtual bool closingCondition(ClosableFacility * fac){
return false;
}
};
/**
* Special kind of facility that can be closed when closing condition is met or by calling method close(),
* it can be openen again by calling open()
*/
class ClosableFacility : public Facility {
/**
* To make the closing condition use whatever it needs to make a decision
*/
friend class ClosableFacilityCondition;
/**
* specifies current state of the closable facility
*/
bool _isOpen;
/**
* current closing condition, it is possible to change it by calling setClosableCondition
*/
ClosableFacilityCondition *_closingCondition;
public:
/**
* whole bunch of constructors,
* @see Facility for details about other params
* @param isOpen specifies starting state of the facility
* @param closingCondition specifies closing condition
*/
ClosableFacility(bool isOpen):Facility(),_closingCondition(new AlwaysFalse()),_isOpen(isOpen){}
ClosableFacility():Facility(),_closingCondition(new AlwaysFalse()),_isOpen(true){}
ClosableFacility(ClosableFacilityCondition *closingCondition)
: Facility(), _closingCondition(closingCondition), _isOpen(true) {}
ClosableFacility(ClosableFacilityCondition *closingCondition, bool isOpen) : _closingCondition(closingCondition),
_isOpen(isOpen) {}
ClosableFacility(const char *_name, ClosableFacilityCondition *closingCondition) : Facility(_name),
_closingCondition(
closingCondition),
_isOpen(true) {}
ClosableFacility(const char *_name, ClosableFacilityCondition *closingCondition, bool isOpen) : Facility(_name),
_closingCondition(
closingCondition),
_isOpen(isOpen) {}
ClosableFacility(Queue *_queue1, ClosableFacilityCondition *closingCondition) : Facility(_queue1),
_closingCondition(closingCondition),
_isOpen(true) {}
ClosableFacility(Queue *_queue1, ClosableFacilityCondition *closingCondition, bool isOpen) : Facility(_queue1),
_closingCondition(
closingCondition),
_isOpen(isOpen) {}
ClosableFacility(const char *_name, Queue *_queue1, ClosableFacilityCondition *closingCondition) : Facility(_name,
_queue1),
_closingCondition(
closingCondition),
_isOpen(true) {}
ClosableFacility(const char *_name, Queue *_queue1, ClosableFacilityCondition *closingCondition, bool isOpen)
: Facility(_name,
_queue1),
_closingCondition(
closingCondition),
_isOpen(isOpen) {}
/**
* @see Facility::Seize
* @throws FacilityNotOpenException if the facility is closed
*/
virtual void Seize(Entity *e, ServicePriority_t sp);
/**
* @see Facility::Seize
* @throws FacilityNotOpenException if the facility is closed
*/
virtual void Seize(Entity *e);
/**
* @see Facility::Release
* @throws FacilityNotOpenException if the facility is closed
*/
virtual void Release(Entity *e);
/**
* checks the underlying closing condition
* @return true if the condition is met, false otherwise
*/
bool checkClosingCondition();
/**
* force-opens the facility
*/
virtual void open();
/**
* force-closes the facility
*/
virtual void close();
/**
* @return true if the facility is open, false otherwise
*/
bool isOpen() {
return _isOpen;
}
/**
* sets new closing condition
* @warning by calling the method you pass ownership of the condition, it will be deleted in closablefacility's destructor,
* you should not use the condition at all after calling this method
* @param condition new condition
*/
void setClosingCondition(ClosableFacilityCondition * condition){
delete this->_closingCondition;
this->_closingCondition = condition;
}
/**
* desctructor, deletes the condition
*/
~ClosableFacility(){
delete this->_closingCondition;
}
};
#endif //IMS_PROJ_CLOSABLEFACILITY_H