valuemetic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file valuemetic.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. * Comparison functions for values.
  32. *
  33. * Synopsis:
  34. * val_lesser(v1, v2)
  35. * val_greater(v1, v2)
  36. * val_lesser_equal(v1, v2)
  37. * val_greater_equal(v1, v2)
  38. * val_equal(v1, v2)
  39. * val_different(v1, v2)
  40. *
  41. * Variables:
  42. * (empty) - "true" or "false", reflecting the value of the relation in question
  43. *
  44. * Description:
  45. * These statements perform comparisons of values. Order of values is defined by the
  46. * following rules:
  47. * 1. Values of different types have the following order: strings, lists, maps.
  48. * 2. String values are ordered lexicographically, with respect to the numeric values
  49. * of their bytes.
  50. * 3. List values are ordered lexicographically, where the order of the elements is
  51. * defined by recursive application of these rules.
  52. * 4. Map values are ordered lexicographically, as if a map was a list of (key, value)
  53. * pairs ordered by key, where the order of both keys and values is defined by
  54. * recursive application of these rules.
  55. */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <ncd/NCDModule.h>
  59. #include <ncd/static_strings.h>
  60. #include <generated/blog_channel_ncd_valuemetic.h>
  61. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  62. struct instance {
  63. NCDModuleInst *i;
  64. int result;
  65. };
  66. typedef int (*compute_func) (NCDValRef v1, NCDValRef v2);
  67. static int compute_lesser (NCDValRef v1, NCDValRef v2)
  68. {
  69. return NCDVal_Compare(v1, v2) < 0;
  70. }
  71. static int compute_greater (NCDValRef v1, NCDValRef v2)
  72. {
  73. return NCDVal_Compare(v1, v2) > 0;
  74. }
  75. static int compute_lesser_equal (NCDValRef v1, NCDValRef v2)
  76. {
  77. return NCDVal_Compare(v1, v2) <= 0;
  78. }
  79. static int compute_greater_equal (NCDValRef v1, NCDValRef v2)
  80. {
  81. return NCDVal_Compare(v1, v2) >= 0;
  82. }
  83. static int compute_equal (NCDValRef v1, NCDValRef v2)
  84. {
  85. return NCDVal_Compare(v1, v2) == 0;
  86. }
  87. static int compute_different (NCDValRef v1, NCDValRef v2)
  88. {
  89. return NCDVal_Compare(v1, v2) != 0;
  90. }
  91. static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, compute_func cfunc)
  92. {
  93. struct instance *o = vo;
  94. o->i = i;
  95. NCDValRef v1_arg;
  96. NCDValRef v2_arg;
  97. if (!NCDVal_ListRead(params->args, 2, &v1_arg, &v2_arg)) {
  98. ModuleLog(i, BLOG_ERROR, "wrong arity");
  99. goto fail0;
  100. }
  101. o->result = cfunc(v1_arg, v2_arg);
  102. NCDModuleInst_Backend_Up(i);
  103. return;
  104. fail0:
  105. NCDModuleInst_Backend_SetError(i);
  106. NCDModuleInst_Backend_Dead(i);
  107. }
  108. static void func_die (void *vo)
  109. {
  110. struct instance *o = vo;
  111. NCDModuleInst_Backend_Dead(o->i);
  112. }
  113. static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  114. {
  115. struct instance *o = vo;
  116. if (name == NCD_STRING_EMPTY) {
  117. const char *str = o->result ? "true" : "false";
  118. *out = NCDVal_NewString(mem, str);
  119. if (NCDVal_IsInvalid(*out)) {
  120. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  121. }
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  127. {
  128. new_templ(vo, i, params, compute_lesser);
  129. }
  130. static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  131. {
  132. new_templ(vo, i, params, compute_greater);
  133. }
  134. static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  135. {
  136. new_templ(vo, i, params, compute_lesser_equal);
  137. }
  138. static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  139. {
  140. new_templ(vo, i, params, compute_greater_equal);
  141. }
  142. static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  143. {
  144. new_templ(vo, i, params, compute_equal);
  145. }
  146. static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  147. {
  148. new_templ(vo, i, params, compute_different);
  149. }
  150. static struct NCDModule modules[] = {
  151. {
  152. .type = "val_lesser",
  153. .func_new2 = func_new_lesser,
  154. .func_die = func_die,
  155. .func_getvar2 = func_getvar2,
  156. .alloc_size = sizeof(struct instance)
  157. }, {
  158. .type = "val_greater",
  159. .func_new2 = func_new_greater,
  160. .func_die = func_die,
  161. .func_getvar2 = func_getvar2,
  162. .alloc_size = sizeof(struct instance)
  163. }, {
  164. .type = "val_lesser_equal",
  165. .func_new2 = func_new_lesser_equal,
  166. .func_die = func_die,
  167. .func_getvar2 = func_getvar2,
  168. .alloc_size = sizeof(struct instance)
  169. }, {
  170. .type = "val_greater_equal",
  171. .func_new2 = func_new_greater_equal,
  172. .func_die = func_die,
  173. .func_getvar2 = func_getvar2,
  174. .alloc_size = sizeof(struct instance)
  175. }, {
  176. .type = "val_equal",
  177. .func_new2 = func_new_equal,
  178. .func_die = func_die,
  179. .func_getvar2 = func_getvar2,
  180. .alloc_size = sizeof(struct instance)
  181. }, {
  182. .type = "val_different",
  183. .func_new2 = func_new_different,
  184. .func_die = func_die,
  185. .func_getvar2 = func_getvar2,
  186. .alloc_size = sizeof(struct instance)
  187. }, {
  188. .type = NULL
  189. }
  190. };
  191. const struct NCDModuleGroup ncdmodule_valuemetic = {
  192. .modules = modules
  193. };