pointer_manipulation.i 3.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*!
 * Copyright (c) 2018 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
/*!
 * This SWIG interface extension provides support to
 * the pointer manipulation methods present in the standard
 * SWIG wrappers, but with support for larger arrays.
 * 
 * SWIG provides this in https://github.com/swig/swig/blob/master/Lib/carrays.i
 * but the standard methods only provide arrays with up to
 * max(int32_t) elements.
 * 
 * The `long_array_functions` wrappers extend this
 * to arrays of size max(int64_t) instead of max(int32_t).
 */

Scott Votaw's avatar
Scott Votaw committed
18
%pointer_functions(uint8_t, bytep)
19
20
21
22
23
24
%pointer_functions(int, intp)
%pointer_functions(long, longp)
%pointer_functions(double, doublep)
%pointer_functions(float, floatp)
%pointer_functions(int64_t, int64_tp)
%pointer_functions(int32_t, int32_tp)
25
%pointer_functions(size_t, size_tp)
26
27
28
29
30
31
32
33
34
35
36

%pointer_cast(int64_t *, long *, int64_t_to_long_ptr)
%pointer_cast(int64_t *, double *, int64_t_to_double_ptr)
%pointer_cast(int32_t *, int *, int32_t_to_int_ptr)
%pointer_cast(long *, int64_t *, long_to_int64_t_ptr)
%pointer_cast(double *, int64_t *, double_to_int64_t_ptr)
%pointer_cast(int *, int32_t *, int_to_int32_t_ptr)

%pointer_cast(double *, void *, double_to_voidp_ptr)
%pointer_cast(float *, void *, float_to_voidp_ptr)
%pointer_cast(int *, void *, int_to_voidp_ptr)
Scott Votaw's avatar
Scott Votaw committed
37
%pointer_cast(uint8_t *, void *, byte_to_voidp_ptr)
38
39
40
%pointer_cast(int32_t *, void *, int32_t_to_voidp_ptr)
%pointer_cast(int64_t *, void *, int64_t_to_voidp_ptr)

41
42
%pointer_cast(void *, double **, void_to_doublep_ptr)

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Custom pointer manipulation template */
%define %pointer_manipulation(TYPE, NAME)
%{
  static TYPE *new_##NAME() { %}
  %{  TYPE* NAME = new TYPE; return NAME; %}
  %{}

  static void delete_##NAME(TYPE *self) { %}
  %{  if (self) delete self; %}
  %{}
  %}

TYPE *new_##NAME();
void  delete_##NAME(TYPE *self);

%enddef

%define %pointer_dereference(TYPE, NAME)
%{
  static TYPE NAME ##_value(TYPE *self) {
    TYPE NAME = *self;
    return NAME;
  }
%}

TYPE NAME##_value(TYPE *self);

%enddef

%define %pointer_handle(TYPE, NAME)
%{
  static TYPE* NAME ##_handle() { %}
  %{ TYPE* NAME = new TYPE; *NAME = (TYPE)operator new(sizeof(int*)); return NAME; %}
  %{}
%}

TYPE *NAME##_handle();

%enddef

%define %long_array_functions(TYPE,NAME)
%{
  static TYPE *new_##NAME(int64_t nelements) { %}
  %{  return new TYPE[nelements](); %}
  %{}

  static void delete_##NAME(TYPE *ary) { %}
  %{  delete [] ary; %}
  %{}

  static TYPE NAME##_getitem(TYPE *ary, int64_t index) {
    return ary[index];
  }
  static void NAME##_setitem(TYPE *ary, int64_t index, TYPE value) {
    ary[index] = value;
  }
  %}

TYPE *new_##NAME(int64_t nelements);
void delete_##NAME(TYPE *ary);
TYPE NAME##_getitem(TYPE *ary, int64_t index);
void NAME##_setitem(TYPE *ary, int64_t index, TYPE value);

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%enddef 

/* Custom template for arrays of pointers */
%define %ptr_array_functions(TYPE,NAME)
%{
  static TYPE **new_##NAME(int64_t nelements) { %}
  %{  return new TYPE*[nelements](); %}
  %{}

  static void delete_##NAME(TYPE **ary) { %}
  %{  delete [] ary; %}
  %{}

  static TYPE *NAME##_getitem(TYPE **ary, int64_t index) {
    return ary[index];
  }
  static void NAME##_setitem(TYPE **ary, int64_t index, TYPE *value) {
    ary[index] = value;
  }
  %}

TYPE **new_##NAME(int64_t nelements);
void delete_##NAME(TYPE **ary);
TYPE *NAME##_getitem(TYPE **ary, int64_t index);
void NAME##_setitem(TYPE **ary, int64_t index, TYPE *value);

132
133
%enddef

134
%long_array_functions(uint8_t, byteArray)
135
136
%long_array_functions(double, doubleArray)
%long_array_functions(float, floatArray)
137
138
139
140
141
142
%long_array_functions(int32_t, intArray)
%long_array_functions(int64_t, longArray)

%ptr_array_functions(void, voidPtrArray)
%ptr_array_functions(double, doublePtrArray)
%ptr_array_functions(int, intPtrArray)
143
144
145
146
147
148
149
150

%pointer_manipulation(void*, voidpp)

/* Allow dereferencing of void** to void* */
%pointer_dereference(void*, voidpp)

/* Allow retrieving handle to void** */
%pointer_handle(void*, voidpp)