Class: OSX::ObjcPtr

Inherits:
Object show all
Defined in:
src/objc/cls_objcptr.m,
src/objc/cls_objcptr.m

Overview

Representaion of C-pointer in Ruby world. Commonly, RubyCocoa represents most of C-types or C-structures as OSX::Boxed.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) allocate_as_bool



185
186
187
188
189
190
191
192
# File 'src/objc/cls_objcptr.m', line 185

static VALUE
rb_objcptr_s_allocate_as_int8(VALUE klass)
{
  VALUE obj;
  obj = _objcptr_s_new (klass, sizeof(int8_t));
  _set_encoding(obj, "c"); /* char */
  return obj;
}

+ (Object) allocate_as_int



203
204
205
206
207
208
209
210
# File 'src/objc/cls_objcptr.m', line 203

static VALUE
rb_objcptr_s_allocate_as_int32(VALUE klass)
{
  VALUE obj;
  obj = _objcptr_s_new (klass, sizeof(int32_t));
  _set_encoding(obj, "i"); /* int */
  return obj;
}

+ (Object) allocate_as_int16



194
195
196
197
198
199
200
201
# File 'src/objc/cls_objcptr.m', line 194

static VALUE
rb_objcptr_s_allocate_as_int16(VALUE klass)
{
  VALUE obj;
  obj = _objcptr_s_new (klass, sizeof(int16_t));
  _set_encoding(obj, "s"); /* short */
  return obj;
}

+ (Object) allocate_as_int32



203
204
205
206
207
208
209
210
# File 'src/objc/cls_objcptr.m', line 203

static VALUE
rb_objcptr_s_allocate_as_int32(VALUE klass)
{
  VALUE obj;
  obj = _objcptr_s_new (klass, sizeof(int32_t));
  _set_encoding(obj, "i"); /* int */
  return obj;
}

+ (Object) allocate_as_int8



185
186
187
188
189
190
191
192
# File 'src/objc/cls_objcptr.m', line 185

static VALUE
rb_objcptr_s_allocate_as_int8(VALUE klass)
{
  VALUE obj;
  obj = _objcptr_s_new (klass, sizeof(int8_t));
  _set_encoding(obj, "c"); /* char */
  return obj;
}

+ (Object) new



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
# File 'src/objc/cls_objcptr.m', line 157

static VALUE
rb_objcptr_s_allocate(int argc, VALUE* argv, VALUE klass)
{
  VALUE  key, cnt, obj;
  size_t length;
  const struct _encoding_type_rec* rec;

  argc = rb_scan_args(argc, argv, "11", &key, &cnt);

  if (argc == 1 && ! SYMBOL_P(key)) {
    length = NUM2LONG(key);
    obj = _objcptr_s_new (klass, length);
    _set_encoding(obj, "C"); /* uchar */
    return obj;
  }

  rec = _lookup_encoding_type(key);
  if (rec == NULL)
    rb_raise(rb_eRuntimeError, "unsupported encoding -- %s", 
	     rb_id2name(rb_to_id(key)));

  length = (argc == 2) ? NUM2LONG(cnt) : 1;
  length *= ocdata_size(rec->encoding);
  obj = _objcptr_s_new (klass, length);
  _set_encoding(obj, rec->encoding);
  return obj;
}

Instance Method Details

- (Object) []



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'src/objc/cls_objcptr.m', line 518

static VALUE
rb_objcptr_at (int argc, VALUE* argv, VALUE rcv)
{
  VALUE key, count;
  
  if (ENCODING_OF(rcv) == NULL)
    rb_raise(rb_eRuntimeError, "#[] can't be called on this instance");

  argc = rb_scan_args(argc, argv, "11", &key, &count);
  if (argc == 1)
    return objcptr_at(rcv, key);
  else if (argc == 2)
    return objcptr_to_a(rcv, key, count);
  return Qnil;
}

- (Object) []=



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'src/objc/cls_objcptr.m', line 436

static VALUE
rb_objcptr_set_at (VALUE rcv, VALUE key, VALUE val)
{
  unsigned offset;

  Check_Type(key, T_FIXNUM);

  if (ENCODING_OF(rcv) == NULL)
    rb_raise(rb_eRuntimeError, "#[]= can't be called on this instance");

  offset = FIX2INT(key);  
  offset *= ocdata_size(ENCODING_OF(rcv));
 
  if (!rbobj_to_ocdata(val, ENCODING_OF(rcv), CPTR_OF(rcv) + offset, NO))
    rb_raise(rb_eRuntimeError, "Can't convert given object to type '%s' at index %d offset %d", ENCODING_OF(rcv), FIX2INT(key), offset);

  return val;
}

- (Object) __regard_as__ Also known as: regard_as



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
278
279
280
281
282
283
# File 'src/objc/cls_objcptr.m', line 249

static VALUE
rb_objcptr_regard_as(VALUE rcv, VALUE key)
{
  const struct _encoding_type_rec* rec;
  BOOL ok;
  const char *encoding;

  ok = YES; 
  rec = _lookup_encoding_type(key);
  if (rec == NULL) {
#if __LP64__
    unsigned long size;
#else
    unsigned int size;
#endif
    ok = NO;
    encoding = StringValuePtr(key);
    @try {
      NSGetSizeAndAlignment(StringValuePtr(key), &size, NULL);
      if (size > 0)
        ok = YES;
    }
    @catch (id exception) {}
  }
  else {
    encoding = rec->encoding;
  }

  if (!ok)
    rb_raise(rb_eRuntimeError, "unsupported encoding -- %s", 
	     rb_id2name(rb_to_id(key)));

  _set_encoding(rcv, encoding);
  return rcv;
}

- (Object) allocated_size



229
230
231
232
233
# File 'src/objc/cls_objcptr.m', line 229

static VALUE
rb_objcptr_allocated_size(VALUE rcv)
{
  return UINT2NUM (ALLOCATED_SIZE_OF (rcv));
}

- (Object) assign



455
456
457
458
459
460
461
462
463
464
465
# File 'src/objc/cls_objcptr.m', line 455

static VALUE
rb_objcptr_assign (VALUE rcv, VALUE obj)
{
  if (ENCODING_OF(rcv) == NULL)
    rb_raise(rb_eRuntimeError, "#assign can't be called on this instance");

  if (!rbobj_to_ocdata(obj, ENCODING_OF(rcv), CPTR_OF(rcv), NO))
    rb_raise(rb_eArgError, "Can't convert object to type '%s'", ENCODING_OF(rcv));
  
  return rcv;
}

- (Object) bytestr



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'src/objc/cls_objcptr.m', line 294

static VALUE
rb_objcptr_bytestr(int argc, VALUE* argv, VALUE rcv)
{
  VALUE  rb_length;
  VALUE  str;
  long length;

  length = ALLOCATED_SIZE_OF(rcv);
  rb_scan_args(argc, argv, "01", &rb_length);
  if (length == 0 || rb_length != Qnil) {
    if (! FIXNUM_P(rb_length))
      Check_Type(rb_length, T_BIGNUM);
    length = NUM2LONG(rb_length);
  }
  // ASCII-8BIT on ruby-2.0 or later
  str = rb_tainted_str_new (CPTR_OF(rcv), length);
  return str;
}

- (Object) bytestr_at



285
286
287
288
289
290
291
292
# File 'src/objc/cls_objcptr.m', line 285

static VALUE
rb_objcptr_bytestr_at(VALUE rcv, VALUE offset, VALUE length)
{
  VALUE str;
  // ASCII-8BIT on ruby-2.0 or later
  str = rb_tainted_str_new ((char*)CPTR_OF(rcv) + NUM2LONG(offset), NUM2LONG(length));
  return str;
}

- (Object) cast_as



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'src/objc/cls_objcptr.m', line 467

static VALUE
rb_objcptr_cast_as (VALUE rcv, VALUE encoding)
{
  VALUE val;
  const char* enc = StringValuePtr(encoding);
  void* ptr;
  
  if (*enc == '^') {
    enc++;
    ptr = CPTR_OF(rcv);
  } else {
    ptr = &CPTR_OF(rcv);
  }
  
  if (!ocdata_to_rbobj(Qnil, enc, ptr, &val, NO))
    rb_raise(rb_eArgError, "Can't convert object to type '%s'", StringValuePtr(encoding));
  return val;
}

- (Object) encoding



241
242
243
244
245
246
247
# File 'src/objc/cls_objcptr.m', line 241

static VALUE
rb_objcptr_encoding(VALUE rcv)
{
  return ENCODING_OF(rcv) ?
    rb_str_new2(ENCODING_OF(rcv)):
    Qnil;
}

- (Object) inspect



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'src/objc/cls_objcptr.m', line 212

static VALUE
rb_objcptr_inspect(VALUE rcv)
{
  char s[512];
  VALUE rbclass_name;

  rbclass_name = rb_mod_name(CLASS_OF(rcv));
  snprintf(s, sizeof(s), "#<%s:0x%lx cptr=%p allocated_size=%ld encoding=%s>",
           StringValuePtr(rbclass_name),
           NUM2ULONG(rb_obj_id(rcv)),
           CPTR_OF(rcv),
           ALLOCATED_SIZE_OF(rcv),
           ENCODING_OF(rcv) ? ENCODING_OF(rcv) : "(unknown)");
  // cptrlog ("rb_objcptr_inspect", rcv);
  return rb_str_new2(s);
}

- (Object) int16



368
369
370
371
372
# File 'src/objc/cls_objcptr.m', line 368

static VALUE
rb_objcptr_int16(VALUE rcv)
{
  return INT2NUM (* (int16_t*) CPTR_OF(rcv));
}

- (Object) int16_at



327
328
329
330
331
332
# File 'src/objc/cls_objcptr.m', line 327

static VALUE
rb_objcptr_int16_at(VALUE rcv, VALUE index)
{
  int16_t* ptr = (int16_t*) CPTR_OF(rcv);
  return INT2NUM ( ptr [NUM2LONG(index)] );
}

- (Object) int32 Also known as: int



380
381
382
383
384
# File 'src/objc/cls_objcptr.m', line 380

static VALUE
rb_objcptr_int32(VALUE rcv)
{
  return INT2NUM (* (int32_t*) CPTR_OF(rcv));
}

- (Object) int32_at Also known as: int_at



341
342
343
344
345
346
# File 'src/objc/cls_objcptr.m', line 341

static VALUE
rb_objcptr_int32_at(VALUE rcv, VALUE index)
{
  int32_t* ptr = (int32_t*) CPTR_OF(rcv);
  return INT2NUM ( ptr [NUM2LONG(index)] );
}

- (Object) int8



356
357
358
359
360
# File 'src/objc/cls_objcptr.m', line 356

static VALUE
rb_objcptr_int8(VALUE rcv)
{
  return INT2NUM (* (int8_t*) CPTR_OF(rcv));
}

- (Object) int8_at



313
314
315
316
317
318
# File 'src/objc/cls_objcptr.m', line 313

static VALUE
rb_objcptr_int8_at(VALUE rcv, VALUE index)
{
  int8_t* ptr = (int8_t*) CPTR_OF(rcv);
  return INT2NUM ( ptr [NUM2LONG(index)] );
}

- (Object) uint16



374
375
376
377
378
# File 'src/objc/cls_objcptr.m', line 374

static VALUE
rb_objcptr_uint16(VALUE rcv)
{
  return UINT2NUM (* (u_int16_t*) CPTR_OF(rcv));
}

- (Object) uint16_at



334
335
336
337
338
339
# File 'src/objc/cls_objcptr.m', line 334

static VALUE
rb_objcptr_uint16_at(VALUE rcv, VALUE index)
{
  u_int16_t* ptr = (u_int16_t*) CPTR_OF(rcv);
  return UINT2NUM ( ptr [NUM2LONG(index)] );
}

- (Object) uint32 Also known as: uint



386
387
388
389
390
# File 'src/objc/cls_objcptr.m', line 386

static VALUE
rb_objcptr_uint32(VALUE rcv)
{
  return UINT2NUM (* (u_int32_t*) CPTR_OF(rcv));
}

- (Object) uint32_at Also known as: uint_at



348
349
350
351
352
353
# File 'src/objc/cls_objcptr.m', line 348

static VALUE
rb_objcptr_uint32_at(VALUE rcv, VALUE index)
{
  u_int32_t* ptr = (u_int32_t*) CPTR_OF(rcv);
  return UINT2NUM ( ptr [NUM2LONG(index)] );
}

- (Object) uint8 Also known as: bool



362
363
364
365
366
# File 'src/objc/cls_objcptr.m', line 362

static VALUE
rb_objcptr_uint8(VALUE rcv)
{
  return UINT2NUM (* (u_int8_t*) CPTR_OF(rcv));
}

- (Object) uint8_at Also known as: bool_at



320
321
322
323
324
325
# File 'src/objc/cls_objcptr.m', line 320

static VALUE
rb_objcptr_uint8_at(VALUE rcv, VALUE index)
{
  u_int8_t* ptr = (u_int8_t*) CPTR_OF(rcv);
  return UINT2NUM ( ptr [NUM2LONG(index)] );
}