design_pattern_for_c  V 1.00
flyweight.c File Reference

Implement of Flyweight design petten library API, defined in flyweight.h. More...

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "flyweight.h"
#include "dp_util.h"
Include dependency graph for flyweight.c:

Go to the source code of this file.

Data Structures

struct  flyweight_instance_t
 instance data definition, instance storaged by list More...
 
struct  flyweight_factory_t
 FlyweightFactory member definition, defined in flyweight.c. More...
 

Macros

Error definition
#define FLYWEIGHT_FAILED   (-1) /*! error */
 
#define FLYWEIGHT_SUCCESS   (0) /*! success */
 

Typedefs

typedef struct flyweight_instance_tFlyweightInstance
 

Functions

static FlyweightInstance flyweight_instance_new (size_t size)
 
static void flyweight_instance_free (FlyweightInstance instance)
 
FlyweightFactory flyweight_factory_new (size_t class_size, int is_threadsafe, FlyweightMethodsIF methods)
 define class for flyweight More...
 
void * flyweight_get (FlyweightFactory this, void *constructor_parameter)
 getter More...
 
int flyweight_set (FlyweightFactory this, void *constructor_parameter, void *data, int(*setter)(void *this, size_t size, void *input_parameter))
 setter More...
 
void flyweight_factory_free (FlyweightFactory this)
 clear class handle More...
 
public API with FlyweightInstance
static FlyweightInstance flyweight_factory_get_storaged_instance (FlyweightFactory class_factory, void *constructor_parameter)
 Check has instance. More...
 
static FlyweightInstance flyweight_factory_instance_new (FlyweightFactory class_factory, void *constructor_parameter)
 allocate. More...
 
static void flyweight_factory_push_instance (FlyweightInstance instance, FlyweightFactory class_factory)
 push instance into list. More...
 
static FlyweightInstance flyweight_factory_pop_instance (FlyweightFactory class_factory)
 pop instance from list. More...
 
static FlyweightInstance flyweight_factory_get (FlyweightFactory class_factory, void *constructor_parameter)
 Getter. More...
 

private API for FlyweightFactory

#define FLYWEIGHT_CLASS_LOCK(instance)   DPUTIL_LOCK(instance->lock)
 
#define FLYWEIGHT_CLASS_UNLOCK   DPUTIL_UNLOCK
 
static void flyweight_class_default_constructor (void *this, size_t size, void *input_parameter)
 Default constructor. More...
 
static int flyweight_class_default_equall_operand (void *this, size_t size, void *input_parameter)
 Default equall operand. More...
 
static int flyweight_class_default_setter (void *this, size_t size, void *input_parameter)
 Default setter. More...
 
static void flyweight_class_set_methods (FlyweightMethodsIF methods, FlyweightFactory instance)
 Set methods. More...
 

Detailed Description

Implement of Flyweight design petten library API, defined in flyweight.h.

Definition in file flyweight.c.

Macro Definition Documentation

◆ FLYWEIGHT_CLASS_LOCK

#define FLYWEIGHT_CLASS_LOCK (   instance)    DPUTIL_LOCK(instance->lock)

Definition at line 73 of file flyweight.c.

◆ FLYWEIGHT_CLASS_UNLOCK

#define FLYWEIGHT_CLASS_UNLOCK   DPUTIL_UNLOCK

Definition at line 74 of file flyweight.c.

◆ FLYWEIGHT_FAILED

#define FLYWEIGHT_FAILED   (-1) /*! error */

Definition at line 18 of file flyweight.c.

◆ FLYWEIGHT_SUCCESS

#define FLYWEIGHT_SUCCESS   (0) /*! success */

Definition at line 19 of file flyweight.c.

Typedef Documentation

◆ FlyweightInstance

Definition at line 26 of file flyweight.c.

Function Documentation

◆ flyweight_class_default_constructor()

static void flyweight_class_default_constructor ( void *  this,
size_t  size,
void *  input_parameter 
)
inlinestatic

Default constructor.

Definition at line 170 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_class_default_equall_operand()

static int flyweight_class_default_equall_operand ( void *  this,
size_t  size,
void *  input_parameter 
)
inlinestatic

Default equall operand.

Definition at line 179 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_class_default_setter()

static int flyweight_class_default_setter ( void *  this,
size_t  size,
void *  input_parameter 
)
inlinestatic

Default setter.

Definition at line 188 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_class_set_methods()

static void flyweight_class_set_methods ( FlyweightMethodsIF  methods,
FlyweightFactory  class_factory 
)
static

Set methods.

Definition at line 198 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_factory_free()

void flyweight_factory_free ( FlyweightFactory  this)

clear class handle

Parameters
[in]thisFlyweightFactory instance returned at flyweight_factory_new,
Returns
none

Definition at line 293 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_factory_get()

static FlyweightInstance flyweight_factory_get ( FlyweightFactory  class_factory,
void *  constructor_parameter 
)
static

Getter.

Definition at line 152 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_factory_get_storaged_instance()

static FlyweightInstance flyweight_factory_get_storaged_instance ( FlyweightFactory  class_factory,
void *  constructor_parameter 
)
static

Check has instance.

Definition at line 107 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_factory_instance_new()

static FlyweightInstance flyweight_factory_instance_new ( FlyweightFactory  class_factory,
void *  constructor_parameter 
)
static

allocate.

Definition at line 126 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_factory_new()

FlyweightFactory flyweight_factory_new ( size_t  class_size,
int  is_threadsafe,
FlyweightMethodsIF  methods 
)

define class for flyweight

Parameters
[in]class_sizesize of instance which defined in user side.
[in]is_threadsafeif !=0, ensure threadsafe to create new class instace, please set !=0 if you want to use this API on multi thread
[in]methodsfor generating class instance If NULL, use defautlt. If not NULL, override methods. override NULL, this method is no effect. destructor is called at free
Return values
!=NULLthis class handle
NULLerror

Definition at line 212 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_factory_pop_instance()

static FlyweightInstance flyweight_factory_pop_instance ( FlyweightFactory  class_factory)
static

pop instance from list.

Definition at line 143 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_factory_push_instance()

static void flyweight_factory_push_instance ( FlyweightInstance  instance,
FlyweightFactory  class_factory 
)
static

push instance into list.

Definition at line 138 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_get()

void* flyweight_get ( FlyweightFactory  this,
void *  constructor_parameter 
)

getter

Parameters
[in]thisFlyweightFactory instance returned at flyweight_factory_new, first time to call get, allocate class, memset 0 and call constructor
[in]constructor_parameterconstructor parameter
Return values
!NULLclass instance
NULLid is invalid

Definition at line 244 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flyweight_instance_free()

static void flyweight_instance_free ( FlyweightInstance  instance)
static

Definition at line 97 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_instance_new()

static FlyweightInstance flyweight_instance_new ( size_t  size)
static

Definition at line 80 of file flyweight.c.

Here is the caller graph for this function:

◆ flyweight_set()

int flyweight_set ( FlyweightFactory  this,
void *  constructor_parameter,
void *  data,
int(*)(void *this, size_t size, void *input_parameter)  setter 
)

setter

Parameters
[in]thisFlyweightFactory instance returned at flyweight_factory_new,
[in]constructor_parameterconstructor parameter
[in]dataset data pointer
[in]settersetter if you want to change setter ( if NULL, use setter related to flyweight_register_class input)

Definition at line 264 of file flyweight.c.

Here is the call graph for this function:
Here is the caller graph for this function: