ref.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @file ref.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. * @section DESCRIPTION
  30. *
  31. * References module.
  32. *
  33. * Synopsis:
  34. * refhere()
  35. * Variables:
  36. * Exposes variables and objects as seen from this refhere() statement.
  37. *
  38. * Synopsis:
  39. * ref refhere::ref()
  40. * ref ref::ref()
  41. * Variables:
  42. * Exposes variables and objects as seen from the corresponding refhere()
  43. * statement.
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <misc/offset.h>
  48. #include <structure/LinkedList0.h>
  49. #include <ncd/NCDModule.h>
  50. #include <generated/blog_channel_ncd_ref.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct refhere_instance {
  53. NCDModuleInst *i;
  54. LinkedList0 refs_list;
  55. };
  56. struct ref_instance {
  57. NCDModuleInst *i;
  58. struct refhere_instance *rh;
  59. LinkedList0Node refs_list_node;
  60. };
  61. static void ref_instance_free (struct ref_instance *o);
  62. static void refhere_func_new (NCDModuleInst *i)
  63. {
  64. // allocate instance
  65. struct refhere_instance *o = malloc(sizeof(*o));
  66. if (!o) {
  67. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  68. goto fail0;
  69. }
  70. NCDModuleInst_Backend_SetUser(i, o);
  71. // init arguments
  72. o->i = i;
  73. // check arguments
  74. if (!NCDValue_ListRead(i->args, 0)) {
  75. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  76. goto fail1;
  77. }
  78. // init refs list
  79. LinkedList0_Init(&o->refs_list);
  80. // signal up
  81. NCDModuleInst_Backend_Up(o->i);
  82. return;
  83. fail1:
  84. free(o);
  85. fail0:
  86. NCDModuleInst_Backend_SetError(i);
  87. NCDModuleInst_Backend_Dead(i);
  88. }
  89. static void refhere_func_die (void *vo)
  90. {
  91. struct refhere_instance *o = vo;
  92. NCDModuleInst *i = o->i;
  93. // die refs
  94. while (!LinkedList0_IsEmpty(&o->refs_list)) {
  95. struct ref_instance *ref = UPPER_OBJECT(LinkedList0_GetFirst(&o->refs_list), struct ref_instance, refs_list_node);
  96. ASSERT(ref->rh == o)
  97. ref_instance_free(ref);
  98. }
  99. // free instance
  100. free(o);
  101. NCDModuleInst_Backend_Dead(i);
  102. }
  103. static int refhere_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  104. {
  105. struct refhere_instance *o = vo;
  106. // We don't redirect methods, and there will never be an object
  107. // with empty name. Fail here so we don't report non-errors.
  108. if (!strcmp(objname, "")) {
  109. return 0;
  110. }
  111. return NCDModuleInst_Backend_GetObj(o->i, objname, out_object);
  112. }
  113. static void ref_func_new_templ (NCDModuleInst *i, struct refhere_instance *rh)
  114. {
  115. // allocate instance
  116. struct ref_instance *o = malloc(sizeof(*o));
  117. if (!o) {
  118. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  119. goto fail0;
  120. }
  121. NCDModuleInst_Backend_SetUser(i, o);
  122. // init arguments
  123. o->i = i;
  124. // check arguments
  125. if (!NCDValue_ListRead(i->args, 0)) {
  126. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  127. goto fail1;
  128. }
  129. // set refhere
  130. o->rh = rh;
  131. // add to refhere's refs list
  132. LinkedList0_Prepend(&o->rh->refs_list, &o->refs_list_node);
  133. // signal up
  134. NCDModuleInst_Backend_Up(o->i);
  135. return;
  136. fail1:
  137. free(o);
  138. fail0:
  139. NCDModuleInst_Backend_SetError(i);
  140. NCDModuleInst_Backend_Dead(i);
  141. }
  142. static void ref_func_new_from_refhere (NCDModuleInst *i)
  143. {
  144. struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  145. return ref_func_new_templ(i, rh);
  146. }
  147. static void ref_func_new_from_ref (NCDModuleInst *i)
  148. {
  149. struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  150. return ref_func_new_templ(i, ref->rh);
  151. }
  152. static void ref_instance_free (struct ref_instance *o)
  153. {
  154. NCDModuleInst *i = o->i;
  155. // remove from refhere's reft list
  156. LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
  157. // free instance
  158. free(o);
  159. NCDModuleInst_Backend_Dead(i);
  160. }
  161. static void ref_func_die (void *vo)
  162. {
  163. struct ref_instance *o = vo;
  164. ref_instance_free(o);
  165. }
  166. static int ref_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  167. {
  168. struct ref_instance *o = vo;
  169. // We don't redirect methods, and there will never be an object
  170. // with empty name. Fail here so we don't report non-errors.
  171. if (!strcmp(objname, "")) {
  172. return 0;
  173. }
  174. return NCDModuleInst_Backend_GetObj(o->rh->i, objname, out_object);
  175. }
  176. static const struct NCDModule modules[] = {
  177. {
  178. .type = "refhere",
  179. .func_new = refhere_func_new,
  180. .func_die = refhere_func_die,
  181. .func_getobj = refhere_func_getobj
  182. }, {
  183. .type = "refhere::ref",
  184. .base_type = "ref",
  185. .func_new = ref_func_new_from_refhere,
  186. .func_die = ref_func_die,
  187. .func_getobj = ref_func_getobj
  188. }, {
  189. .type = "ref::ref",
  190. .base_type = "ref",
  191. .func_new = ref_func_new_from_ref,
  192. .func_die = ref_func_die,
  193. .func_getobj = ref_func_getobj
  194. }, {
  195. .type = NULL
  196. }
  197. };
  198. const struct NCDModuleGroup ncdmodule_ref = {
  199. .modules = modules
  200. };