home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Languages / python / PyObjC-0.47-MIHS / pyobjc-0.47-src / Modules / ObjCPointer.m < prev    next >
Encoding:
Text File  |  1996-11-13  |  3.8 KB  |  158 lines

  1. /* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
  2.  *
  3.  * This software may be used and distributed freely for any purpose
  4.  * provided that this notice is included unchanged on any and all
  5.  * copies. The author does not warrant or guarantee this software in
  6.  * any way.
  7.  *
  8.  * This file is part of the PyObjC package.
  9.  *
  10.  * $RCSfile: ObjCPointer.m,v $
  11.  * $Revision: 1.1.1.1 $
  12.  * $Date: 1996/11/14 01:50:23 $
  13.  *
  14.  * Created Mon Oct 28 12:38:18 1996.
  15.  */
  16.  
  17. #include "ObjC.h"
  18. #include "objc_support.h"
  19.  
  20. static void
  21. ObjCPointer_dealloc (ObjCPointer *self)
  22. {
  23.   Py_DECREF (self->type);
  24.   PyMem_DEL (self);
  25. }
  26.  
  27. static char ObjCPointer_unpack_doc[] =
  28. FUNDOC("Unpack the pointed value accordingly to its type.",
  29.        ".unpack()",
  30.        "none",
  31.        "A Python object containing the unpacked value");
  32. static PyObject *
  33. ObjCPointer_unpack (ObjCPointer *self, PyObject *args)
  34. {
  35.   if (PyArg_ParseTuple (args, ""))
  36.     {
  37.       if (self->ptr)
  38.     {
  39.       const char *type = PyString_AS_STRING (self->type);
  40.  
  41.       if (!strcmp (type, @encode (void)))
  42.         {
  43.           PyErr_SetString (ObjC_Error, "Cannot dereference a pointer to void");
  44.           return NULL;
  45.         }
  46.       else
  47.         return pythonify_c_value (PyString_AS_STRING (self->type),
  48.                       self->ptr, NULL);
  49.     }
  50.       else
  51.     {
  52.       Py_INCREF (Py_None);
  53.       return Py_None;
  54.     }
  55.     }
  56.         
  57.   return NULL;
  58. }
  59.  
  60. static PyMethodDef ObjCPointer_methods[] =
  61. {
  62.   { "unpack",    (PyCFunction) ObjCPointer_unpack,    METH_VARARGS,    ObjCPointer_unpack_doc },
  63.   { 0, 0, 0, 0 }
  64. };
  65.  
  66. static PyObject *
  67. ObjCPointer_getattr (ObjCPointer *self, char *name)
  68. {
  69.   PyObject *method;
  70.  
  71.   method = Py_FindMethod (ObjCPointer_methods, (PyObject *) self, name);
  72.   if (method)
  73.     return method;
  74.   else
  75.     {
  76.       PyErr_Clear();
  77.  
  78.       if (!strcmp (name, "type"))
  79.     {
  80.       Py_INCREF (self->type);
  81.       return (PyObject *) self->type;
  82.     }
  83.       else if (!strcmp (name, "pointerAsInteger"))
  84.     return PyInt_FromLong ((long) self->ptr);
  85.       else if (!strcmp (name, "__members__"))
  86.     {
  87.       const char *members[] = { "type", "pointerAsInteger" };
  88.       PyObject *list;
  89.       unsigned int idx;
  90.       
  91.       idx = sizeof (members) / sizeof (members[0]);
  92.       list = PyList_New (idx);
  93.       while (idx--)
  94.         PyList_SetItem (list, idx, PyString_FromString ((char *) members[idx]));
  95.       return list;
  96.     }      
  97.     }
  98.  
  99.   PyErr_SetString (PyExc_AttributeError, name);
  100.   return method;
  101. }
  102.     
  103. PyTypeObject ObjCPointer_Type =
  104. {
  105.   PyObject_HEAD_INIT(&PyType_Type)
  106.   0,                          /*ob_size*/
  107.   "ObjCPointer",                  /*tp_name*/
  108.   sizeof (ObjCPointer),                  /*tp_basicsize*/
  109.   sizeof (char),                  /*tp_itemsize*/
  110.   
  111.   /* methods */
  112.   (destructor) ObjCPointer_dealloc,          /*tp_dealloc*/
  113.   (printfunc) 0,                  /*tp_print*/
  114.   (getattrfunc) ObjCPointer_getattr,          /*tp_getattr*/
  115.   (setattrfunc) 0,                  /*tp_setattr*/
  116.   (cmpfunc) 0,                      /*tp_compare*/
  117.   (reprfunc) 0,                      /*tp_repr*/
  118.   0,                          /*tp_as_number*/
  119.   0,                          /*tp_as_sequence*/
  120.   0,                          /*tp_as_mapping*/
  121.   (hashfunc) 0,                      /*tp_hash*/
  122.   (ternaryfunc) 0,                  /*tp_call*/
  123.   (reprfunc) 0,                      /*tp_str*/
  124.   (getattrofunc) 0,                        /*tp_getattro*/
  125.   (setattrofunc) 0,                  /*tp_setattro*/
  126.  
  127.   /* Space for future expansion */
  128.   0L,0L,
  129.   
  130.   "Wrapper around a Objective-C Pointer"      /* Documentation string */
  131. };
  132.  
  133. ObjCPointer *
  134. ObjCPointer_new (void *p, const char *t)
  135. {
  136.   unsigned int size = objc_sizeof_type (t);
  137.   const char *typeend = objc_skip_typespec (t);
  138.   ObjCPointer *self = PyObject_NEW_VAR (ObjCPointer, &ObjCPointer_Type, size);
  139.  
  140.   if (self == NULL)
  141.     return NULL;
  142.  
  143.   self->type = (PyStringObject *) PyString_FromStringAndSize ((char *) t, typeend-t);
  144.  
  145.   if (size && p)
  146.     memcpy ((self->ptr = self->contents), p, size);
  147.   else
  148.     self->ptr = p;
  149.   
  150.   return self;
  151. }
  152.  
  153. /*
  154. ** Local Variables:
  155. ** change-log-default-name:"../ChangeLog.PyObjC"
  156. ** End:
  157. */
  158.