#ifndef _LSUP_TRIPLE_H #define _LSUP_TRIPLE_H #include "term.h" typedef struct LSUP_Triple { LSUP_Term *s; LSUP_Term *p; LSUP_Term *o; } LSUP_Triple; typedef struct LSUP_SerTriple { LSUP_Buffer *s; LSUP_Buffer *p; LSUP_Buffer *o; } LSUP_SerTriple; typedef enum { TRP_POS_S = 0, TRP_POS_P = 1, TRP_POS_O = 2, } LSUP_TriplePos; LSUP_Triple * LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o); #define TRP_DUMMY LSUP_triple_new (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY) LSUP_SerTriple * LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o); #define STRP_DUMMY LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY) LSUP_Triple * LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo); LSUP_SerTriple * LSUP_striple_new_from_triple (const LSUP_Triple *spo); /** @brief Initialize internal term pointers in a heap-allocated triple. * * NOTE: the term structures are not copied. If the triple is freed with * #LSUP_triple_free(), the originally provided terms are freed too. * * @param spo[in] Triple pointer to initialize. */ LSUP_rc LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o); /** @brief Initialize internal term pointers in a heap-allocated buffer triple. * * The triple must be freed with #LSUP_striple_free(). * * @param sspo[in] Serialized triple pointer to initialize. */ LSUP_rc LSUP_striple_init ( LSUP_SerTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o); /** @brief Serialize a RDF triple into a buffer triple. * * The internal structure must be freed with #LSUP_striple_done after use. * * @param spo[in] Triple to be serialized. * @param sspo[out] Buffer triple handle. It must point to an already allocated * structure. * * @return LSUP_OK or an error code resulting from #LSUP_term_serialize. */ LSUP_rc LSUP_triple_serialize (const LSUP_Triple *spo, LSUP_SerTriple *sspo); /** @brief Deserialize a buffer triple into a RDF triple. * * The internal structure must be freed with #LSUP_triple_done after use. * * @param sspo[in] Buffer triple to be serialized. * @param spo[out] RDF triple handle. It must point to an already allocated * structure. * * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize. */ LSUP_rc LSUP_triple_deserialize (const LSUP_SerTriple *sspo, LSUP_Triple *spo); /** @brief Free the internal pointers of a triple. * * @param spo[in] Triple to be freed. */ void LSUP_triple_done (LSUP_Triple *spo); /** @brief Free the internal pointers of a buffer triple. * * @param sspo[in] Buffer triple to be freed. */ void LSUP_striple_done (LSUP_SerTriple *sspo); /** @brief Free a triple and all its internal pointers. * * @param spo[in] Triple to be freed. */ void LSUP_triple_free (LSUP_Triple *spo); /** @brief Free a buffer triple and all its internal pointers. * * @param sspo[in] Buffer triple to be freed. */ void LSUP_striple_free (LSUP_SerTriple *sspo); #define _FN_BODY \ if (n == TRP_POS_S) return trp->s; \ if (n == TRP_POS_P) return trp->p; \ if (n == TRP_POS_O) return trp->o; \ return NULL; /** @brief Get triple by term position. * * Useful for looping over all terms. * * @param trp[in] Triple pointer. * * @param n[in] A number between 0÷2. * * @return Corresponding triple term or NULL if n is out of range. */ inline LSUP_Term * LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n) { _FN_BODY } /** @brief Get serialized triple by term position. * * Useful for looping over all terms. * * @param trp[in] Serialized triple pointer. * * @param n[in] A number between 0÷2. * * @return Corresponding serialized term or NULL if n is out of range. */ inline LSUP_Buffer * LSUP_striple_pos (const LSUP_SerTriple *trp, LSUP_TriplePos n) { _FN_BODY } #undef _FN_BODY #endif