Class: CShadow::CharPointerAttribute

Inherits:
PointerAttribute show all
Defined in:
lib/cgen/attribute.rb

Overview

Stores a null-terminated string

Instance Attribute Summary

Attributes inherited from CNativeAttribute

#ctype

Attributes inherited from Attribute

#cdecl, #check, #cvar, #dump, #free, #init, #load, #mark, #owner_class, #persists, #reader, #var, #writer

Instance Method Summary collapse

Methods inherited from CNativeAttribute

match

Methods inherited from Attribute

inherited, #inspect, match

Constructor Details

#initialize(*args) ⇒ CharPointerAttribute

Returns a new instance of CharPointerAttribute.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/cgen/attribute.rb', line 367

def initialize(*args)
  super
  @reader =
    "result = shadow->#{@cvar} ? rb_str_new2(shadow->#{@cvar}) : Qnil"
  @writer = %{
    {
      int len;
      char *str;
      
      free(shadow->#{@cvar});

      if (arg == Qnil)
        shadow->#{@cvar} = 0;
      else {
        StringValueCStr(arg);
        len = RSTRING_LEN(arg);
        str = RSTRING_PTR(arg);
        shadow->#{@cvar} = ALLOC_N(char, len + 1);

        if (str)
          memcpy(shadow->#{@cvar}, str, len);

        shadow->#{@cvar}[len] = '\\0';
      }
    }
  }
  @dump = %{
    rb_ary_push(result,
      shadow->#{@cvar} ? rb_str_new2(shadow->#{@cvar}) : 0);
  }
  @load = %{
    {
      VALUE arg = rb_ary_shift(from_array);
      int len;
      char *str;
                
      if (arg == Qnil)
        shadow->#{@cvar} = 0;
      else {
        len = RSTRING_LEN(arg);
        str = RSTRING_PTR(arg);
        shadow->#{@cvar} = ALLOC_N(char, len + 1);

        if (str)
          memcpy(shadow->#{@cvar}, str, len);

        shadow->#{@cvar}[len] = '\\0';
      }
    }
  }
end