| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* object.c: Object management. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
|
| 26 |
/* |
| 27 |
* mowgli_object_init |
| 28 |
* |
| 29 |
* Populates the object manager part of an object. |
| 30 |
* |
| 31 |
* Inputs: |
| 32 |
* - pointer to object manager area |
| 33 |
* - (optional) name of object |
| 34 |
* - (optional) class of object |
| 35 |
* - (optional) custom destructor |
| 36 |
* |
| 37 |
* Outputs: |
| 38 |
* - none |
| 39 |
* |
| 40 |
* Side Effects: |
| 41 |
* - none |
| 42 |
*/ |
| 43 |
void |
| 44 |
mowgli_object_init(mowgli_object_t *obj, const char *name, mowgli_object_class_t *klass, mowgli_destructor_t des) |
| 45 |
{ |
| 46 |
return_if_fail(obj != NULL); |
| 47 |
|
| 48 |
if (name != NULL) |
| 49 |
obj->name = mowgli_strdup(name); |
| 50 |
|
| 51 |
if (klass != NULL) |
| 52 |
{ |
| 53 |
obj->klass = klass; |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
mowgli_object_class_t *tmp = mowgli_alloc(sizeof *tmp); |
| 58 |
mowgli_object_class_init(tmp, name, des, TRUE); |
| 59 |
obj->klass = tmp; |
| 60 |
} |
| 61 |
|
| 62 |
obj->refcount = 1; |
| 63 |
|
| 64 |
obj->message_handlers.head = NULL; |
| 65 |
obj->message_handlers.tail = NULL; |
| 66 |
obj->message_handlers.count = 0; |
| 67 |
|
| 68 |
obj->metadata.head = NULL; |
| 69 |
obj->metadata.tail = NULL; |
| 70 |
obj->metadata.count = 0; |
| 71 |
|
| 72 |
mowgli_object_message_broadcast(obj, "create"); |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* mowgli_object_init_from_class |
| 77 |
* |
| 78 |
* Populates the object manager part of an object from an object class. |
| 79 |
* |
| 80 |
* Inputs: |
| 81 |
* - pointer to object manager area |
| 82 |
* - class of object |
| 83 |
* |
| 84 |
* Outputs: |
| 85 |
* - none |
| 86 |
* |
| 87 |
* Side Effects: |
| 88 |
* - none |
| 89 |
*/ |
| 90 |
void |
| 91 |
mowgli_object_init_from_class(mowgli_object_t *obj, const char *name, mowgli_object_class_t *klass) |
| 92 |
{ |
| 93 |
return_if_fail(obj != NULL); |
| 94 |
return_if_fail(klass != NULL); |
| 95 |
|
| 96 |
mowgli_object_init(obj, name, klass, NULL); |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* mowgli_object_ref |
| 101 |
* |
| 102 |
* Increment the reference counter on an object. |
| 103 |
* |
| 104 |
* Inputs: |
| 105 |
* - the object to refcount |
| 106 |
* |
| 107 |
* Outputs: |
| 108 |
* - none |
| 109 |
* |
| 110 |
* Side Effects: |
| 111 |
* - none |
| 112 |
*/ |
| 113 |
void * |
| 114 |
mowgli_object_ref(void *object) |
| 115 |
{ |
| 116 |
return_val_if_fail(object != NULL, NULL); |
| 117 |
|
| 118 |
mowgli_object(object)->refcount++; |
| 119 |
|
| 120 |
return object; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* mowgli_object_unref |
| 125 |
* |
| 126 |
* Decrement the reference counter on an object. |
| 127 |
* |
| 128 |
* Inputs: |
| 129 |
* - the object to refcount |
| 130 |
* |
| 131 |
* Outputs: |
| 132 |
* - none |
| 133 |
* |
| 134 |
* Side Effects: |
| 135 |
* - if the refcount is 0, the object is destroyed. |
| 136 |
*/ |
| 137 |
void |
| 138 |
mowgli_object_unref(void *object) |
| 139 |
{ |
| 140 |
mowgli_object_t *obj = mowgli_object(object); |
| 141 |
|
| 142 |
return_if_fail(object != NULL); |
| 143 |
|
| 144 |
obj->refcount--; |
| 145 |
|
| 146 |
if (obj->refcount <= 0) |
| 147 |
{ |
| 148 |
mowgli_object_message_broadcast(obj, "destroy"); |
| 149 |
|
| 150 |
if (obj->name != NULL) |
| 151 |
free(obj->name); |
| 152 |
|
| 153 |
if (obj->klass != NULL) |
| 154 |
{ |
| 155 |
mowgli_destructor_t destructor = obj->klass->destructor; |
| 156 |
|
| 157 |
if (obj->klass->dynamic == TRUE) |
| 158 |
mowgli_object_class_destroy(obj->klass); |
| 159 |
|
| 160 |
if (destructor != NULL) |
| 161 |
destructor(obj); |
| 162 |
else |
| 163 |
free(obj); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
mowgli_log_warning("invalid object class"); |
| 168 |
} |
| 169 |
} |
| 170 |
} |