Design Pattern Examples
Overview of object-oriented design patterns
Facade_Exercise.c
Go to the documentation of this file.
1
6
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "Facade_Interface.h"
13
14#include "Facade_Exercise.h"
15
16//=============================================================================
17//=============================================================================
18
25static void _Facade_ShowIdCodes(int chainIndex, UIntArray* idcodes)
26{
27 if (idcodes != NULL)
28 {
29 printf(" On chain %d, idcodes = [ ", chainIndex);
30 for (size_t index = 0; index < idcodes->length; index++)
31 {
32 printf("0x%04x ", idcodes->data[index]);
33 }
34 printf("]\n");
35 }
36}
37
38//=============================================================================
39//=============================================================================
40
57// ! [Using Facade in C]
59{
60 printf("\nFacade_Exercise\n");
61
63 if (deviceChainFacade != NULL)
64 {
65 int numChains = deviceChainFacade->NumChains();
66 printf(" Showing idcodes of devices after a device reset (expect one device on each chain)...\n");
67
68 for (int chainIndex = 0; chainIndex < numChains; ++chainIndex)
69 {
70 deviceChainFacade->DisableDevicesInDeviceChain(chainIndex);
71 UIntArray idcodes;
72 UIntArray_Initialize(&idcodes);
73 deviceChainFacade->GetIdcodes(chainIndex, &idcodes);
74 _Facade_ShowIdCodes(chainIndex, &idcodes);
75 UIntArray_Clear(&idcodes);
76 }
77
78 printf(" Showing idcodes of devices after selecting all devices...\n");
79 for (int chainIndex = 0; chainIndex < numChains; ++chainIndex)
80 {
81 deviceChainFacade->EnableDevicesInDeviceChain(chainIndex, 0xffffffff);
82 UIntArray idcodes;
83 UIntArray_Initialize(&idcodes);
84 deviceChainFacade->GetIdcodes(chainIndex, &idcodes);
85 _Facade_ShowIdCodes(chainIndex, &idcodes);
86 UIntArray_Clear(&idcodes);
87 }
88 }
89
90 printf(" Done.\n");
91}
92// ! [Using Facade in C]
static void _Facade_ShowIdCodes(int chainIndex, UIntArray *idcodes)
Helper function to present a formatted list of idcodes for a particular device chain....
void Facade_Exercise(void)
Example of using the Facade Pattern.
IDeviceNetworkHighLevel * Facade_GetHighLevelDeviceService(void)
Retrieve a set of function pointers to the high-level device service used in the Facade Pattern examp...
Declaration of the Facade_Exercise() function as used in the Facade Pattern.
Declaration of the IDeviceNetworkHighLevel interface representing the high-level system used in the F...
Represents a high level view of a complex network of device chains. A device chain can be thought of ...
void(* EnableDevicesInDeviceChain)(int chainIndex, uint32_t selectMask)
Make visible certain devices in the given device chain. The selectMask value has a bit set for each d...
int(* NumChains)(void)
The number of device chains available from the sub-system.
void(* GetIdcodes)(int chainIndex, UIntArray *idcodes)
Returns a list of all idcodes from all selected devices in the given device chain.
void(* DisableDevicesInDeviceChain)(int chainIndex)
Resets the given device chain so that all devices except the first are no longer visible.
Represents an array of 32-bit unsigned integers. The data field points to a block of memory allocated...
Definition: uintarray.h:24
uint32_t * data
Pointer to array of 32-bit unsigned integers.
Definition: uintarray.h:25
size_t length
Number of 32-bit unsigned integers actually in the data array.
Definition: uintarray.h:26
void UIntArray_Initialize(UIntArray *array)
Initialize the given UIntArray object.
Definition: uintarray.c:13
void UIntArray_Clear(UIntArray *array)
Clear the given UIntArray object so it can be reused again. Releases the list of integers.
Definition: uintarray.c:26