triple.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef _LSUP_TRIPLE_H
  2. #define _LSUP_TRIPLE_H
  3. #include "term.h"
  4. typedef struct LSUP_Triple {
  5. LSUP_Term *s;
  6. LSUP_Term *p;
  7. LSUP_Term *o;
  8. } LSUP_Triple;
  9. typedef struct LSUP_SerTriple {
  10. LSUP_Buffer *s;
  11. LSUP_Buffer *p;
  12. LSUP_Buffer *o;
  13. } LSUP_SerTriple;
  14. typedef enum {
  15. TRP_POS_S = 0,
  16. TRP_POS_P = 1,
  17. TRP_POS_O = 2,
  18. } LSUP_TriplePos;
  19. LSUP_Triple *
  20. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  21. #define TRP_DUMMY LSUP_triple_new (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY)
  22. LSUP_SerTriple *
  23. LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  24. #define STRP_DUMMY LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY)
  25. LSUP_Triple *
  26. LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo);
  27. LSUP_SerTriple *
  28. LSUP_striple_new_from_triple (const LSUP_Triple *spo);
  29. /** @brief Initialize internal term pointers in a heap-allocated triple.
  30. *
  31. * NOTE: the term structures are not copied. If the triple is freed with
  32. * #LSUP_triple_free(), the originally provided terms are freed too.
  33. *
  34. * @param spo[in] Triple pointer to initialize.
  35. */
  36. LSUP_rc
  37. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  38. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  39. *
  40. * The triple must be freed with #LSUP_striple_free().
  41. *
  42. * @param sspo[in] Serialized triple pointer to initialize.
  43. */
  44. LSUP_rc
  45. LSUP_striple_init (
  46. LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  47. /** @brief Serialize a RDF triple into a buffer triple.
  48. *
  49. * The internal structure must be freed with #LSUP_striple_done after use.
  50. *
  51. * @param spo[in] Triple to be serialized.
  52. * @param sspo[out] Buffer triple handle. It must point to an already allocated
  53. * structure.
  54. *
  55. * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  56. */
  57. LSUP_rc
  58. LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  59. /** @brief Deserialize a buffer triple into a RDF triple.
  60. *
  61. * The internal structure must be freed with #LSUP_triple_done after use.
  62. *
  63. * @param sspo[in] Buffer triple to be serialized.
  64. * @param spo[out] RDF triple handle. It must point to an already allocated
  65. * structure.
  66. *
  67. * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  68. */
  69. LSUP_rc
  70. LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo);
  71. /** @brief Free the internal pointers of a triple.
  72. *
  73. * @param spo[in] Triple to be freed.
  74. */
  75. void
  76. LSUP_triple_done (LSUP_Triple *spo);
  77. /** @brief Free the internal pointers of a buffer triple.
  78. *
  79. * @param sspo[in] Buffer triple to be freed.
  80. */
  81. void
  82. LSUP_striple_done (LSUP_SerTriple *sspo);
  83. /** @brief Free a triple and all its internal pointers.
  84. *
  85. * @param spo[in] Triple to be freed.
  86. */
  87. void
  88. LSUP_triple_free (LSUP_Triple *spo);
  89. /** @brief Free a buffer triple and all its internal pointers.
  90. *
  91. * @param sspo[in] Buffer triple to be freed.
  92. */
  93. void
  94. LSUP_striple_free (LSUP_SerTriple *sspo);
  95. #define _FN_BODY \
  96. if (n == TRP_POS_S) return trp->s; \
  97. if (n == TRP_POS_P) return trp->p; \
  98. if (n == TRP_POS_O) return trp->o; \
  99. return NULL;
  100. /** @brief Get triple by term position.
  101. *
  102. * Useful for looping over all terms.
  103. *
  104. * @param trp[in] Triple pointer.
  105. *
  106. * @param n[in] A number between 0÷2.
  107. *
  108. * @return Corresponding triple term or NULL if n is out of range.
  109. */
  110. inline LSUP_Term *
  111. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  112. { _FN_BODY }
  113. /** @brief Get serialized triple by term position.
  114. *
  115. * Useful for looping over all terms.
  116. *
  117. * @param trp[in] Serialized triple pointer.
  118. *
  119. * @param n[in] A number between 0÷2.
  120. *
  121. * @return Corresponding serialized term or NULL if n is out of range.
  122. */
  123. inline LSUP_Buffer *
  124. LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n)
  125. { _FN_BODY }
  126. #undef _FN_BODY
  127. #endif