NCDObject.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "NCDObject.h"
  30. int NCDObject_no_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
  31. {
  32. return 0;
  33. }
  34. int NCDObject_no_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  35. {
  36. return 0;
  37. }
  38. NCDObject NCDObject_Build (NCD_string_id_t type, void *data_ptr, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj)
  39. {
  40. ASSERT(type >= -1)
  41. ASSERT(func_getvar)
  42. ASSERT(func_getobj)
  43. NCDObject obj;
  44. obj.type = type;
  45. obj.data_int = 0;
  46. obj.data_ptr = data_ptr;
  47. obj.method_user = data_ptr;
  48. obj.func_getvar = func_getvar;
  49. obj.func_getobj = func_getobj;
  50. return obj;
  51. }
  52. 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)
  53. {
  54. ASSERT(type >= -1)
  55. ASSERT(func_getvar)
  56. ASSERT(func_getobj)
  57. NCDObject obj;
  58. obj.type = type;
  59. obj.data_int = data_int;
  60. obj.data_ptr = data_ptr;
  61. obj.method_user = method_user;
  62. obj.func_getvar = func_getvar;
  63. obj.func_getobj = func_getobj;
  64. return obj;
  65. }
  66. NCD_string_id_t NCDObject_Type (const NCDObject *o)
  67. {
  68. return o->type;
  69. }
  70. void * NCDObject_DataPtr (const NCDObject *o)
  71. {
  72. return o->data_ptr;
  73. }
  74. int NCDObject_DataInt (const NCDObject *o)
  75. {
  76. return o->data_int;
  77. }
  78. void * NCDObject_MethodUser (const NCDObject *o)
  79. {
  80. return o->method_user;
  81. }
  82. int NCDObject_GetVar (const NCDObject *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
  83. {
  84. ASSERT(name >= 0)
  85. ASSERT(mem)
  86. ASSERT(out_value)
  87. int res = o->func_getvar(o, name, mem, out_value);
  88. ASSERT(res == 0 || res == 1)
  89. ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
  90. return res;
  91. }
  92. int NCDObject_GetObj (const NCDObject *o, NCD_string_id_t name, NCDObject *out_object)
  93. {
  94. ASSERT(name >= 0)
  95. ASSERT(out_object)
  96. int res = o->func_getobj(o, name, out_object);
  97. ASSERT(res == 0 || res == 1)
  98. return res;
  99. }
  100. static NCDObject NCDObject__dig_into_object (NCDObject object)
  101. {
  102. NCDObject obj2;
  103. while (NCDObject_GetObj(&object, NCD_STRING_EMPTY, &obj2)) {
  104. object = obj2;
  105. }
  106. return object;
  107. }
  108. int NCDObject_ResolveVarExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
  109. {
  110. ASSERT(num_names == 0 || names)
  111. ASSERT(mem)
  112. ASSERT(out_value)
  113. NCDObject object = NCDObject__dig_into_object(*o);
  114. while (num_names > 0) {
  115. NCDObject obj2;
  116. if (!NCDObject_GetObj(&object, *names, &obj2)) {
  117. if (num_names == 1 && NCDObject_GetVar(&object, *names, mem, out_value)) {
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. object = NCDObject__dig_into_object(obj2);
  123. names++;
  124. num_names--;
  125. }
  126. return NCDObject_GetVar(&object, NCD_STRING_EMPTY, mem, out_value);
  127. }
  128. int NCDObject_ResolveObjExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
  129. {
  130. ASSERT(num_names == 0 || names)
  131. ASSERT(out_object)
  132. NCDObject object = NCDObject__dig_into_object(*o);
  133. while (num_names > 0) {
  134. NCDObject obj2;
  135. if (!NCDObject_GetObj(&object, *names, &obj2)) {
  136. return 0;
  137. }
  138. object = NCDObject__dig_into_object(obj2);
  139. names++;
  140. num_names--;
  141. }
  142. *out_object = object;
  143. return 1;
  144. }