triple.h 3.5 KB

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