Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_BigResourceManager.c File Reference

Implementation of the BigResourceList structure and its supporting functions, along with the Big Resource Manager functions, BigResourceManager_Clear(), BigResourceManager_AddResource(), and BigResourceManager_GetResource(), used in the Flyweight Pattern. More...

#include <stdlib.h>
#include "Flyweight_BigResourceManager.h"
Include dependency graph for Flyweight_BigResourceManager.c:

Go to the source code of this file.

Classes

struct  BigResourceList
 Represents an expandable list of pointers to BigResource objects. Use the BigResourceList_Clear() to free up the resources in the list. Use the BigResourceList_AddResource() to add a BigResource object to the list (and also take ownership of the BigResource object). More...
 

Functions

static void BigResourceList_Clear (BigResourceList *resourceList)
 Clear the given BigResourceList object by freeing up all allocated BigResource objects and resetting the list to a "new" state so it can be reused.
 
static int BigResourceList_AddResource (BigResourceList *resourceList, BigResource *resource)
 Add a BigResource object to the given BigResourceList object. The list takes ownership of the BigResource object and is responsible for releasing all memory associated with it.
 
void BigResourceManager_Clear (void)
 Release all resources owned by the Big Resource Manager.
 
int BigResourceManager_AddResource (BigResource *rawResource)
 Add a new big resource and return the ID of the resource. If the resource is successfully added, the Big Resource Manager owns the resource and will free it on exit.
 
BigResourceBigResourceManager_GetResource (int bigResourceId)
 Retrieve the requested big resource.
 

Variables

static BigResourceList _resources = { 0 }
 A list of BigResource objects. Initialized by a call to the BigResourceList_AddResource(). This is used by the BigResourceManager_GetResource() to retrieve a BigResource object.
 

Detailed Description

Implementation of the BigResourceList structure and its supporting functions, along with the Big Resource Manager functions, BigResourceManager_Clear(), BigResourceManager_AddResource(), and BigResourceManager_GetResource(), used in the Flyweight Pattern.

Definition in file Flyweight_BigResourceManager.c.

Function Documentation

◆ BigResourceList_AddResource()

static int BigResourceList_AddResource ( BigResourceList resourceList,
BigResource resource 
)
static

Add a BigResource object to the given BigResourceList object. The list takes ownership of the BigResource object and is responsible for releasing all memory associated with it.

This is called from the BigResourceManager_AddResource() function.

Parameters
resourceListThe BigResourceList object to add to.
resourceThe BigResource object to add to the BigResourceList.
Returns
Returns the index of the image added if successful; otherwise, returns -1 to indicate an error (typically an out of memory problem).

Definition at line 69 of file Flyweight_BigResourceManager.c.

References BigResourceList::resources, and BigResourceList::resources_count.

Referenced by BigResourceManager_AddResource().

◆ BigResourceList_Clear()

static void BigResourceList_Clear ( BigResourceList resourceList)
static

Clear the given BigResourceList object by freeing up all allocated BigResource objects and resetting the list to a "new" state so it can be reused.

This is called from the BigResourceManager_Clear() function.

Parameters
resourceListA pointer to the BigResourceList to be cleared.

Definition at line 44 of file Flyweight_BigResourceManager.c.

References BigResource_Clear(), BigResourceList::resources, and BigResourceList::resources_count.

Referenced by BigResourceManager_Clear().

◆ BigResourceManager_AddResource()

int BigResourceManager_AddResource ( BigResource rawResource)

Add a new big resource and return the ID of the resource. If the resource is successfully added, the Big Resource Manager owns the resource and will free it on exit.

Parameters
rawResourceThe BigResource object to add.
Returns
Returns the handle to the new big resource added to the manager. Returns -1 if something went wrong (typically an out of memory condition).

Definition at line 114 of file Flyweight_BigResourceManager.c.

References _resources, and BigResourceList_AddResource().

Referenced by _Flyweight_GenerateBigResource().

◆ BigResourceManager_Clear()

void BigResourceManager_Clear ( void  )

Release all resources owned by the Big Resource Manager.

Definition at line 105 of file Flyweight_BigResourceManager.c.

References _resources, and BigResourceList_Clear().

Referenced by Flyweight_Exercise().

◆ BigResourceManager_GetResource()

BigResource * BigResourceManager_GetResource ( int  bigResourceId)

Retrieve the requested big resource.

Parameters
bigResourceIdHandle to the big resource to retrieve.
Returns
Returns a pointer to the big resource if found; otherwise, returns NULL.

Definition at line 127 of file Flyweight_BigResourceManager.c.

References _resources, BigResourceList::resources, and BigResourceList::resources_count.

Referenced by BigResource_Render().

Variable Documentation

◆ _resources

BigResourceList _resources = { 0 }
static

A list of BigResource objects. Initialized by a call to the BigResourceList_AddResource(). This is used by the BigResourceManager_GetResource() to retrieve a BigResource object.

Definition at line 32 of file Flyweight_BigResourceManager.c.

Referenced by BigResourceManager_AddResource(), BigResourceManager_Clear(), and BigResourceManager_GetResource().