design_pattern_for_c  V 1.00
prototype_factory.c
Go to the documentation of this file.
1 
5 #include <stdlib.h>
6 #include "dp_util.h"
7 #include "prototype_factory.h"
8 
11  void * base;
12  size_t base_len;
14 };
15 
16 /*@name prototype_factory_instance private API definition*/
17 /* @{ */
19 static void * prototype_default_clone(void * base, size_t base_length);
21 static void prototype_default_free(void * clone_base);
23 static void prototype_default_free_base(void * base);
24 /* @} */
25 
27 #define PROT_FACT_DEFAULT_METHOD(key) prototype_default_ ## key
28 
29 #define PROT_FACT_SET_METHOD(this, factory_method, key) \
30  if(factory_method && factory_method->key) instance->factory_method.key = factory_method->key;\
31  else instance->factory_method.key = PROT_FACT_DEFAULT_METHOD(key)
32 
33 /*@name prototype_factory_instance private API definition*/
34 /* @{ */
35 static void * prototype_default_clone(void * base, size_t base_length) {
36  void * clone_data = malloc(base_length);
37  if(!clone_data) return NULL;
38 
39  /*shallow copy*/
40  memcpy(clone_data, base, base_length);
41  return clone_data;
42 }
43 
44 static void prototype_default_free(void * clone_base) {
45  free(clone_base);
46 }
47 
48 static void prototype_default_free_base(void * base) {
49  free(base);
50 }
51 /* @} */
52 /*@name prototype_factory_instance public API definition*/
53 /* @{ */
55 PrototypeFactoryInstance prototype_factory_instance_new(void * base, size_t base_length, prototype_factory_method_t * factory_method) {
56  PrototypeFactoryInstance instance = calloc(1, sizeof(*instance));
57  if(!instance) {
58  return NULL;
59  }
60 
61  instance->base = base;
62  instance->base_len = base_length;
63 
64  PROT_FACT_SET_METHOD(instance, factory_method, clone);
65  PROT_FACT_SET_METHOD(instance, factory_method, free);
66  PROT_FACT_SET_METHOD(instance, factory_method, free_base);
67  return instance;
68 }
69 
72  this->factory_method.free_base(this->base);
73  free(this);
74 }
75 
78  return this->factory_method.clone(this->base, this->base_len);
79 }
80 
83  this->factory_method.free(data);
84 }
85 /* @} */
Utility headers
void prototype_factory_instance_free(PrototypeFactoryInstance this)
free
static void * prototype_default_clone(void *base, size_t base_length)
clone
static void prototype_default_free(void *clone_base)
free
void(* free)(EventInstance this)
Definition: event_thread.c:39
prototype_factory_instance, PrototypeFactory class instance definition
void * prototype_factory_instance_clone_data(PrototypeFactoryInstance this)
clone data
void prototype_factory_instance_free_data(PrototypeFactoryInstance this, void *data)
free data
PrototypeFactoryInstance prototype_factory_instance_new(void *base, size_t base_length, prototype_factory_method_t *factory_method)
new
This is API for PrototypeFactoryInstance class.
#define PROT_FACT_SET_METHOD(this, factory_method, key)
define to set method
prototype_factory_method_t factory_method
static void prototype_default_free_base(void *base)
free base
PrototypeFactory methods interface definition, to set prototype_register.
Definition: prototype.h:28