py_triple.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. PyObject *result = NULL;
  62. int c = 0;
  63. LSUP_Triple *t1 = ((TripleObject *) obj1)->ob_struct;
  64. LSUP_Triple *t2 = ((TripleObject *) obj2)->ob_struct;
  65. switch (op) {
  66. case Py_LT: result = Py_NotImplemented; break;
  67. case Py_LE: result = Py_NotImplemented; break;
  68. case Py_EQ: c = (
  69. LSUP_term_equals (t1->s, t2->s) &&
  70. LSUP_term_equals (t1->p, t2->p) &&
  71. LSUP_term_equals (t1->o, t2->o)); break;
  72. case Py_NE: c = (!
  73. LSUP_term_equals (t1->s, t2->s) &&
  74. LSUP_term_equals (t1->p, t2->p) &&
  75. LSUP_term_equals (t1->o, t2->o)); break;
  76. case Py_GT: result = Py_NotImplemented; break;
  77. case Py_GE: result = Py_NotImplemented; break;
  78. }
  79. if (!result) result = c ? Py_True : Py_False;
  80. Py_INCREF(result);
  81. return result;
  82. }
  83. PyTypeObject TripleType = {
  84. PyVarObject_HEAD_INIT(NULL, 0)
  85. .tp_name = "triple.Triple",
  86. .tp_doc = "RDF triple.",
  87. .tp_basicsize = sizeof(TripleObject),
  88. .tp_itemsize = 0,
  89. .tp_flags = Py_TPFLAGS_DEFAULT,
  90. .tp_new = PyType_GenericNew,
  91. .tp_init = (initproc) Triple_init,
  92. .tp_dealloc = (destructor) Triple_dealloc,
  93. .tp_members = Triple_members,
  94. .tp_richcompare = Triple_richcmp,
  95. };
  96. #endif