triple.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * The triple must be freed with #LSUP_triple_free().
  32. *
  33. * @param spo[in] Triple pointer to initialize.
  34. */
  35. LSUP_rc
  36. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  37. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  38. *
  39. * The triple must be freed with #LSUP_striple_free().
  40. *
  41. * @param sspo[in] Serialized triple pointer to initialize.
  42. */
  43. LSUP_rc
  44. LSUP_striple_init (
  45. LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  46. /** @brief Serialize a RDF triple into a buffer triple.
  47. *
  48. * The internal structure must be freed with #LSUP_striple_done after use.
  49. *
  50. * @param spo[in] Triple to be serialized.
  51. * @param sspo[out] Buffer triple handle. It must point to an already allocated
  52. * structure.
  53. *
  54. * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  55. */
  56. LSUP_rc
  57. LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  58. /** @brief Deserialize a buffer triple into a RDF triple.
  59. *
  60. * The internal structure must be freed with #LSUP_triple_done after use.
  61. *
  62. * @param sspo[in] Buffer triple to be serialized.
  63. * @param spo[out] RDF triple handle. It must point to an already allocated
  64. * structure.
  65. *
  66. * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  67. */
  68. LSUP_rc
  69. LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo);
  70. /** @brief Free the internal pointers of a triple.
  71. *
  72. * @param spo[in] Triple to be freed.
  73. */
  74. void
  75. LSUP_triple_done (LSUP_Triple *spo);
  76. /** @brief Free the internal pointers of a buffer triple.
  77. *
  78. * @param sspo[in] Buffer triple to be freed.
  79. */
  80. void
  81. LSUP_striple_done (LSUP_SerTriple *sspo);
  82. /** @brief Free a triple and all its internal pointers.
  83. *
  84. * @param spo[in] Triple to be freed.
  85. */
  86. void
  87. LSUP_triple_free (LSUP_Triple *spo);
  88. /** @brief Free a buffer triple and all its internal pointers.
  89. *
  90. * @param sspo[in] Buffer triple to be freed.
  91. */
  92. void
  93. LSUP_striple_free (LSUP_SerTriple *sspo);
  94. #define _FN_BODY \
  95. if (n == TRP_POS_S) return trp->s; \
  96. if (n == TRP_POS_P) return trp->p; \
  97. if (n == TRP_POS_O) return trp->o; \
  98. return NULL;
  99. /** @brief Get triple by term position.
  100. *
  101. * Useful for looping over all terms.
  102. *
  103. * @param trp[in] Triple pointer.
  104. *
  105. * @param n[in] A number between 0÷2.
  106. *
  107. * @return Corresponding triple term or NULL if n is out of range.
  108. */
  109. inline LSUP_Term *
  110. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  111. { _FN_BODY }
  112. /** @brief Get serialized triple by term position.
  113. *
  114. * Useful for looping over all terms.
  115. *
  116. * @param trp[in] Serialized triple pointer.
  117. *
  118. * @param n[in] A number between 0÷2.
  119. *
  120. * @return Corresponding serialized term or NULL if n is out of range.
  121. */
  122. inline LSUP_Buffer *
  123. LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n)
  124. { _FN_BODY }
  125. #undef _FN_BODY
  126. #endif