py_triple.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _PY_TRIPLE_OBJ_H
  2. #define _PY_TRIPLE_OBJ_H
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <structmember.h>
  6. #include "triple.h"
  7. #include "py_term.h"
  8. typedef struct {
  9. PyObject_HEAD
  10. LSUP_Triple * ob_struct;
  11. PyObject *s;
  12. PyObject *p;
  13. PyObject *o;
  14. } TripleObject;
  15. static int
  16. Triple_init (TripleObject *self, PyObject *args)
  17. {
  18. PyObject *s = NULL, *p = NULL, *o = NULL, *tmp;
  19. if (! PyArg_ParseTuple (args, "OOO", &s, &p, &o)) return -1;
  20. // TODO check for null s, p, o
  21. tmp = self->s;
  22. Py_INCREF(s);
  23. self->s = s;
  24. Py_XDECREF(tmp);
  25. tmp = self->p;
  26. Py_INCREF(p);
  27. self->p = p;
  28. Py_XDECREF(tmp);
  29. tmp = self->o;
  30. Py_INCREF(o);
  31. self->o = o;
  32. Py_XDECREF(tmp);
  33. self->ob_struct = LSUP_triple_new (
  34. ((TermObject *) self->s)->ob_struct,
  35. ((TermObject *) self->p)->ob_struct,
  36. ((TermObject *) self->o)->ob_struct);
  37. if (!self->ob_struct) {
  38. PyErr_SetString (PyExc_ValueError, "Could not create triple.");
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. static void
  44. Triple_dealloc (TripleObject *self)
  45. {
  46. Py_XDECREF(self->s);
  47. Py_XDECREF(self->p);
  48. Py_XDECREF(self->o);
  49. free (self->ob_struct); // Term pointers were allocated independently.
  50. Py_TYPE (self)->tp_free ((PyObject *) self);
  51. }
  52. static PyMemberDef Triple_members[] = {
  53. {"s", T_OBJECT_EX, offsetof(TripleObject, s), 0, "Triple subject."},
  54. {"p", T_OBJECT_EX, offsetof(TripleObject, p), 0, "Triple predicate."},
  55. {"o", T_OBJECT_EX, offsetof(TripleObject, o), 0, "Triple object."},
  56. {NULL}
  57. };
  58. static PyObject *
  59. Triple_richcmp (PyObject *obj1, PyObject *obj2, int op)
  60. {
  61. // Only equality and non-equality supported.
  62. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  63. LSUP_Triple *t1 = ((TripleObject *) obj1)->ob_struct;
  64. LSUP_Triple *t2 = ((TripleObject *) obj2)->ob_struct;
  65. if (
  66. (
  67. LSUP_term_equals (t1->s, t2->s) &&
  68. LSUP_term_equals (t1->p, t2->p) &&
  69. LSUP_term_equals (t1->o, t2->o)
  70. ) ^ (op == Py_NE)
  71. ) Py_RETURN_TRUE;
  72. Py_RETURN_FALSE;
  73. }
  74. static Py_hash_t
  75. Triple_hash (PyObject *self)
  76. { return LSUP_triple_hash (((TripleObject *)self)->ob_struct); }
  77. PyTypeObject TripleType = {
  78. PyVarObject_HEAD_INIT(NULL, 0)
  79. .tp_name = "triple.Triple",
  80. .tp_doc = "RDF triple.",
  81. .tp_basicsize = sizeof(TripleObject),
  82. .tp_itemsize = 0,
  83. .tp_flags = Py_TPFLAGS_DEFAULT,
  84. .tp_new = PyType_GenericNew,
  85. .tp_init = (initproc) Triple_init,
  86. .tp_dealloc = (destructor) Triple_dealloc,
  87. .tp_members = Triple_members,
  88. .tp_richcompare = Triple_richcmp,
  89. .tp_hash = Triple_hash,
  90. };
  91. /** @brief Build a triple object from a LSUP_Triple struct.
  92. */
  93. PyObject *
  94. build_triple (LSUP_Triple *spo)
  95. {
  96. PyObject *s_obj, *p_obj, *o_obj, *spo_obj, *term_args, *trp_args;
  97. term_args = Py_BuildValue (
  98. "bszz", spo->s->type, spo->s->data,
  99. spo->s->datatype, spo->s->lang);
  100. s_obj = PyObject_CallObject ((PyObject *)&TermType, term_args);
  101. Py_DECREF (term_args);
  102. if (UNLIKELY (!s_obj)) return NULL;
  103. term_args = Py_BuildValue (
  104. "bszz", spo->p->type, spo->p->data,
  105. spo->p->datatype, spo->p->lang);
  106. p_obj = PyObject_CallObject ((PyObject *)&TermType, term_args);
  107. if (UNLIKELY (!p_obj)) return NULL;
  108. Py_DECREF (term_args);
  109. term_args = Py_BuildValue (
  110. "bszz", spo->o->type, spo->o->data,
  111. spo->o->datatype, spo->o->lang);
  112. o_obj = PyObject_CallObject ((PyObject *)&TermType, term_args);
  113. if (UNLIKELY (!o_obj)) return NULL;
  114. Py_DECREF (term_args);
  115. trp_args = Py_BuildValue ("OOO", s_obj, p_obj, o_obj);
  116. spo_obj = PyObject_CallObject ((PyObject *)&TripleType, trp_args);
  117. if (UNLIKELY (!spo_obj)) return NULL;
  118. Py_DECREF (trp_args);
  119. return spo_obj;
  120. }
  121. #endif