Design Pattern Examples
Overview of object-oriented design patterns
Facade_ComplicatedSubSystem.cpp
Go to the documentation of this file.
1
6
7#include <memory>
8#include <string>
9
11#include "Facade_Interface.h"
12
13
15{
16
22 {
26 bool Visible;
27
31 std::string Name;
32
36 uint32_t Idcode;
37
43
51 DeviceNode(std::string name, uint32_t idcode, DeviceTypes tapType, bool initiallyVisible)
52 : Visible(initiallyVisible)
53 , Name(name)
54 , Idcode(idcode)
55 , DeviceType(tapType)
56 {
57 }
58 };
59
60
61 //########################################################################
62 //########################################################################
63
64
70 {
71 private:
75 std::vector<DeviceNode> _nodes;
76
77 public:
81 std::string Name;
82
87
88 private:
89
98 void _ShowHideNodes(uint32_t nodeSelectMask, bool makeVisible)
99 {
100 uint32_t bitMask = 0x2; // bit 0 is always the DEVICECONTROLLER and is always selected
101 size_t numNodes = _nodes.size();
102
103 // Start at the device after the DEVICECONTROLLER
104 for (size_t index = 1; index < numNodes; ++index)
105 {
106 if ((bitMask & nodeSelectMask) != 0)
107 {
108 _nodes[index].Visible = makeVisible;
109 }
110 bitMask <<= 1;
111 if (bitMask == 0)
112 {
113 // We don't allow more than 32 devices
114 break;
115 }
116 }
117 }
118
119 public:
124 DeviceChain(std::string name)
125 : Name(name)
126 , IsLocked(false)
127 {
128 }
129
130
140 {
141 if (_nodes.size() > 0 && node.DeviceType == DeviceTypes::DEVICECONTROLLER)
142 {
143 // DEVICECONTROLLER always goes at the start of the list.
144 _nodes.insert(std::begin(_nodes), node);
145 }
146 else
147 {
148 _nodes.push_back(node);
149 }
150 }
151
152
158 {
159 for(DeviceNode node : _nodes)
160 {
161 if (node.DeviceType != DeviceTypes::DEVICECONTROLLER)
162 {
163 node.Visible = false;
164 }
165 }
166 }
167
174 void SelectNodes(uint32_t nodeSelectMask)
175 {
176 _ShowHideNodes(nodeSelectMask, true);
177 }
178
185 void DeselectNodes(uint32_t nodeSelectMask)
186 {
187 _ShowHideNodes(nodeSelectMask, false);
188 }
189
190
198 std::vector<uint32_t> GetIdCodesForVisibleNodes()
199 {
200 std::vector<uint32_t> idcodes;
201
202 for(DeviceNode node : _nodes)
203 {
204 if (node.Visible)
205 {
206 idcodes.push_back(node.Idcode);
207 }
208 }
209
210 return idcodes;
211 }
212 };
213
214
215 //########################################################################
216 //########################################################################
217
218
234 {
235 public:
239 std::vector<DeviceChain> _deviceChains { DeviceChain("CHAIN0"), DeviceChain("CHAIN1") };
240
245 {
246 _deviceChains[0].AddNode(DeviceNode("DDD_DEVCTRL0", 0x10101010, DeviceTypes::DEVICECONTROLLER, true));
247 _deviceChains[0].AddNode(DeviceNode("DDD_CORE0", 0x20202020, DeviceTypes::CORE, false));
248 _deviceChains[0].AddNode(DeviceNode("DDD_GTE0", 0x30303030, DeviceTypes::GTE, false));
249
250 _deviceChains[1].AddNode(DeviceNode("DDD_DEVCTRL1", 0x10101011, DeviceTypes::DEVICECONTROLLER, true));
251 _deviceChains[1].AddNode(DeviceNode("DDD_PCH0", 0x40404040, DeviceTypes::PCH, false));
252 _deviceChains[1].AddNode(DeviceNode("DDD_PMC0", 0x50505050, DeviceTypes::PMC, false));
253 }
254
255 private:
256 //====================================================================
257 // IDeviceNetworkLowLevel methods
258 //====================================================================
264 {
265 return static_cast<int>(_deviceChains.size());
266 }
267
275 bool LockDeviceChain(int chainIndex) override
276 {
277 bool locked = false;
278
279 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
280 {
281 if (!_deviceChains[chainIndex].IsLocked)
282 {
283 _deviceChains[chainIndex].IsLocked = true;
284 locked = true;
285 }
286 }
287
288 return locked;
289 }
290
298 bool UnlockDeviceChain(int chainIndex) override
299 {
300 bool unlocked = false;
301
302 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
303 {
304 if (_deviceChains[chainIndex].IsLocked)
305 {
306 _deviceChains[chainIndex].IsLocked = false;
307 unlocked = true;
308 }
309 }
310
311 return unlocked;
312 }
313
319 void ResetDeviceChain(int chainIndex) override
320 {
321 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
322 {
323 _deviceChains[chainIndex].ResetVisibility();
324 }
325 }
326
336 void EnableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask) override
337 {
338 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
339 {
340 _deviceChains[chainIndex].SelectNodes(deviceselectMask);
341 }
342 }
343
353 void DisableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask) override
354 {
355 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
356 {
357 _deviceChains[chainIndex].DeselectNodes(deviceselectMask);
358 }
359 }
360
368 std::vector<uint32_t> GetIdcodes(int chainIndex) override
369 {
370 std::vector<uint32_t> idcodes;
371
372 if (chainIndex >= 0 && chainIndex < static_cast<int>(_deviceChains.size()))
373 {
374 idcodes = _deviceChains[chainIndex].GetIdCodesForVisibleNodes();
375 }
376 return idcodes;
377 }
378 };
379
380
381 //########################################################################
382 //########################################################################
383
384
389 static std::unique_ptr<Facade_ComplicatedSubSystem> _instance;
390
397 {
398 if (!_instance)
399 {
400 _instance = std::make_unique<Facade_ComplicatedSubSystem>();
401 }
402 return _instance.get();
403 }
404
405} // end namespace
Declaration of the IDeviceNetworkLowLevel interface representing the complicated sub-system used in t...
Represents a device chain, which is a collection of DeviceNode objects. Part of the Facade Pattern ex...
std::vector< DeviceNode > _nodes
The list of TAPNodes on this device chain.
bool IsLocked
Whether this device chain is locked for access.
void DeselectNodes(uint32_t nodeSelectMask)
Make invisible one or more devices in the device chain.
std::vector< uint32_t > GetIdCodesForVisibleNodes()
Retrieve a list of idcodes for all devices that are visible in the device chain.
void SelectNodes(uint32_t nodeSelectMask)
Make visible one or more devices in the device chain.
void ResetVisibility()
Resets the device chain so that all devices that are not CLdevices are no longer visible in the devic...
std::string Name
The Name of this device chain.
void _ShowHideNodes(uint32_t nodeSelectMask, bool makeVisible)
Helper method to show or hide devices on the device chain.
void AddNode(DeviceNode node)
Helper method to add a DeviceNode to the device chain. DeviceNode objects that are of DeviceTypes::DE...
Represents some kind of system that contains multiple device chains. Part of the Facade Pattern examp...
bool UnlockDeviceChain(int chainIndex) override
Unlock the specified device chain to indicate exclusive access is no longer desired.
void EnableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask) override
Select one or more devices in the given device chain so those devices are visible.
void ResetDeviceChain(int chainIndex) override
Reset the visibility of all devices on the given device chain so that all devices except the first ar...
std::vector< uint32_t > GetIdcodes(int chainIndex) override
Retrieve a list of idcodes for each device in the given device chain that is visible.
std::vector< DeviceChain > _deviceChains
The list of device chains. In this case, there are two.
Facade_ComplicatedSubSystem()
(private) Constructor. Sets up the device chains.
bool LockDeviceChain(int chainIndex) override
Lock the specified device chain to indicate exclusive access is desired.
void DisableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask) override
Deselect one or more devices in the given device chain so those devices are no longer visible.
Declaration of the IDeviceNetworkHighLevel interface representing the high-level system used in the F...
The namespace containing all Design Pattern Examples implemented in C++.
IDeviceNetworkLowLevel * CreateLowLevelInstance()
Class factory for a singleton instance of the sub-system class. Part of the Facade Pattern example.
DeviceTypes
Identifies the type of devices that can appear in a device chain. Part of the Facade Pattern example.
@ DEVICECONTROLLER
device controller. This is always visible.
static std::unique_ptr< Facade_ComplicatedSubSystem > _instance
A singleton instance of the sub-system. Part of the Facade Pattern example.
Represents a single device. Part of the Facade Pattern example.
DeviceTypes DeviceType
A value from the DeviceTypes enumeration identifying the type of the device.
DeviceNode(std::string name, uint32_t idcode, DeviceTypes tapType, bool initiallyVisible)
Constructor.
bool Visible
Whether the device is visible in the device chain.
uint32_t Idcode
The idcode for this device.
Represents a network of device chains and the low level access to that network. In general,...