File: GSConcreteValueTemplate.m

package info (click to toggle)
gnustep-base 1.28.1%2Breally1.28.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,008 kB
  • sloc: objc: 223,137; ansic: 35,562; sh: 184; makefile: 128; cpp: 122; xml: 32
file content (277 lines) | stat: -rw-r--r-- 6,370 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# line 1 "GSConcreteValueTemplate.m"	/* So gdb knows which file we are in */
/* GSConcreteValueTemplate - Object encapsulation for C types.
   Copyright (C) 1993,1994 Free Software Foundation, Inc.

   Written by:  Adam Fedor <fedor@boulder.colorado.edu>
   Date: Mar 1995

   This file is part of the GNUstep Base Library.

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

   This library 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
   Lesser General Public License for more details.

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


/* This file should be run through a preprocessor with the macro TYPE_ORDER
   defined to a number from 0 to 4 corresponding to each value type */
#if TYPE_ORDER == 0
@interface GSNonretainedObjectValue : NSValue
{
  id data;
}
@end
#  define GSTemplateValue	GSNonretainedObjectValue
#  define TYPE_METHOD	nonretainedObjectValue
#  define TYPE_NAME	id
#elif TYPE_ORDER == 1
@interface GSPointValue : NSValue
{
  NSPoint data;
}
@end
#  define GSTemplateValue	GSPointValue
#  define TYPE_METHOD	pointValue
#  define TYPE_NAME	NSPoint
#elif TYPE_ORDER == 2
@interface GSPointerValue : NSValue
{
  void *data;
}
@end
#  define GSTemplateValue	GSPointerValue
#  define TYPE_METHOD	pointerValue
#  define TYPE_NAME	void *
#elif TYPE_ORDER == 3
#  define GSTemplateValue	GSRangeValue
@interface GSRangeValue : NSValue
{
  NSRange data;
}
@end
#  define TYPE_METHOD	rangeValue
#  define TYPE_NAME	NSRange
#elif TYPE_ORDER == 4
@interface GSRectValue : NSValue
{
  NSRect data;
}
@end
#  define GSTemplateValue	GSRectValue
#  define TYPE_METHOD	rectValue
#  define TYPE_NAME	NSRect
#elif TYPE_ORDER == 5
@interface GSSizeValue : NSValue
{
  NSSize data;
}
@end
#  define GSTemplateValue	GSSizeValue
#  define TYPE_METHOD	sizeValue
#  define TYPE_NAME	NSSize
#endif

@implementation GSTemplateValue

+ (void) initialize
{
  /*
   * Ensure that the version encoded is that used by the abstract class.
   */
  [self setVersion: [super version]];
}

// Allocating and Initializing

- (id) initWithBytes: (const void *)value
	    objCType: (const char *)type
{
  typedef __typeof__(data) _dt;
  self = [super init];
  data = *(_dt *)value;
  return self;
}

// Accessing Data
- (void) getValue: (void *)value
{
  NSUInteger	size;
  if (!value)
    {
      [NSException raise: NSInvalidArgumentException
		  format: @"Cannot copy value into NULL buffer"];
	/* NOT REACHED */
    }
  NSGetSizeAndAlignment([self objCType], 0, &size);
  memcpy(value, &data, size);
}

- (BOOL) isEqual: (id)other
{
  if (other != nil && GSObjCIsInstance(other) == YES
    && GSObjCIsKindOf(object_getClass(other), object_getClass(self)))
    {
      return [self isEqualToValue: other];
    }
  return NO;
}

- (BOOL) isEqualToValue: (NSValue*)aValue
{
  typedef __typeof__(data) _dt;

  if (aValue != nil && GSObjCIsInstance(aValue) == YES
    && GSObjCIsKindOf(object_getClass(aValue), object_getClass(self)))
    {
      _dt	val = [aValue TYPE_METHOD];
#if TYPE_ORDER == 0
      return [data isEqual: val];
#elif TYPE_ORDER == 1
      if (data.x == val.x && data.y == val.y)
	return YES;
      else
	return NO;
#elif TYPE_ORDER == 2
      if (data == val)
	return YES;
      else
	return NO;
#elif TYPE_ORDER == 3
      if (data.location == val.location
	&& data.length == val.length)
	return YES;
      else
	return NO;
#elif TYPE_ORDER == 4
      if (data.origin.x == val.origin.x && data.origin.y == val.origin.y
	&& data.size.width == val.size.width
	&& data.size.height == val.size.height)
	return YES;
      else
	return NO;
#elif TYPE_ORDER == 5
      if (data.width == val.width && data.height == val.height)
	return YES;
      else
	return NO;
#endif
    }
  return NO;
}

- (NSUInteger) hash
{
#if TYPE_ORDER == 0
  return [data hash];
#elif TYPE_ORDER == 1
  union {
    double d;
    unsigned char c[sizeof(double)];
  } val;
  NSUInteger	hash = 0;
  unsigned int	i;

  val.d = data.x + data.y;
  for (i = 0; i < sizeof(double); i++)
    hash += val.c[i];
  return hash;
#elif TYPE_ORDER == 2
  return (NSUInteger)(uintptr_t)data;
#elif TYPE_ORDER == 3
  return (data.length ^ data.location);
#elif TYPE_ORDER == 4
  union {
    double d;
    unsigned char c[sizeof(double)];
  } val;
  NSUInteger	hash = 0;
  unsigned int	i;

  val.d = data.origin.x + data.origin.y + data.size.width + data.size.height;
  for (i = 0; i < sizeof(double); i++)
    hash += val.c[i];
  return hash;
#elif TYPE_ORDER == 5
  union {
    double d;
    unsigned char c[sizeof(double)];
  } val;
  NSUInteger	hash = 0;
  unsigned int	i;

  val.d = data.width + data.height;
  for (i = 0; i < sizeof(double); i++)
    hash += val.c[i];
  return hash;
#endif
}

- (const char *)objCType
{
  typedef __typeof__(data) _dt;
  return @encode(_dt);
}

- (TYPE_NAME)TYPE_METHOD
{
  return data;
}

- (NSString *) description
{
#if TYPE_ORDER == 0
  return [NSString stringWithFormat: @"{object = %p;}", data];
#elif TYPE_ORDER == 1
  return NSStringFromPoint(data);
#elif TYPE_ORDER == 2
  return [NSString stringWithFormat: @"{pointer = %p;}", data];
#elif TYPE_ORDER == 3
  return NSStringFromRange(data);
#elif TYPE_ORDER == 4
  return NSStringFromRect(data);
#elif TYPE_ORDER == 5
  return NSStringFromSize(data);
#endif
}

#if TYPE_ORDER == 1
- (NSSize)sizeValue
{
  return NSMakeSize(data.x, data.y);
}
#elif TYPE_ORDER == 5
- (NSPoint)pointValue
{
  return NSMakePoint(data.width, data.height);
}
#endif

// NSCoding
- (void) encodeWithCoder: (NSCoder*)coder
{
#if	TYPE_ORDER == 0
  [NSException raise: NSInternalInconsistencyException
	      format: @"Attempt to encode a non-retained object"];
#elif	TYPE_ORDER == 2
  [NSException raise: NSInternalInconsistencyException
	      format: @"Attempt to encode a pointer to void object"];
#else
  [super encodeWithCoder: coder];
#endif
}

@end
#undef GSTemplateValue
#undef TYPE_METHOD
#undef TYPE_NAME