Design Pattern Examples
Overview of object-oriented design patterns
facade_complicatedsubsystem.py
Go to the documentation of this file.
8
9from .facadesubsystem_interface import DeviceTypes, IDeviceNetworkLowLevel
10
11# <summary>
12# Represents a single device.
13# Part of the @ref facade_pattern "Facade pattern" example.
14# </summary>
16
17 # <summary>
18 # Constructor.
19 # </summary>
20 # @param name">Name to use.
21 # @param idcode">idcode for the device.
22 # @param tapType">Value from the DeviceTypes enumeration.
23 # @param initiallyVisible">true if initially visible otherwise false.
24 def __init__(self, name: str, idcode: int, tapType: DeviceTypes, initiallyVisible: bool) -> None:
25 self.Visible = initiallyVisible
26 self.Name = name
27 self.Idcode = idcode
28 self.DeviceType = tapType
29
30
39
40
41
43
44
45
48
49
58 def _ShowHideNodes(self, nodeSelectMask: int, makeVisible : bool) -> None:
59 bitMask = 0x2 # bit 0 is always the DEVICECONTROLLER and is always selected
60 numNodes = len(self._nodes)
61
62 # Start at the device after the DEVICECONTROLLER
63 for index in range(1, numNodes):
64 if (bitMask & nodeSelectMask) != 0:
65 self._nodes[index].Visible = makeVisible
66 bitMask <<= 1
67 if bitMask == 0:
68 # We don't allow more than 32 devices
69 break
70
71
72
76 def __init__(self, name : str) -> None:
77 self._nodes = []
78 self.Name = name
79 self.IsLocked = False
80
81
83
84
86
87
89
90
91
99 def AddNode(self, node : DeviceNode) -> None:
100 if self._nodes and node.DeviceType == DeviceTypes.DEVICECONTROLLER:
101 # DEVICECONTROLLER always goes at the start of the list.
102 self._nodes.insert(0, node)
103 else:
104 self._nodes.append(node)
105
106
107
108
110 def ResetVisibility(self) -> None:
111 for node in self._nodes:
112 if node.DeviceType != DeviceTypes.DEVICECONTROLLER:
113 node.Visible = False
114
115
116
122 def SelectNodes(self, nodeSelectMask : int) -> None:
123 self._ShowHideNodes(nodeSelectMask, True)
124
125
126
132 def DeselectNodes(self, nodeSelectMask : int) -> None:
133 self._ShowHideNodes(nodeSelectMask, False)
134
135
136
143 def GetIdCodesForVisibleNodes(self) -> list:
144 idcodes = []
145
146 for node in self._nodes:
147 if node.Visible:
148 idcodes.append(node.Idcode)
149
150 return idcodes
151
152
153
155
156
157
169
170
171 def __init__(self) -> None:
172 self._deviceChains = [ DeviceChain("CHAIN0"), DeviceChain("CHAIN1") ]
173 self._deviceChains[0].AddNode(DeviceNode("DDD_DEVCTRL0", 0x10101010, DeviceTypes.DEVICECONTROLLER, True))
174 self._deviceChains[0].AddNode(DeviceNode("DDD_CORE0", 0x20202020, DeviceTypes.CORE, False))
175 self._deviceChains[0].AddNode(DeviceNode("DDD_GTE0", 0x30303030, DeviceTypes.GTE, False))
176
177 self._deviceChains[1].AddNode(DeviceNode("DDD_DEVCTRL1", 0x10101011, DeviceTypes.DEVICECONTROLLER, True))
178 self._deviceChains[1].AddNode(DeviceNode("DDD_PCH0", 0x40404040, DeviceTypes.PCH, False))
179 self._deviceChains[1].AddNode(DeviceNode("DDD_PMC0", 0x50505050, DeviceTypes.PMC, False))
180
181
183
184 #====================================================================
185 # IDeviceNetworkLowLevel methods
186 #====================================================================
187
188
192 def GetNumChains(self) -> int:
193 return len(self._deviceChains)
194
195
196
205 def LockDeviceChain(self, chainIndex : int) -> bool:
206 locked = False
207
208 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
209 if not self._deviceChains[chainIndex].IsLocked:
210 self._deviceChains[chainIndex].IsLocked = True
211 locked = True
212
213 return locked
214
215
216
225 def UnlockDeviceChain(self, chainIndex : int) -> bool:
226 unlocked = False
227
228 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
229 if self._deviceChains[chainIndex].IsLocked:
230 self._deviceChains[chainIndex].IsLocked = False
231 unlocked = True
232
233 return unlocked
234
235
236
241 def ResetDeviceChain(self, chainIndex : int) -> None:
242 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
243 self._deviceChains[chainIndex].ResetVisibility()
244
245
246
256 def EnableDevicesInDeviceChain(self, chainIndex : int, deviceselectMask : int) -> None:
257 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
258 self._deviceChains[chainIndex].SelectNodes(deviceselectMask)
259
260
261
271 def DisableDevicesInDeviceChain(self, chainIndex : int, deviceselectMask : int) -> None:
272 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
273 self._deviceChains[chainIndex].DeselectNodes(deviceselectMask)
274
275
276
284 def GetIdcodes(self, chainIndex : int) -> list:
285 idcodes = []
286
287 if chainIndex >= 0 and chainIndex < len(self._deviceChains):
288 idcodes = self._deviceChains[chainIndex].GetIdCodesForVisibleNodes()
289 return idcodes
290
291
292
294_instance = None # type: Facade_ComplicatedSubSystem
295
296
303def CreateLowLevelInstance() -> IDeviceNetworkLowLevel:
304 global _instance
305 if not _instance:
306 _instance = Facade_ComplicatedSubSystem()
307 return _instance
static int GetNumChains(void)
Retrieve the number of device chains.
Represents a device chain, which is a collection of DeviceNode objects.
None DeselectNodes(self, int nodeSelectMask)
Make invisible one or more devices in the device chain.
None SelectNodes(self, int nodeSelectMask)
Make visible one or more devices in the device chain.
None _ShowHideNodes(self, int nodeSelectMask, bool makeVisible)
Helper method to show or hide devices on the device chain.
list GetIdCodesForVisibleNodes(self)
Retrieve a list of idcodes for all devices that are visible in the device chain.
None AddNode(self, DeviceNode node)
Helper method to add a DeviceNode to the device chain.
None ResetVisibility(self)
Resets the device chain so that all devices that are not CLdevices are no longer visible in the devic...
None __init__(self, str name, int idcode, DeviceTypes tapType, bool initiallyVisible)
DeviceType
A value from the DeviceTypes enumeration identifying the type of the device.
bool UnlockDeviceChain(self, int chainIndex)
Unlock the specified device chain to indicate exclusive access is no longer desired.
list GetIdcodes(self, int chainIndex)
Retrieve a list of idcodes for each device in the given device chain that is visible.
None ResetDeviceChain(self, int chainIndex)
Reset the visibility of all devices on the given device chain so that all devices except the first ar...
None DisableDevicesInDeviceChain(self, int chainIndex, int deviceselectMask)
Deselect one or more devices in the given device chain so those devices are no longer visible.
None EnableDevicesInDeviceChain(self, int chainIndex, int deviceselectMask)
Select one or more devices in the given device chain so those devices are visible.
bool LockDeviceChain(self, int chainIndex)
Lock the specified device chain to indicate exclusive access is desired.
IDeviceNetworkLowLevel CreateLowLevelInstance()
Class factory for a singleton instance of the sub-system class as represented by the IDeviceNetworkLo...