1// PyProperty.cpp --
2// $Id: PyProperty.cpp 1230 2007-03-09 15:58:53Z jcw $
3// This is part of MetaKit, the homepage is http://www.equi4.com/metakit.html
4// Copyright (C) 1999-2004 Gordon McMillan and Jean-Claude Wippler.
5//
6//  Property class implementation
7
8#include "PyProperty.h"
9#include <PWONumber.h>
10#include <PWOSequence.h>
11
12static PyMethodDef PropertyMethods[] =  {
13   {
14    0, 0, 0, 0
15  }
16};
17
18static void PyProperty_dealloc(PyProperty *o) {
19  delete o;
20}
21
22static int PyProperty_print(PyProperty *o, FILE *f, int) {
23  fprintf(f, "Property('%c', '%s')", o->Type(), o->Name());
24  return 0;
25}
26
27static PyObject *PyProperty_getattr(PyProperty *o, char *nm) {
28  try {
29    if (nm[0] == 'n' && strcmp(nm, "name") == 0) {
30      PWOString rslt(o->Name());
31      return rslt.disOwn();
32    }
33    if (nm[0] == 't' && strcmp(nm, "type") == 0) {
34      char s = o->Type();
35      PWOString rslt(&s, 1);
36      return rslt.disOwn();
37    }
38    if (nm[0] == 'i' && strcmp(nm, "id") == 0) {
39      PWONumber rslt(o->GetId());
40      return rslt.disOwn();
41    }
42    return Py_FindMethod(PropertyMethods, o, nm);
43  } catch (...) {
44    return 0;
45  }
46}
47
48static int PyProperty_compare(PyProperty *o, PyObject *ob) {
49  PyProperty *other;
50  int myid, hisid;
51  try {
52    if (!PyProperty_Check(ob))
53      return  - 1;
54    other = (PyProperty*)ob;
55    myid = o->GetId();
56    hisid = other->GetId();
57    if (myid < hisid)
58      return  - 1;
59    else if (myid == hisid)
60      return 0;
61    return 1;
62  } catch (...) {
63    return  - 1;
64  }
65}
66
67PyTypeObject PyPropertytype =  {
68  PyObject_HEAD_INIT(&PyType_Type)0, "PyProperty", sizeof(PyProperty), 0,
69    (destructor)PyProperty_dealloc,  /*tp_dealloc*/
70  (printfunc)PyProperty_print,  /*tp_print*/
71  (getattrfunc)PyProperty_getattr,  /*tp_getattr*/
72  0,  /*tp_setattr*/
73  (cmpfunc)PyProperty_compare,  /*tp_compare*/
74  0,  /*tp_repr*/
75  0,  /*tp_as_number*/
76  0,  /*tp_as_sequence*/
77  0,  /*tp_as_mapping*/
78};
79
80PyObject *PyProperty_new(PyObject *o, PyObject *_args) {
81  try {
82    PWOSequence args(_args);
83    PWOString typ(args[0]);
84    PWOString nam(args[1]);
85    return new PyProperty(*(const char*)typ, nam);
86  } catch (...) {
87    return 0;
88  }
89}
90