triple.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /** @brief Serialize a RDF triple into a buffer triple.
  20. *
  21. * The internal structure must be freed with #LSUP_striple_done after use.
  22. *
  23. * @param spo[in] Triple to be serialized.
  24. * @param sspo[out] Buffer triple handle. It must point to an already allocated
  25. * structure.
  26. *
  27. * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  28. */
  29. LSUP_rc
  30. LSUP_triple_serialize(const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  31. /** @brief Deserialize a buffer triple into a RDF triple.
  32. *
  33. * The internal structure must be freed with #LSUP_triple_done after use.
  34. *
  35. * @param sspo[in] Buffer triple to be serialized.
  36. * @param spo[out] RDF triple handle. It must point to an already allocated
  37. * structure.
  38. *
  39. * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  40. */
  41. LSUP_rc
  42. LSUP_triple_deserialize(const LSUP_SerTriple *sspo, LSUP_Triple *spo);
  43. /** @brief Free the internal pointers of a triple.
  44. *
  45. * The striple structure itself is not freed, so it can be used with a stack-
  46. * allocated structure.
  47. *
  48. * @param spo[in] Triple to be freed.
  49. */
  50. void
  51. LSUP_triple_done(LSUP_Triple *spo);
  52. /** @brief Free the internal pointers of a buffer triple.
  53. *
  54. * The triple structure itself is not freed, so it can be used with a stack-
  55. * allocated structure.
  56. *
  57. * @param sspo[in] Buffer triple to be freed.
  58. */
  59. void
  60. LSUP_striple_done(LSUP_SerTriple *sspo);
  61. #define _FN_BODY \
  62. if (n == TRP_POS_S) return trp->s; \
  63. if (n == TRP_POS_P) return trp->p; \
  64. if (n == TRP_POS_O) return trp->o; \
  65. return NULL;
  66. /** @brief Get triple by term position.
  67. *
  68. * Useful for looping over all terms.
  69. *
  70. * @param trp[in] Triple pointer.
  71. *
  72. * @param n[in] A number between 0÷2.
  73. *
  74. * @return Corresponding triple term or NULL if n is out of range.
  75. */
  76. inline LSUP_Term *
  77. LSUP_triple_pos(const LSUP_Triple *trp, LSUP_TriplePos n)
  78. { _FN_BODY }
  79. /** @brief Get serialized triple by term position.
  80. *
  81. * Useful for looping over all terms.
  82. *
  83. * @param trp[in] Serialized triple pointer.
  84. *
  85. * @param n[in] A number between 0÷2.
  86. *
  87. * @return Corresponding serialized term or NULL if n is out of range.
  88. */
  89. inline LSUP_Buffer *
  90. LSUP_striple_pos(const LSUP_SerTriple *trp, LSUP_TriplePos n)
  91. { _FN_BODY }
  92. #undef _FN_BODY
  93. // TODO Add constructors and destructors with term type checks.
  94. #endif