File: cpoint_temp.h

package info (click to toggle)
gambas3 3.20.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,208 kB
  • sloc: ansic: 197,232; cpp: 124,273; sh: 18,999; javascript: 7,761; sql: 5,399; makefile: 2,358; perl: 1,397; xml: 490; python: 335
file content (262 lines) | stat: -rw-r--r-- 31,397 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/***************************************************************************

  cpoint_temp.h

  (c) 2000-2017 BenoƮt Minisini <benoit.minisini@gambas-basic.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.

***************************************************************************/

#define IMPLEMENT_POINT_CLASS(__struct, __name, __gtype, __ctype, __sign, __return, __this, __rstruct, __rname)               \
                                                                                                                              \
__struct * __struct##_create(__ctype x, __ctype y)                                                                            \
{                                                                                                                             \
  __struct *p = GB.New(GB.FindClass(#__name), NULL, NULL);                                                                    \
  p->x = x;                                                                                                                   \
  p->y = y;                                                                                                                   \
  return p;                                                                                                                   \
}                                                                                                                             \
                                                                                                                              \
static inline __struct *__struct##_make(__struct *a, const __ctype x, const __ctype y)                                        \
{                                                                                                                             \
  if (a->ob.ref <= 1)                                                                                                         \
  {                                                                                                                           \
    a->x = x;                                                                                                                 \
    a->y = y;                                                                                                                 \
    return a;                                                                                                                 \
  }                                                                                                                           \
  else                                                                                                                        \
    return __struct##_create(x, y);                                                                                           \
}                                                                                                                             \
                                                                                                                              \
static __struct *_add_##__name(__struct *a, __struct *b, bool invert)                                                         \
{                                                                                                                             \
  return __struct##_make(a, a->x + b->x, a->y + b->y);                                                                        \
}                                                                                                                             \
                                                                                                                              \
static __struct *_sub_##__name(__struct *a, __struct *b, bool invert)                                                         \
{                                                                                                                             \
  return __struct##_make(a, a->x - b->x, a->y - b->y);                                                                        \
}                                                                                                                             \
                                                                                                                              \
static __struct *_mulf_##__name(__struct *a, double f, bool invert)                                                           \
{                                                                                                                             \
  return __struct##_make(a, a->x * f, a->y * f);                                                                              \
}                                                                                                                             \
                                                                                                                              \
static __struct *_mulo_##__name(__struct *a, void *b, bool invert)                                                            \
{                                                                                                                             \
  return NULL;                                                                                                                \
}                                                                                                                             \
                                                                                                                              \
static __struct *_divf_##__name(__struct *a, double f, bool invert)                                                           \
{                                                                                                                             \
  if (invert)                                                                                                                 \
    return NULL;                                                                                                              \
  if (f == 0.0)                                                                                                               \
    return NULL;                                                                                                              \
                                                                                                                              \
  return __struct##_make(a, a->x / f, a->y / f);                                                                              \
}                                                                                                                             \
                                                                                                                              \
static __struct *_divo_##__name(__struct *a, void *b, bool invert)                                                            \
{                                                                                                                             \
  return NULL;                                                                                                                \
}                                                                                                                             \
                                                                                                                              \
static int _equal_##__name(__struct *a, __struct *b, bool invert)                                                             \
{                                                                                                                             \
  return a->x == b->x && a->y == b->y;                                                                                        \
}                                                                                                                             \
                                                                                                                              \
static double _fabs_##__name(__struct *a)                                                                                     \
{                                                                                                                             \
  return hypot(a->x, a->y);                                                                                                   \
}                                                                                                                             \
                                                                                                                              \
static __struct *_neg_##__name(__struct *a)                                                                                   \
{                                                                                                                             \
  return __struct##_make(a, -a->x, -a->y);                                                                                    \
}                                                                                                                             \
                                                                                                                              \
static GB_OPERATOR_DESC _operator_##__name =                                                                                  \
{                                                                                                                             \
  .equal   = (void *)_equal_##__name,                                                                                         \
  .add     = (void *)_add_##__name,                                                                                           \
  .sub     = (void *)_sub_##__name,                                                                                           \
  .mulf    = (void *)_mulf_##__name,                                                                                          \
  .mulo    = (void *)_mulo_##__name,                                                                                          \
  .divf    = (void *)_divf_##__name,                                                                                          \
  .divo    = (void *)_divo_##__name,                                                                                          \
  .fabs     = (void *)_fabs_##__name,                                                                                         \
  .neg     = (void *)_neg_##__name,                                                                                           \
};                                                                                                                            \
                                                                                                                              \
char *__struct##_to_string(void *a, bool local)                                                                               \
{                                                                                                                             \
  char *result = NULL;                                                                                                        \
  char *str;                                                                                                                  \
  int len;                                                                                                                    \
                                                                                                                              \
  __ctype x = ((__struct *)a)->x;                                                                                             \
  __ctype y = ((__struct *)a)->y;                                                                                             \
                                                                                                                              \
  result = GB.AddChar(result, '[');                                                                                           \
                                                                                                                              \
  GB.NumberToString(local, x, NULL, &str, &len);                                                                              \
  result = GB.AddString(result, str, len);                                                                                    \
                                                                                                                              \
  result = GB.AddChar(result, local ? ' ' : ',');                                                                             \
                                                                                                                              \
  GB.NumberToString(local, y, NULL, &str, &len);                                                                              \
  result = GB.AddString(result, str, len);                                                                                    \
                                                                                                                              \
  result = GB.AddChar(result, ']');                                                                                           \
                                                                                                                              \
  return result;                                                                                                              \
}                                                                                                                             \
                                                                                                                              \
static bool _convert_##__name(void *a, GB_TYPE type, GB_VALUE *conv)                                                          \
{                                                                                                                             \
  if (a)                                                                                                                      \
  {                                                                                                                           \
    double norm = _fabs_##__name(a);                                                                                          \
                                                                                                                              \
    switch (type)                                                                                                             \
    {                                                                                                                         \
      case GB_T_FLOAT:                                                                                                        \
        conv->_float.value = norm;                                                                                            \
        return FALSE;                                                                                                         \
                                                                                                                              \
      case GB_T_SINGLE:                                                                                                       \
        conv->_single.value = norm;                                                                                           \
        return FALSE;                                                                                                         \
                                                                                                                              \
      case GB_T_INTEGER:                                                                                                      \
      case GB_T_SHORT:                                                                                                        \
      case GB_T_BYTE:                                                                                                         \
        conv->_integer.value = norm;                                                                                          \
        return FALSE;                                                                                                         \
                                                                                                                              \
      case GB_T_LONG:                                                                                                         \
        conv->_long.value = norm;                                                                                             \
        return FALSE;                                                                                                         \
                                                                                                                              \
      case GB_T_STRING:                                                                                                       \
      case GB_T_CSTRING:                                                                                                      \
        conv->_string.value.addr = __struct##_to_string(a, type == GB_T_CSTRING);                                             \
        conv->_string.value.start = 0;                                                                                        \
        conv->_string.value.len = GB.StringLength(conv->_string.value.addr);                                                  \
        return FALSE;                                                                                                         \
                                                                                                                              \
      default:                                                                                                                \
        if (type == GB.FindClass("Point"))                                                                                    \
        {                                                                                                                     \
          conv->_object.value = CPOINT_create(((__struct *)a)->x, ((__struct *)a)->y);                                        \
          return FALSE;                                                                                                       \
        }                                                                                                                     \
        if (type == GB.FindClass("PointF"))                                                                                   \
        {                                                                                                                     \
          conv->_object.value = CPOINTF_create(((__struct *)a)->x, ((__struct *)a)->y);                                       \
          return FALSE;                                                                                                       \
        }                                                                                                                     \
        else                                                                                                                  \
          return TRUE;                                                                                                        \
    }                                                                                                                         \
  }                                                                                                                           \
  else                                                                                                                        \
    return TRUE;                                                                                                              \
}                                                                                                                             \
                                                                                                                              \
                                                                                                                              \
BEGIN_METHOD(__name##_new, __gtype x; __gtype y; __gtype w; __gtype h)                                                        \
                                                                                                                              \
  if (!MISSING(x) && !MISSING(y))                                                                                             \
  {                                                                                                                           \
    __this->x = VARG(x);                                                                                                      \
    __this->y = VARG(y);                                                                                                      \
  }                                                                                                                           \
  else if (!MISSING(x) || !MISSING(y))                                                                                        \
  {                                                                                                                           \
    GB.Error("Not enough arguments");                                                                                         \
  }                                                                                                                           \
                                                                                                                              \
END_METHOD                                                                                                                    \
                                                                                                                              \
BEGIN_METHOD(__name##_call, __gtype x; __gtype y)                                                                             \
                                                                                                                              \
  GB.ReturnObject(__struct##_create(VARGOPT(x, 0), VARGOPT(y, 0)));                                                           \
                                                                                                                              \
END_METHOD                                                                                                                    \
                                                                                                                              \
BEGIN_PROPERTY(__name##_X)                                                                                                    \
                                                                                                                              \
  if (READ_PROPERTY)                                                                                                          \
    __return(__this->x);                                                                                                      \
  else                                                                                                                        \
    __this->x = VPROP(__gtype);                                                                                               \
                                                                                                                              \
END_PROPERTY                                                                                                                  \
                                                                                                                              \
BEGIN_PROPERTY(__name##_Y)                                                                                                    \
                                                                                                                              \
  if (READ_PROPERTY)                                                                                                          \
    __return(__this->y);                                                                                                      \
  else                                                                                                                        \
    __this->y = VPROP(__gtype);                                                                                               \
                                                                                                                              \
END_PROPERTY                                                                                                                  \
                                                                                                                              \
BEGIN_METHOD_VOID(__name##_Copy)                                                                                              \
                                                                                                                              \
  GB.ReturnObject(__struct##_create(__this->x, __this->y));                                                                   \
                                                                                                                              \
END_METHOD                                                                                                                    \
                                                                                                                              \
BEGIN_METHOD(__name##_InRect, GB_OBJECT rect)                                                                                 \
                                                                                                                              \
  __rstruct *rect = VARG(rect);                                                                                               \
                                                                                                                              \
  if (GB.CheckObject(rect))                                                                                                   \
    return;                                                                                                                   \
                                                                                                                              \
  GB.ReturnBoolean(                                                                                                           \
    (__this->x >= rect->x) && (__this->x < (rect->x + rect->w))                                                               \
    && (__this->y >= rect->y) && (__this->y < (rect->y + rect->h))                                                            \
  );                                                                                                                          \
                                                                                                                              \
END_METHOD                                                                                                                    \
                                                                                                                              \
GB_DESC __name##Desc[] =                                                                                                      \
{                                                                                                                             \
  GB_DECLARE(#__name, sizeof(__struct)),                                                                                      \
                                                                                                                              \
  GB_METHOD("_new", NULL, __name##_new, "[(X)" __sign "(Y)" __sign "]"),                                                      \
  GB_STATIC_METHOD("_call", #__name, __name##_call, "[(X)" __sign "(Y)" __sign "]"),                                          \
                                                                                                                              \
  GB_PROPERTY("X", __sign, __name##_X),                                                                                       \
  GB_PROPERTY("Y", __sign, __name##_Y),                                                                                       \
                                                                                                                              \
  GB_METHOD("Copy", #__name, __name##_Copy, NULL),                                                                            \
  GB_METHOD("InRect", "b", __name##_InRect, "(Rectangle)" #__rname ";"),                                                      \
                                                                                                                              \
  GB_INTERFACE("_operator", &_operator_##__name),                                                                             \
  GB_INTERFACE("_convert", &_convert_##__name),                                                                               \
                                                                                                                              \
  GB_END_DECLARE                                                                                                              \
};