Design Pattern Examples
Overview of object-oriented design patterns
Memento_Memento.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <memory.h>
11
12#include "helpers/strdup.h"
13
14#include "Memento_Memento.h"
15
17// Memento_Create()
19Memento* Memento_Create(const char* text, const char* name)
20{
21 Memento* memento = NULL;
22
23 if (text != NULL && name != NULL)
24 {
25 memento = calloc(1, sizeof(Memento));
26 if (memento != NULL)
27 {
28 memento->text = NULL;
29 memento->name = STRDUP(name);
30 if (memento->name != NULL)
31 {
32
33 memento->text = STRDUP(text);
34 if (memento->text == NULL)
35 {
36 printf(" Error! Out of memory preserving text in the Memento!\n");
37 free((char*)memento->name);
38 free(memento);
39 memento = NULL;
40 }
41 }
42 else
43 {
44 printf(" Error! Out of memory duplicating the Memento's name.\n");
45 free(memento);
46 memento = NULL;
47 }
48 }
49 else
50 {
51 printf(" Error! Out of memory creating a Memento.\n");
52 }
53 }
54
55 return memento;
56}
57
59// Memento_Destroy()
62{
63 if (memento != NULL)
64 {
65 free(memento->text);
66 memento->text = NULL;
67 free((char*)memento->name);
68 memento->name = NULL;
69 free(memento);
70 }
71}
void Memento_Destroy(Memento *memento)
Destroy an existing instance of the Memento structure. After this function returns,...
Memento * Memento_Create(const char *text, const char *name)
Create a new instance of the Memento structure, initialized to the given text and name.
Declaration of the Memento structure and support functions, Memento_Create() and Memento_Destroy(),...
Declaration of the STRDUP macro that hides the differences between how strdup() is declared in differ...
#define STRDUP
Define STRDUP to be the operating system-specific version of strdup().
Definition: strdup.h:17
Represents a single memento (snapshot) of the text state before an operation is applied....
char * text
The snapshot to be remembered by the Memento.
const char * name
The operation name that triggered the need for this Memento.