-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathelectricity.h
More file actions
102 lines (73 loc) · 2.71 KB
/
electricity.h
File metadata and controls
102 lines (73 loc) · 2.71 KB
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
#pragma once
#include <string>
class Object;
struct Pole {
std::string name;
Object* connectedObject;
std::string connectedObjectPole;
Pole(const std::string& name_) : name(name_), connectedObject(nullptr) {}
};
class Object {
std::string name;
protected:
Object(const std::string& name_) : name(name_) {}
Pole* getPole(size_t idx) { return nullptr; }
Pole* getPole(size_t idx) {
return const_cast<Pole*>(const_cast<const Object*>(this)->getPole(idx));
}
public:
virtual ~Object() {}
const std::string& getName() const { return name; }
void getName(const std::string &newName) { name = newName; }
void setName(const std::string &newName) { name = newName; }
Pole* getPole(const std::string& name) { return const_cast<Pole*>(const_cast<const Object*>(this)->getPole(name)); }
virtual const Pole* getPole(const std::string& name) const = 0;
virtual size_t getPoleCount() const = 0;
bool connect(const std::string& poleName, Object& other, const std::string& otherPoleName);
bool disconnect(const std::string& poleName);
};
class Switch : public Object {
public:
Pole a1, a2;
Switch(const std::string& name = "");
virtual size_t getPoleCount() const { return 2; }
virtual const Pole* getPole(const std::string& name) const;
protected:
virtual const Pole* getPole(size_t idx) const;
};
class Light : public Object {
public:
Pole a1, a2;
Light(const std::string& name = "") : Object(name), a1("A1"), a2("A2") {}
virtual size_t getPoleCount() const { return 2; }
virtual const Pole* getPole(const std::string& name) const {
if (name == a1.name) return &a1;
else if (name == a2.name) return &a2;
return nullptr;
}
protected:
virtual const Pole* getPole(size_t idx) const {
if (idx == 0) return &a1;
else if (idx == 1)return &a2;
return nullptr;
}
};
class Generator : public Object {
public:
Pole phase, neural, earth;
Generator(const std::string& name = "") : Object(name), phase("p"), neural("n"), earth("e") {}
virtual size_t getPoleCount() const { return 3; }
virtual const Pole* getPole(const std::string& name) const {
if (name == phase.name) return &phase;
else if (name == neural.name) return &neural;
else if (name == earth.name) return &earth;
return nullptr;
}
protected:
virtual const Pole* getPole(size_t idx) const {
if (idx == 0) return &phase;
else if (idx == 1)return &neural;
else if (idx == 2)return &earth;
return nullptr;
}
};