NCDVal.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /**
  2. * @file NCDVal.h
  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. #ifndef BADVPN_NCDVAL_H
  30. #define BADVPN_NCDVAL_H
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <misc/debug.h>
  34. #include <structure/CAvl.h>
  35. #include <ncd/NCDStringIndex.h>
  36. #include <ncd/NCDRefTarget.h>
  37. // these are implementation details. The interface is defined below.
  38. #define NCDVAL_FASTBUF_SIZE 64
  39. #define NCDVAL_FIRST_SIZE 256
  40. #define NCDVAL_MAXIDX INT_MAX
  41. #define NCDVAL_MINIDX INT_MIN
  42. typedef int NCDVal__idx;
  43. struct NCDVal__ref {
  44. NCDVal__idx next;
  45. NCDRefTarget *target;
  46. };
  47. struct NCDVal__string {
  48. int type;
  49. NCDVal__idx length;
  50. char data[];
  51. };
  52. struct NCDVal__list {
  53. int type;
  54. NCDVal__idx maxcount;
  55. NCDVal__idx count;
  56. NCDVal__idx elem_indices[];
  57. };
  58. struct NCDVal__mapelem {
  59. NCDVal__idx key_idx;
  60. NCDVal__idx val_idx;
  61. NCDVal__idx tree_child[2];
  62. NCDVal__idx tree_parent;
  63. int8_t tree_balance;
  64. };
  65. struct NCDVal__idstring {
  66. int type;
  67. NCD_string_id_t string_id;
  68. NCDStringIndex *string_index;
  69. };
  70. struct NCDVal__externalstring {
  71. int type;
  72. const char *data;
  73. size_t length;
  74. struct NCDVal__ref ref;
  75. };
  76. typedef struct {
  77. char *buf;
  78. NCDVal__idx size;
  79. NCDVal__idx used;
  80. NCDVal__idx first_ref;
  81. union {
  82. char fastbuf[NCDVAL_FASTBUF_SIZE];
  83. struct NCDVal__ref align_ref;
  84. struct NCDVal__string align_string;
  85. struct NCDVal__list align_list;
  86. struct NCDVal__mapelem align_mapelem;
  87. struct NCDVal__idstring align_idstring;
  88. struct NCDVal__externalstring align_externalstring;
  89. };
  90. } NCDValMem;
  91. typedef struct {
  92. NCDValMem *mem;
  93. NCDVal__idx idx;
  94. } NCDValRef;
  95. typedef struct {
  96. NCDVal__idx idx;
  97. } NCDValSafeRef;
  98. typedef struct NCDVal__mapelem NCDVal__maptree_entry;
  99. typedef NCDValMem *NCDVal__maptree_arg;
  100. #include "NCDVal_maptree.h"
  101. #include <structure/CAvl_decl.h>
  102. struct NCDVal__map {
  103. int type;
  104. NCDVal__idx maxcount;
  105. NCDVal__idx count;
  106. NCDVal__MapTree tree;
  107. struct NCDVal__mapelem elems[];
  108. };
  109. typedef struct {
  110. NCDVal__idx elemidx;
  111. } NCDValMapElem;
  112. #define NCDVAL_INSTR_PLACEHOLDER 0
  113. #define NCDVAL_INSTR_REINSERT 1
  114. struct NCDVal__instr {
  115. int type;
  116. union {
  117. struct {
  118. NCDVal__idx plid;
  119. NCDVal__idx plidx;
  120. } placeholder;
  121. struct {
  122. NCDVal__idx mapidx;
  123. NCDVal__idx elempos;
  124. } reinsert;
  125. };
  126. };
  127. typedef struct {
  128. struct NCDVal__instr *instrs;
  129. size_t num_instrs;
  130. } NCDValReplaceProg;
  131. typedef struct {
  132. char *data;
  133. int is_allocated;
  134. } NCDValNullTermString;
  135. //
  136. #define NCDVAL_STRING 1
  137. #define NCDVAL_LIST 2
  138. #define NCDVAL_MAP 3
  139. #define NCDVAL_PLACEHOLDER 4
  140. /**
  141. * Initializes a value memory object.
  142. * A value memory object holds memory for value structures. Values within
  143. * the memory are referenced using {@link NCDValRef} objects, which point
  144. * to values within memory objects.
  145. *
  146. * Values may be added to a memory object using functions such as
  147. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  148. * and {@link NCDVal_NewCopy}, which return references to the new values within
  149. * the memory object.
  150. *
  151. * It is not possible to remove values from the memory object, or modify existing
  152. * values other than adding elements to pre-allocated slots in lists and maps.
  153. * Once a value is added, it will consume memory as long as its memory object
  154. * exists. This is by design - this code is intended and optimized for constructing
  155. * and passing around values, not for operating on them in place. In fact, al
  156. * values within a memory object are stored in a single memory buffer, as an
  157. * embedded data structure with relativepointers. For example, map values use an
  158. * embedded AVL tree.
  159. */
  160. void NCDValMem_Init (NCDValMem *o);
  161. /**
  162. * Frees a value memory object.
  163. * All values within the memory object cease to exist, and any {@link NCDValRef}
  164. * object pointing to them must no longer be used.
  165. */
  166. void NCDValMem_Free (NCDValMem *o);
  167. /**
  168. * Initializes the memory object to be a copy of an existing memory object.
  169. * Value references from the original may be used if they are first turned
  170. * to {@link NCDValSafeRef} using {@link NCDVal_ToSafe} and back to
  171. * {@link NCDValRef} using {@link NCDVal_FromSafe} with the new memory object
  172. * specified. Alternatively, {@link NCDVal_Moved} can be used.
  173. * Returns 1 on success and 0 on failure.
  174. */
  175. int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other) WARN_UNUSED;
  176. /**
  177. * Does nothing.
  178. * The value reference object must either point to a valid value within a valid
  179. * memory object, or must be an invalid reference (most functions operating on
  180. * {@link NCDValRef} implicitly require that).
  181. */
  182. void NCDVal_Assert (NCDValRef val);
  183. /**
  184. * Determines if a value reference is invalid.
  185. */
  186. int NCDVal_IsInvalid (NCDValRef val);
  187. /**
  188. * Determines if a value is a placeholder value.
  189. * The value reference must not be an invalid reference.
  190. */
  191. int NCDVal_IsPlaceholder (NCDValRef val);
  192. /**
  193. * Returns the type of the value reference, which must not be an invalid reference.
  194. * Possible values are NCDVAL_STRING, NCDVAL_LIST, NCDVAL_MAP and NCDVAL_PLACEHOLDER.
  195. * The placeholder type is only used internally in the interpreter for argument
  196. * resolution, and is never seen by modules; see {@link NCDVal_NewPlaceholder}.
  197. */
  198. int NCDVal_Type (NCDValRef val);
  199. /**
  200. * Returns an invalid reference.
  201. * An invalid reference must not be passed to any function here, except:
  202. * {@link NCDVal_Assert}, {@link NCDVal_IsInvalid}, {@link NCDVal_ToSafe},
  203. * {@link NCDVal_FromSafe}, {@link NCDVal_Moved}.
  204. */
  205. NCDValRef NCDVal_NewInvalid (void);
  206. /**
  207. * Returns a new placeholder value reference. A placeholder value is a valid value
  208. * containing an integer placeholder identifier.
  209. * This always succeeds; however, the caller must ensure the identifier is
  210. * non-negative and satisfies (NCDVAL_MINIDX + plid < -1).
  211. *
  212. * The placeholder type is only used internally in the interpreter for argument
  213. * resolution, and is never seen by modules. Also see {@link NCDPlaceholderDb}.
  214. */
  215. NCDValRef NCDVal_NewPlaceholder (NCDValMem *mem, int plid);
  216. /**
  217. * Returns the indentifier of a placeholder value.
  218. * The value reference must point to a placeholder value.
  219. */
  220. int NCDVal_PlaceholderId (NCDValRef val);
  221. /**
  222. * Copies a value into the specified memory object. The source
  223. * must not be an invalid reference, however it may reside in any memory
  224. * object (including 'mem').
  225. * Returns a reference to the copied value. On out of memory, returns
  226. * an invalid reference.
  227. */
  228. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  229. /**
  230. * Compares two values, both of which must not be invalid references.
  231. * Returns -1, 0 or 1.
  232. */
  233. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  234. /**
  235. * Converts a value reference to a safe referece format, which remains valid
  236. * if the memory object is moved (safe references do not contain a pointer
  237. * to the memory object, unlike {@link NCDValRef} references).
  238. */
  239. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  240. /**
  241. * Converts a safe value reference to a normal value reference.
  242. * This should be used to recover references from safe references
  243. * after the memory object is moved.
  244. */
  245. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  246. /**
  247. * Fixes a value reference after its memory object was moved.
  248. */
  249. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  250. /**
  251. * Determines if a value is a string value.
  252. * The value reference must not be an invalid reference.
  253. */
  254. int NCDVal_IsString (NCDValRef val);
  255. /**
  256. * Determines if a value is an ID-string value. See {@link NCDVal_NewIdString}
  257. * for an explanation of ID-string values.
  258. * The value reference must not be an invalid reference.
  259. */
  260. int NCDVal_IsIdString (NCDValRef val);
  261. /**
  262. * Determines if a value is an external string value.
  263. * See {@link NCDVal_NewExternalString} for an explanation of external
  264. * string values.
  265. * The value reference must not be an invalid reference.
  266. */
  267. int NCDVal_IsExternalString (NCDValRef val);
  268. /**
  269. * Determines if a value is a string value which has no null bytes.
  270. * The value reference must not be an invalid reference.
  271. */
  272. int NCDVal_IsStringNoNulls (NCDValRef val);
  273. /**
  274. * Builds a new string value from a null-terminated array of bytes.
  275. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  276. * Returns a reference to the new value, or an invalid reference
  277. * on out of memory.
  278. * WARNING: The buffer passed must NOT be part of any value in the
  279. * memory object specified. In particular, you may NOT use this
  280. * function to copy a string that resides in the same memory object.
  281. */
  282. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  283. /**
  284. * Builds a new string value.
  285. * Returns a reference to the new value, or an invalid reference
  286. * on out of memory.
  287. * WARNING: The buffer passed must NOT be part of any value in the
  288. * memory object specified. In particular, you may NOT use this
  289. * function to copy a string that resides in the same memory object.
  290. */
  291. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  292. /**
  293. * Builds a new string value of the given length with undefined contents.
  294. * You can define the contents of the string later by copying to the address
  295. * returned by {@link NCDVal_StringData}.
  296. */
  297. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  298. /**
  299. * Builds a new ID-string value.
  300. * Returns a reference to the new value, or an invalid reference
  301. * on out of memory.
  302. *
  303. * An ID-string value is a special kind of string value which is represented
  304. * efficiently as a string identifier via {@link NCDStringIndex}. An ID-string
  305. * is also a string and is transparent for use. For example, for an ID-string,
  306. * {@link NCDVal_Type} still returns NCDVAL_STRING, {@link NCDVal_IsString}
  307. * returns 1, and {@link NCDVal_StringData} and {@link NCDVal_StringLength}
  308. * both work. The only way to distinguish an ID-string from a non-ID string is
  309. * by calling {@link NCDVal_IsIdString}.
  310. */
  311. NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id,
  312. NCDStringIndex *string_index);
  313. /**
  314. * Builds a new string value pointing to the given external data. A reference to
  315. * the external data is taken using {@link NCDRefTarget}. The data must not change
  316. * while the reference is being held. Like ID-strings, external strings are
  317. * transparent for use. An external string can be recognized using
  318. * {@link NCDVal_IsExternalString}.
  319. *
  320. * Returns a reference to the new value, or an invalid reference
  321. * on out of memory.
  322. */
  323. NCDValRef NCDVal_NewExternalString (NCDValMem *mem, const char *data, size_t len,
  324. NCDRefTarget *ref_target);
  325. /**
  326. * Returns a pointer to the data of a string value.
  327. * WARNING: the string data may not be null-terminated. To get a null-terminated
  328. * version, use {@link NCDVal_StringNullTerminate}.
  329. * The value reference must point to a string value.
  330. */
  331. const char * NCDVal_StringData (NCDValRef string);
  332. /**
  333. * Returns the length of the string value.
  334. * The value reference must point to a string value.
  335. */
  336. size_t NCDVal_StringLength (NCDValRef string);
  337. /**
  338. * Produces a null-terminated version of a string value. On success, the result is
  339. * stored into a {@link NCDValNullTermString} structure, and the null-terminated
  340. * string is available via its 'data' member. This function may either simply pass
  341. * through the data pointer as returned by {@link NCDVal_StringData} (if the string
  342. * is known to be null-terminated) or produce a null-terminated dynamically allocated
  343. * copy.
  344. * On success, {@link NCDValNullTermString_Free} should be called to release any allocated
  345. * memory when the null-terminated string is no longer needed. This must be called before
  346. * the memory object is freed, because it may point to data inside the memory object.
  347. * It is guaranteed that *out is not modified on failure.
  348. * Returns 1 on success and 0 on failure.
  349. */
  350. int NCDVal_StringNullTerminate (NCDValRef string, NCDValNullTermString *out) WARN_UNUSED;
  351. /**
  352. * Returns a dummy {@link NCDValNullTermString} which can be freed using
  353. * {@link NCDValNullTermString_Free}, but need not be.
  354. */
  355. NCDValNullTermString NCDValNullTermString_NewDummy (void);
  356. /**
  357. * Releases any memory which was dynamically allocated by {@link NCDVal_StringNullTerminate}
  358. * to null-terminate a string.
  359. */
  360. void NCDValNullTermString_Free (NCDValNullTermString *o);
  361. /**
  362. * Returns the string ID and the string index of an ID-string.
  363. * The value given must be an ID-string value (which can be determined via
  364. * {@link NCDVal_IsIdString}). Both the \a out_string_id and \a out_string_index
  365. * pointers must be non-NULL.
  366. */
  367. void NCDVal_IdStringGet (NCDValRef idstring, NCD_string_id_t *out_string_id,
  368. NCDStringIndex **out_string_index);
  369. /**
  370. * Returns the string ID of an ID-string.
  371. * The value given must be an ID-string value (which can be determined via
  372. * {@link NCDVal_IsIdString}).
  373. */
  374. NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring);
  375. /**
  376. * Returns the string index of an ID-string.
  377. * The value given must be an ID-string value (which can be determined via
  378. * {@link NCDVal_IsIdString}).
  379. */
  380. NCDStringIndex * NCDVal_IdStringStringIndex (NCDValRef idstring);
  381. /**
  382. * Returns the reference target of an external string.
  383. * The value given must be an external string value (which can be determined
  384. * via {@link NCDVal_IsExternalString}).
  385. */
  386. NCDRefTarget * NCDVal_ExternalStringTarget (NCDValRef externalstring);
  387. /**
  388. * Determines if the string value has any null bytes in its contents.
  389. * The value reference must point to a string value.
  390. */
  391. int NCDVal_StringHasNulls (NCDValRef string);
  392. /**
  393. * Determines if the string value is equal to the given null-terminated
  394. * string.
  395. * The value reference must point to a string value.
  396. */
  397. int NCDVal_StringEquals (NCDValRef string, const char *data);
  398. /**
  399. * Determines if the string value is equal to the given string represented
  400. * by an {@link NCDStringIndex} identifier.
  401. * The value reference must point to a string value.
  402. * NOTE: \a string_index must be equal to the string_index of every ID-string
  403. * that exist within this memory object.
  404. */
  405. int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id,
  406. NCDStringIndex *string_index);
  407. /**
  408. * Determines if a value is a list value.
  409. * The value reference must not be an invalid reference.
  410. */
  411. int NCDVal_IsList (NCDValRef val);
  412. /**
  413. * Builds a new list value. The 'maxcount' argument specifies how
  414. * many element slots to preallocate. Not more than that many
  415. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  416. * Returns a reference to the new value, or an invalid reference
  417. * on out of memory.
  418. */
  419. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  420. /**
  421. * Appends a value to to the list value.
  422. * The 'list' reference must point to a list value, and the
  423. * 'elem' reference must be non-invalid and point to a value within
  424. * the same memory object as the list.
  425. * Inserting a value into a list does not in any way change it;
  426. * internally, the list only points to it.
  427. */
  428. void NCDVal_ListAppend (NCDValRef list, NCDValRef elem);
  429. /**
  430. * Returns the number of elements in a list value, i.e. the number
  431. * of times {@link NCDVal_ListAppend} was called.
  432. * The 'list' reference must point to a list value.
  433. */
  434. size_t NCDVal_ListCount (NCDValRef list);
  435. /**
  436. * Returns the maximum number of elements a list value may contain,
  437. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  438. * The 'list' reference must point to a list value.
  439. */
  440. size_t NCDVal_ListMaxCount (NCDValRef list);
  441. /**
  442. * Returns a reference to the value at the given position 'pos' in a list,
  443. * starting with zero.
  444. * The 'list' reference must point to a list value.
  445. * The position 'pos' must refer to an existing element, i.e.
  446. * pos < NCDVal_ListCount().
  447. */
  448. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  449. /**
  450. * Returns references to elements within a list by writing them
  451. * via (NCDValRef *) variable arguments.
  452. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  453. * references, as mentioned.
  454. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  455. * references
  456. */
  457. int NCDVal_ListRead (NCDValRef list, int num, ...);
  458. /**
  459. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  460. * elements.
  461. */
  462. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  463. /**
  464. * Determines if a value is a map value.
  465. * The value reference must not be an invalid reference.
  466. */
  467. int NCDVal_IsMap (NCDValRef val);
  468. /**
  469. * Builds a new map value. The 'maxcount' argument specifies how
  470. * many entry slots to preallocate. Not more than that many
  471. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  472. * Returns a reference to the new value, or an invalid reference
  473. * on out of memory.
  474. */
  475. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  476. /**
  477. * Inserts an entry to the map value.
  478. * The 'map' reference must point to a map value, and the
  479. * 'key' and 'val' references must be non-invalid and point to values within
  480. * the same memory object as the map.
  481. * Inserting an entry does not in any way change the 'key'and 'val';
  482. * internally, the map only points to it.
  483. * You must not modify the key after inserting it into a map. This is because
  484. * the map builds an embedded AVL tree of entries indexed by keys.
  485. * If 'key' does not exist in the map, succeeds, returning 1.
  486. * If 'key' already exists in the map, fails, returning 0.
  487. */
  488. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val);
  489. /**
  490. * Returns the number of entries in a map value, i.e. the number
  491. * of times {@link NCDVal_MapInsert} was called successfully.
  492. * The 'map' reference must point to a map value.
  493. */
  494. size_t NCDVal_MapCount (NCDValRef map);
  495. /**
  496. * Returns the maximum number of entries a map value may contain,
  497. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  498. * The 'map' reference must point to a map value.
  499. */
  500. size_t NCDVal_MapMaxCount (NCDValRef map);
  501. /**
  502. * Determines if a map entry reference is invalid. This is used in combination
  503. * with the map iteration functions to detect the end of iteration.
  504. */
  505. int NCDVal_MapElemInvalid (NCDValMapElem me);
  506. /**
  507. * Returns a reference to the first entry in a map, with respect to some
  508. * arbitrary order.
  509. * If the map is empty, returns an invalid map entry reference.
  510. */
  511. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  512. /**
  513. * Returns a reference to the entry in a map that follows the entry referenced
  514. * by 'me', with respect to some arbitrary order.
  515. * The 'me' argument must be a non-invalid reference to an entry in the map.
  516. * If 'me' is the last entry, returns an invalid map entry reference.
  517. */
  518. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  519. /**
  520. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  521. * {@link NCDVal_Compare}.
  522. * Ordered iteration is slower and should only be used when needed.
  523. */
  524. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  525. /**
  526. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  527. * {@link NCDVal_Compare}.
  528. * Ordered iteration is slower and should only be used when needed.
  529. */
  530. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  531. /**
  532. * Returns a reference to the key of the map entry referenced by 'me'.
  533. * The 'me' argument must be a non-invalid reference to an entry in the map.
  534. */
  535. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  536. /**
  537. * Returns a reference to the value of the map entry referenced by 'me'.
  538. * The 'me' argument must be a non-invalid reference to an entry in the map.
  539. */
  540. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  541. /**
  542. * Looks for a key in the map. The 'key' reference must be a non-invalid
  543. * value reference, and may point to a value in a different memory object
  544. * than the map.
  545. * If the key exists in the map, returns a reference to the corresponding
  546. * map entry.
  547. * If the key does not exist, returns an invalid map entry reference.
  548. */
  549. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  550. /**
  551. * Retrieves the value reference to the value of the map entry whose key is a
  552. * string value equal to the given null-terminated string. If there is no such
  553. * entry, returns an invalid value reference.
  554. */
  555. NCDValRef NCDVal_MapGetValue (NCDValRef map, const char *key_str);
  556. /**
  557. * Builds a placeholder replacement program, which is a list of instructions for
  558. * efficiently replacing placeholders in identical values in identical memory
  559. * objects.
  560. * To actually perform replacements, make copies of the memory object of this value
  561. * using {@link NCDValMem_InitCopy}, then call {@link NCDValReplaceProg_Execute}
  562. * on the copies.
  563. * The value passed must be a valid value, and not a placeholder.
  564. * Returns 1 on success, 0 on failure.
  565. */
  566. int NCDValReplaceProg_Init (NCDValReplaceProg *o, NCDValRef val);
  567. /**
  568. * Frees the placeholder replacement program.
  569. */
  570. void NCDValReplaceProg_Free (NCDValReplaceProg *o);
  571. /**
  572. * Callback used by {@link NCDValReplaceProg_Execute} to allow the caller to produce
  573. * values of placeholders.
  574. * This function should build a new value within the memory object 'mem' (which is
  575. * the same as of the memory object where placeholders are being replaced).
  576. * On success, it should return 1, writing a valid value reference to *out.
  577. * On failure, it can either return 0, or return 1 but write an invalid value reference.
  578. * This callback must not access the memory object in any other way than building
  579. * new values in it; it must not modify any values that were already present at the
  580. * point it was called.
  581. */
  582. typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  583. /**
  584. * Executes the replacement program, replacing placeholders in a value.
  585. * The memory object must given be identical to the memory object which was used in
  586. * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_InitCopy}.
  587. * This will call the callback 'replace', which should build the values to replace
  588. * the placeholders.
  589. * Returns 1 on success and 0 on failure. On failure, the entire memory object enters
  590. * and inconsistent state and must be freed using {@link NCDValMem_Free} before
  591. * performing any other operation on it.
  592. * The program is passed by value instead of pointer because this appears to be faster.
  593. * Is is not modified in any way.
  594. */
  595. int NCDValReplaceProg_Execute (NCDValReplaceProg prog, NCDValMem *mem, NCDVal_replace_func replace, void *arg);
  596. #endif