changeset 73:c12589026ff4

RealTime comparator
author Chris Cannam
date Wed, 21 Jan 2015 11:06:03 +0000
parents ca1b533b9480
children 78a4034c3830
files native/PyRealTime.cpp
diffstat 1 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/native/PyRealTime.cpp	Fri Jan 16 08:32:06 2015 +0000
+++ b/native/PyRealTime.cpp	Wed Jan 21 11:06:03 2015 +0000
@@ -185,7 +185,6 @@
 static int
 RealTime_setattr(RealTimeObject *self, char *name, PyObject *value)
 {
-
     if ( !string(name).compare("sec")) { 
         self->rt->sec= (int) PyInt_AS_LONG(value);
         return 0;
@@ -202,7 +201,6 @@
 static PyObject *
 RealTime_getattr(RealTimeObject *self, char *name)
 {
-
     if ( !string(name).compare("sec") ) { 
         return PyInt_FromSsize_t(
             (Py_ssize_t) self->rt->sec); 
@@ -217,6 +215,22 @@
                          (PyObject *)self, name);
 }
 
+static int
+RealTime_compare(PyObject *self, PyObject *other)
+{
+    if (!PyRealTime_Check(self) || !PyRealTime_Check(other)) {
+        PyErr_SetString(PyExc_TypeError, "RealTime Object Expected.");
+        return -1;
+    }
+
+    RealTime *rt1 = PyRealTime_AS_REALTIME(self);
+    RealTime *rt2 = PyRealTime_AS_REALTIME(other);
+
+    if (*rt1 == *rt2) return 0;
+    else if (*rt1 > *rt2) return 1;
+    else return -1;
+}
+
 /* String representation called by e.g. str(realtime), print realtime*/
 static PyObject *
 RealTime_repr(PyObject *self)
@@ -298,7 +312,7 @@
     0,                                  /*tp_print*/
     (getattrfunc)RealTime_getattr, /*tp_getattr*/
     (setattrfunc)RealTime_setattr, /*tp_setattr*/
-    0,                             /*tp_compare*/
+    (cmpfunc)RealTime_compare,     /*tp_compare*/
     RealTime_repr,                 /*tp_repr*/
     &realtime_as_number,        /*tp_as_number*/
     0,                          /*tp_as_sequence*/