PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/d/carrays.i

#
Swig | 111 lines | 64 code | 14 blank | 33 comment | 0 complexity | 9b16967602610b470b378c5dae5376b4 MD5 | raw file
  1/* -----------------------------------------------------------------------------
  2 * carrays.i
  3 *
  4 * D-specific version of ../carrays.i.
  5 * ----------------------------------------------------------------------------- */
  6
  7/* -----------------------------------------------------------------------------
  8 * %array_functions(TYPE,NAME)
  9 *
 10 * Generates functions for creating and accessing elements of a C array
 11 * (as pointers).  Creates the following functions:
 12 *
 13 *        TYPE *new_NAME(int nelements)
 14 *        void delete_NAME(TYPE *);
 15 *        TYPE NAME_getitem(TYPE *, int index);
 16 *        void NAME_setitem(TYPE *, int index, TYPE value);
 17 *
 18 * ----------------------------------------------------------------------------- */
 19
 20%define %array_functions(TYPE,NAME)
 21%{
 22static TYPE *new_##NAME(int nelements) { %}
 23#ifdef __cplusplus
 24%{  return new TYPE[nelements]; %}
 25#else
 26%{  return (TYPE *) calloc(nelements,sizeof(TYPE)); %}
 27#endif
 28%{}
 29
 30static void delete_##NAME(TYPE *ary) { %}
 31#ifdef __cplusplus
 32%{  delete [] ary; %}
 33#else
 34%{  free(ary); %}
 35#endif
 36%{}
 37
 38static TYPE NAME##_getitem(TYPE *ary, int index) {
 39    return ary[index];
 40}
 41static void NAME##_setitem(TYPE *ary, int index, TYPE value) {
 42    ary[index] = value;
 43}
 44%}
 45
 46TYPE *new_##NAME(int nelements);
 47void delete_##NAME(TYPE *ary);
 48TYPE NAME##_getitem(TYPE *ary, int index);
 49void NAME##_setitem(TYPE *ary, int index, TYPE value);
 50
 51%enddef
 52
 53
 54/* -----------------------------------------------------------------------------
 55 * %array_class(TYPE,NAME)
 56 *
 57 * Generates a class wrapper around a C array.  The class has the following
 58 * interface:
 59 *
 60 *          struct NAME {
 61 *              NAME(int nelements);
 62 *             ~NAME();
 63 *              TYPE getitem(int index);
 64 *              void setitem(int index, TYPE value);
 65 *              TYPE * ptr();
 66 *              static NAME *frompointer(TYPE *t);
 67  *         }
 68 *
 69 * ----------------------------------------------------------------------------- */
 70
 71%define %array_class(TYPE,NAME)
 72%{
 73typedef TYPE NAME;
 74%}
 75
 76typedef struct {} NAME;
 77
 78%extend NAME {
 79#ifdef __cplusplus
 80  NAME(int nelements) {
 81    return new TYPE[nelements];
 82  }
 83  ~NAME() {
 84    delete [] self;
 85  }
 86#else
 87  NAME(int nelements) {
 88    return (TYPE *) calloc(nelements,sizeof(TYPE));
 89  }
 90  ~NAME() {
 91    free(self);
 92  }
 93#endif
 94
 95  TYPE getitem(int index) {
 96    return self[index];
 97  }
 98  void setitem(int index, TYPE value) {
 99    self[index] = value;
100  }
101  TYPE * ptr() {
102    return self;
103  }
104  static NAME *frompointer(TYPE *t) {
105    return (NAME *) t;
106  }
107};
108
109%types(NAME = TYPE);
110
111%enddef