Design Pattern Examples
Overview of object-oriented design patterns
Command_Command.c
Go to the documentation of this file.
1
5
6#include <errno.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10
11#include "Command_Command.h"
12
14// Command_Create_Two_Parameters()
16Command* Command_Create_Two_Parameters(const char* commandName, Command_TextObject* receiver, two_parameter_operation operation, const char* arg1, const char* arg2)
17{
18 Command* commandObject = calloc(1, sizeof(Command));
19 if (commandObject != NULL)
20 {
21 commandObject->commandName = commandName;
22 commandObject->receiver = receiver;
23 commandObject->operation_two_parameters = operation;
24 commandObject->argument1 = arg1;
25 commandObject->argument2 = arg2;
26 }
27 return commandObject;
28}
29
31// Command_Create_No_Parameters()
34{
35 Command* commandObject = calloc(1, sizeof(Command));
36 if (commandObject != NULL)
37 {
38 commandObject->commandName = commandName;
39 commandObject->receiver = receiver;
40 commandObject->operation_no_parameters = operation;
41 }
42 return commandObject;
43}
44
46// Command_Destroy()
48void Command_Destroy(Command* commandObject)
49{
50 free(commandObject);
51}
52
54// Command_Execute()
56void Command_Execute(Command* commandObject)
57{
58 if (commandObject != NULL && commandObject->receiver != NULL)
59 {
60 if (commandObject->operation_two_parameters != NULL)
61 {
62 commandObject->operation_two_parameters(commandObject->receiver, commandObject->argument1, commandObject->argument2);
63 }
64 else if (commandObject->operation_no_parameters != NULL)
65 {
66 commandObject->operation_no_parameters(commandObject->receiver);
67 }
68 }
69}
70
72// Command_ToString()
74const char* Command_ToString(Command* commandObject)
75{
76 static char commandAsString[64] = { '\0'};
77
78 commandAsString[0] = '\0';
79
80 const char* string = commandAsString;
81
82 if (commandObject != NULL)
83 {
84 if (commandObject->operation_two_parameters != NULL)
85 {
86 int numChars = snprintf(commandAsString, sizeof(commandAsString), "%s \"%s\" with \"%s\"", commandObject->commandName, commandObject->argument1, commandObject->argument2);
87 if (numChars < 0)
88 {
89 int errorCode = errno;
90 printf(" Error(%d)! Failed to format command '%s' as a string: %s\n", errorCode, commandObject->commandName, strerror(errorCode));
91 }
92 }
93 else if (commandObject->operation_no_parameters != NULL)
94 {
95 string = commandObject->commandName;
96 }
97 }
98 if (string[0] == '\0')
99 {
100 string = "<NO COMMAND>";
101 }
102 return string;
103}
Command * Command_Create_No_Parameters(const char *commandName, Command_TextObject *receiver, no_parameter_operation operation)
Create a new Command object with the given parameters, creating a command that uses no additional par...
Command * Command_Create_Two_Parameters(const char *commandName, Command_TextObject *receiver, two_parameter_operation operation, const char *arg1, const char *arg2)
Create a new Command object with the given parameters, creating a command that uses two additional pa...
void Command_Execute(Command *commandObject)
Execute the given command on the Command_TextObject it knows about.
const char * Command_ToString(Command *commandObject)
Convert the given command object to a string representation.
void Command_Destroy(Command *commandObject)
Destroy the given command object, releasing it and any associated resources.
Declaration of the Command structure and associated functions as used in the Command Pattern.
void(* no_parameter_operation)(Command_TextObject *source)
Alias for a function type representing an operation applied to a Command_TextObject that uses no addi...
void(* two_parameter_operation)(Command_TextObject *source, const char *argument1, const char *argument2)
Alias for a function type representing an operation applied to a Command_TextObject using two paramet...
Container for a string. Need to use a structure to keep the starting text and the current text togeth...
Represents an operation that can be applied to a Command_TextObject. Can hold one of two kinds of ope...
two_parameter_operation operation_two_parameters
Two parameter operation to apply to the receiver.
const char * argument1
The first argument to a two parameter operation.
const char * commandName
Easy-to-read command name.
no_parameter_operation operation_no_parameters
No parameter operation to apply to the receiver.
Command_TextObject * receiver
The receiver of the command.
const char * argument2
The second argument to a two parameter operation.