Design Pattern Examples
Overview of object-oriented design patterns
NullObject_MoveCommandList.c
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <memory.h>
11
13
15// MoveCommandList_Initialize()
18{
19 if (commandList != NULL)
20 {
21 commandList->commands = NULL;
22 commandList->commands_count = 0;
23 }
24}
25
27// MoveCommandList_Clear()
30{
31 if (commandList != NULL)
32 {
33 for (size_t index = 0; index < commandList->commands_count; index++)
34 {
35 free(commandList->commands[index]);
36 }
37 free(commandList->commands);
38 MoveCommandList_Initialize(commandList);
39 }
40}
41
43// MoveCommandList_Add()
45bool MoveCommandList_Add(MoveCommandList* commandList, MoveCommand* moveCommand)
46{
47 bool added = false;
48
49 if (commandList != NULL && moveCommand != NULL)
50 {
51 MoveCommand** new_list = NULL;
52 if (commandList->commands == NULL)
53 {
54 new_list = malloc(sizeof(MoveCommand*));
55 }
56 else
57 {
58 size_t new_count = commandList->commands_count + 1;
59 new_list = realloc(commandList->commands, new_count * sizeof(MoveCommand*));
60 }
61 if (new_list != NULL)
62 {
63 commandList->commands = new_list;
64 commandList->commands[commandList->commands_count] = moveCommand;
65 commandList->commands_count++;
66 added = true;
67 }
68 }
69 return added;
70}
void MoveCommandList_Clear(MoveCommandList *commandList)
Clear the given MoveCommandList, freeing up any allocated resources, so the list can be reused.
void MoveCommandList_Initialize(MoveCommandList *commandList)
Initialize the given MoveCommandList object. This should be the first function called for an uninitia...
bool MoveCommandList_Add(MoveCommandList *commandList, MoveCommand *moveCommand)
Add a given MoveCommand object to the given MoveCommandList object. The MoveCommandList takes ownersh...
Declaration of the MoveCommandList structure along with the support functions, MoveCommandList_Initia...
Represents a move command. A move command has a name and the command character that represents the co...
Represents a list of MoveCommand objects.
MoveCommand ** commands
Array of pointers to MoveCommand objects.
size_t commands_count
Number of commands in the commands array.