NCDObject.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file NCDObject.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <misc/offset.h>
  30. #include <misc/debug.h>
  31. #include <ncd/NCDVal.h>
  32. #include "NCDObject.h"
  33. int NCDObject_no_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
  34. {
  35. return 0;
  36. }
  37. int NCDObject_no_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  38. {
  39. return 0;
  40. }
  41. NCDObject NCDObject_Build (NCD_string_id_t type, void *data_ptr, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj)
  42. {
  43. ASSERT(type >= -1)
  44. ASSERT(func_getvar)
  45. ASSERT(func_getobj)
  46. NCDObject obj;
  47. obj.type = type;
  48. obj.data_int = 0;
  49. obj.data_ptr = data_ptr;
  50. obj.method_user = data_ptr;
  51. obj.func_getvar = func_getvar;
  52. obj.func_getobj = func_getobj;
  53. obj.pobj = NULL;
  54. return obj;
  55. }
  56. NCDObject NCDObject_BuildFull (NCD_string_id_t type, void *data_ptr, int data_int, void *method_user, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj, NCDPersistentObj *pobj)
  57. {
  58. ASSERT(type >= -1)
  59. ASSERT(func_getvar)
  60. ASSERT(func_getobj)
  61. NCDObject obj;
  62. obj.type = type;
  63. obj.data_int = data_int;
  64. obj.data_ptr = data_ptr;
  65. obj.method_user = method_user;
  66. obj.func_getvar = func_getvar;
  67. obj.func_getobj = func_getobj;
  68. obj.pobj = pobj;
  69. return obj;
  70. }
  71. NCD_string_id_t NCDObject_Type (const NCDObject *o)
  72. {
  73. return o->type;
  74. }
  75. void * NCDObject_DataPtr (const NCDObject *o)
  76. {
  77. return o->data_ptr;
  78. }
  79. int NCDObject_DataInt (const NCDObject *o)
  80. {
  81. return o->data_int;
  82. }
  83. void * NCDObject_MethodUser (const NCDObject *o)
  84. {
  85. return o->method_user;
  86. }
  87. int NCDObject_GetVar (const NCDObject *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
  88. {
  89. ASSERT(name >= 0)
  90. ASSERT(mem)
  91. ASSERT(out_value)
  92. int res = o->func_getvar(o, name, mem, out_value);
  93. ASSERT(res == 0 || res == 1)
  94. ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
  95. return res;
  96. }
  97. int NCDObject_GetObj (const NCDObject *o, NCD_string_id_t name, NCDObject *out_object)
  98. {
  99. ASSERT(name >= 0)
  100. ASSERT(out_object)
  101. int res = o->func_getobj(o, name, out_object);
  102. ASSERT(res == 0 || res == 1)
  103. return res;
  104. }
  105. NCDPersistentObj * NCDObject_Pobj (const NCDObject *o)
  106. {
  107. return o->pobj;
  108. }
  109. static NCDObject NCDObject__dig_into_object (NCDObject object)
  110. {
  111. NCDObject obj2;
  112. while (NCDObject_GetObj(&object, NCD_STRING_EMPTY, &obj2)) {
  113. object = obj2;
  114. }
  115. return object;
  116. }
  117. int NCDObject_ResolveVarExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  118. {
  119. ASSERT(num_names == 0 || names)
  120. ASSERT(mem)
  121. ASSERT(out_value)
  122. NCDObject object = NCDObject__dig_into_object(*o);
  123. while (num_names > 0) {
  124. NCDObject obj2;
  125. if (!NCDObject_GetObj(&object, *names, &obj2)) {
  126. if (num_names == 1 && NCDObject_GetVar(&object, *names, mem, out_value)) {
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. object = NCDObject__dig_into_object(obj2);
  132. names++;
  133. num_names--;
  134. }
  135. return NCDObject_GetVar(&object, NCD_STRING_EMPTY, mem, out_value);
  136. }
  137. int NCDObject_ResolveObjExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
  138. {
  139. ASSERT(num_names == 0 || names)
  140. ASSERT(out_object)
  141. NCDObject object = NCDObject__dig_into_object(*o);
  142. while (num_names > 0) {
  143. NCDObject obj2;
  144. if (!NCDObject_GetObj(&object, *names, &obj2)) {
  145. return 0;
  146. }
  147. object = NCDObject__dig_into_object(obj2);
  148. names++;
  149. num_names--;
  150. }
  151. *out_object = object;
  152. return 1;
  153. }
  154. void NCDPersistentObj_Init (NCDPersistentObj *o, NCDPersistentObj_func_retobj func_retobj)
  155. {
  156. ASSERT(func_retobj)
  157. o->func_retobj = func_retobj;
  158. LinkedList0_Init(&o->refs_list);
  159. }
  160. void NCDPersistentObj_Free (NCDPersistentObj *o)
  161. {
  162. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->refs_list); ln; ln = LinkedList0Node_Next(ln)) {
  163. NCDObjRef *ref = UPPER_OBJECT(ln, NCDObjRef, refs_list_node);
  164. ASSERT(ref->pobj == o)
  165. ref->pobj = NULL;
  166. }
  167. }
  168. void NCDObjRef_Init (NCDObjRef *o, NCDPersistentObj *pobj)
  169. {
  170. o->pobj = pobj;
  171. if (o->pobj) {
  172. LinkedList0_Prepend(&o->pobj->refs_list, &o->refs_list_node);
  173. }
  174. }
  175. void NCDObjRef_Free (NCDObjRef *o)
  176. {
  177. if (o->pobj) {
  178. LinkedList0_Remove(&o->pobj->refs_list, &o->refs_list_node);
  179. }
  180. }
  181. int NCDObjRef_Deref (NCDObjRef *o, NCDObject *res)
  182. {
  183. ASSERT(res)
  184. if (!o->pobj) {
  185. return 0;
  186. }
  187. *res = o->pobj->func_retobj(o->pobj);
  188. return 1;
  189. }