Coconut Framework  beta
CNObject.h
Go to the documentation of this file.
1 
8 #ifndef CNOBJECT_H
9 #define CNOBJECT_H
10 
11 #include "CNResource.h"
12 #include "CNForwarders.h"
13 #include <stdlib.h>
14 #include <assert.h>
15 
21 static const unsigned long CNReferenceCountForStaticObject = ((unsigned long) -1) ;
22 
24 typedef void (*CNDeallocateObjectFuncRef)(struct CNObject * dst) ;
25 
30 {
33 } ;
34 
38 struct CNObject
39 {
41  unsigned long referenceCount ;
43  size_t sizeOfObject ;
45  struct CNResource * resource ;
47  const struct CNObjectMethods * methods ;
48 } ;
49 
58 static inline struct CNObject *
59 CNAllocateObject(size_t size, const struct CNObjectMethods * methods, struct CNResource * resource)
60 {
61  assert(size>0 && methods != NULL && resource != NULL) ;
62  struct CNObject * newobj = CNAllocateData(resource, size) ;
63  newobj->referenceCount = 1 ;
64  newobj->sizeOfObject = size ;
65  newobj->resource = resource ;
66  newobj->methods = methods ;
67  return newobj ;
68 }
69 
74 static inline void
75 CNRetainObject(struct CNObject * dst)
76 {
78  (dst->referenceCount)++ ;
79  }
80 }
81 
86 void
87 CNReleaseObject(struct CNObject * dst) ;
88 
94 static inline struct CNResource *
96 {
97  return src->resource ;
98 }
99 
105 static inline const struct CNObjectMethods *
106 CNMethodsOfObject(const struct CNObject * src)
107 {
108  return src->methods ;
109 }
110 
116 static inline size_t
117 CNSizeOfObject(const struct CNObject * src)
118 {
119  return src->sizeOfObject ;
120 }
121 
122 #endif /* CNOBJECT_H */
Define CNResource data structure.
struct CNResource * resource
Definition: CNObject.h:45
Forward declaration of data types.
CNDeallocateObjectFuncRef deallocFuncRef
Definition: CNObject.h:32
Root object.
Definition: CNObject.h:38
void(* CNDeallocateObjectFuncRef)(struct CNObject *dst)
Definition: CNObject.h:24
size_t sizeOfObject
Definition: CNObject.h:43
Methods for CNObject.
Definition: CNObject.h:29
void CNReleaseObject(struct CNObject *dst)
Release the object.
struct CNObjectMethods * methods
Definition: CNObject.h:47
static void CNRetainObject(struct CNObject *dst)
Retain the object.
Definition: CNObject.h:75
void * CNAllocateData(struct CNResource *resource, size_t size)
Allocate data memory.
unsigned long referenceCount
Definition: CNObject.h:41
static size_t CNSizeOfObject(const struct CNObject *src)
Get size of object.
Definition: CNObject.h:117
static struct CNResource * CNResourceOfObject(struct CNObject *src)
Get resource for the object.
Definition: CNObject.h:95
static struct CNObject * CNAllocateObject(size_t size, const struct CNObjectMethods *methods, struct CNResource *resource)
Allocate object.
Definition: CNObject.h:59
static const unsigned long CNReferenceCountForStaticObject
Definition: CNObject.h:21
static struct CNObjectMethods * CNMethodsOfObject(const struct CNObject *src)
Get methods for the object.
Definition: CNObject.h:106