Design Pattern Examples
Overview of object-oriented design patterns
Facade_Interface.cpp
Go to the documentation of this file.
1
5
6#include <memory>
8#include "Facade_Interface.h"
10
11namespace // Anonymous
12{
13
21 class DeviceNetworkHighLevel : public DesignPatternExamples_cpp::IDeviceNetworkHighLevel
22 {
23 private:
28
29 public:
37 DeviceNetworkHighLevel(DesignPatternExamples_cpp::IDeviceNetworkLowLevel* system)
38 : _lowlevelSystem(system)
39 {
40 if (system == nullptr)
41 {
42 throw Helpers::argumentnull_error("system", "The system being wrapped cannot be null.");
43 }
44 }
45
46
47 //====================================================================
48 // IDeviceNetworkHighLevel methods
49 //====================================================================
50
52 int NumChains() override
53 {
54 return _lowlevelSystem->GetNumChains();
55 }
56
58 std::vector<uint32_t> GetIdcodes(int chainIndex) override
59 {
60 std::vector<uint32_t> idcodes;
61
62 if (_lowlevelSystem->LockDeviceChain(chainIndex))
63 {
64 idcodes = _lowlevelSystem->GetIdcodes(chainIndex);
65 _lowlevelSystem->UnlockDeviceChain(chainIndex);
66 }
67 return idcodes;
68 }
69
71 void EnableDevicesInDeviceChain(int chainIndex, uint32_t selectMask) override
72 {
73 if (_lowlevelSystem->LockDeviceChain(chainIndex))
74 {
75 _lowlevelSystem->EnableDevicesInDeviceChain(chainIndex, selectMask);
76 _lowlevelSystem->UnlockDeviceChain(chainIndex);
77 }
78 }
79
81 void DisableDevicesInDeviceChain(int chainIndex) override
82 {
83 if (_lowlevelSystem->LockDeviceChain(chainIndex))
84 {
85 _lowlevelSystem->ResetDeviceChain(chainIndex);
86 _lowlevelSystem->UnlockDeviceChain(chainIndex);
87 }
88 }
89 };
90
91 static std::unique_ptr<DeviceNetworkHighLevel> _instance;
92
93} // end anonymous namespace
94
95
96//########################################################################
97//########################################################################
98
99
101{
102
104 // Function: CreateHighLevelInstance
107 {
108 if (!_instance)
109 {
110 IDeviceNetworkLowLevel* lowlevelSystem;
111 lowlevelSystem = CreateLowLevelInstance();
112 _instance = std::make_unique<DeviceNetworkHighLevel>(lowlevelSystem);
113 }
114 return _instance.get();
115 }
116
117} // end namespace
static IDeviceNetworkLowLevel * _lowlevelSystem
Declaration of the IDeviceNetworkLowLevel interface representing the complicated sub-system used in t...
Implementation of the argumentnull_error exception.
Exception for arguments that are null.
Declaration of the IDeviceNetworkHighLevel interface representing the high-level system used in the F...
The namespace containing all Design Pattern Examples implemented in C++.
IDeviceNetworkHighLevel * CreateHighLevelInstance()
Class factory for a singleton instance of the IDeviceNetworkHighLevel interface. Part of the Facade P...
IDeviceNetworkLowLevel * CreateLowLevelInstance()
Class factory for a singleton instance of the sub-system class. Part of the Facade Pattern example.
static std::unique_ptr< Facade_ComplicatedSubSystem > _instance
A singleton instance of the sub-system. Part of the Facade Pattern example.
Represents a high level view of a complex network of device chains. A device chain can be thought of ...
virtual void EnableDevicesInDeviceChain(int chainIndex, uint32_t selectMask)=0
Make visible certain devices in the given device chain. The selectMask value has a bit set for each T...
virtual void DisableDevicesInDeviceChain(int chainIndex)=0
Resets the given device chain so that all devices except the TAP controller is no longer visible.
virtual std::vector< uint32_t > GetIdcodes(int chainIndex)=0
Returns a list of all idcodes from all selected devices in the given device chain.
virtual int NumChains()=0
The number of device chains available from the sub-system.
Represents a network of device chains and the low level access to that network. In general,...
virtual bool LockDeviceChain(int chainIndex)=0
Lock the specified device chain for exclusive access.
virtual void ResetDeviceChain(int chainIndex)=0
Reset the visibility of all devices on the specified device chain.
virtual std::vector< uint32_t > GetIdcodes(int chainIndex)=0
Retrieve a list of idcodes of all visible devices in the given device chain.
virtual void EnableDevicesInDeviceChain(int chainIndex, uint32_t devicesSelectMask)=0
Make visible the specified devices on the specified device chain.
virtual int GetNumChains()=0
Retrieve the number of device chains available in the network.
virtual bool UnlockDeviceChain(int chainIndex)=0
Unlock the specified device chain to release exclusive access.