Class: DataView

Inherits:
Object
  • Object
show all
Defined in:
ext/type_array/data_view.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(buf) ⇒ DataView .new("buffer") ⇒ DataView

Creates a new DataView instance. Both ArrayBuffer and data (String) constructors are supported.

Examples

buf = ArrayBuffer.new(8)         =>  ArrayBuffer
view = DataView.new(buf)         =>  DataView

view = DataView.new(buf, 2)      =>  DataView
view.byte_offset                 => 2
view.byte_length                 => 6

view = DataView.new(buf, 2, 4)   =>  DataView
view.byte_offset                 => 2
view.byte_length                 => 2

view = DataView.new("buffer")    =>  DataView

Overloads:



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
# File 'ext/type_array/data_view.c', line 74

static VALUE rb_data_view_s_new(int argc, VALUE *argv, VALUE klass)
{
    VALUE data_view;
    unsigned long new_byte_length;
    rb_data_view_t *view = NULL;
    VALUE obj, byte_offset, byte_length, buffer;
    new_byte_length = 0;
    rb_scan_args(argc, argv, "12", &obj, &byte_offset, &byte_length);
    data_view = Data_Make_Struct(klass, rb_data_view_t, rb_mark_data_view, rb_free_data_view, view);
    view->byte_offset = 0;
    view->byte_length = 0;
    if (rb_class_of(obj) == rb_cArrayBuffer) {
        GetArrayBuffer(obj);
        view->byte_length = buf->length;
        view->buf = obj;
    } else if (rb_type(obj) == T_STRING){
        ArrayBufferEncode(obj);
        buffer = rb_alloc_array_buffer((unsigned long)RSTRING_LEN(obj), (void *)RSTRING_PTR(obj));
        GetArrayBuffer(buffer);
        view->byte_length = buf->length;
        view->buf = buffer;
    } else {
        rb_raise(rb_eTypeError, "DataView constructor %s not supported.", RSTRING_PTR(rb_obj_as_string(obj)));
    }
    if (!NIL_P(byte_offset)) {
        Check_Type(byte_offset, T_FIXNUM);
         view->byte_offset = FIX2ULONG(byte_offset);
         if (view->byte_offset >= view->byte_length)
            rb_raise(rb_eRangeError, "Byte offset out of range.");
    }
    if (!NIL_P(byte_length)) {
        Check_Type(byte_length, T_FIXNUM);
        new_byte_length = FIX2ULONG(byte_length);
        if (new_byte_length > view->byte_length)
            rb_raise(rb_eRangeError, "Byte length out of range.");
        if ((view->byte_offset + new_byte_length) > view->byte_length)
            rb_raise(rb_eRangeError, "Byte offset / length is not aligned.");
        view->byte_length = new_byte_length;
    }
    view->byte_length -= view->byte_offset;
    rb_obj_call_init(data_view, 0, NULL);
    return data_view;
}

Instance Method Details

#bufferString

Returns the underlying buffer managed by this DataView instance.

Examples

view = DataView.new("buffer")   =>  DataView
view.buffer                     =>  ArrayBuffer

Returns:

  • (String)


166
167
168
169
170
# File 'ext/type_array/data_view.c', line 166

static VALUE rb_data_view_buffer(VALUE obj)
{
    GetDataView(obj);
    return view->buf; 
}

#byte_lengthFixnum

Returns the size of the underlying buffer managed by this DataView instance.

Examples

view = DataView.new("buffer")   =>  DataView
view.byte_length                =>  6

Returns:

  • (Fixnum)


129
130
131
132
133
# File 'ext/type_array/data_view.c', line 129

static VALUE rb_data_view_byte_length(VALUE obj)
{
    GetDataView(obj);
    return ULONG2NUM(view->byte_length);
}

#byte_offsetFixnum

Returns the offset into the underlying buffer managed by this DataView instance.

Examples

view = DataView.new("buffer")     =>  DataView
view.byte_offset                  =>  0

view = DataView.new("buffer", 2)  =>  DataView
view.byte_offset                  =>  2

Returns:

  • (Fixnum)


149
150
151
152
153
# File 'ext/type_array/data_view.c', line 149

static VALUE rb_data_view_byte_offset(VALUE obj)
{
    GetDataView(obj);
    return ULONG2NUM(view->byte_offset);
}

#get_float32(2) ⇒ Float

Gets a float32 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)   =>  ArrayBuffer
view = DataView.new(buf)     =>  DataView
view.set_float32(2, 77.643)  =>  nil
view.get_float32(2)          =>  758

Returns:

  • (Float)


471
472
473
474
475
# File 'ext/type_array/data_view.c', line 471

static VALUE rb_data_view_get_float32(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return rb_float_new((double)rb_type_array_get_float32(buf->buf, index, little_endian));
}

#get_float64(2) ⇒ Float

Gets a float64 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)   =>  ArrayBuffer
view = DataView.new(buf)     =>  DataView
view.set_float64(2, 77.643)  =>  nil
view.get_float64(2)          =>  758

Returns:

  • (Float)


523
524
525
526
527
# File 'ext/type_array/data_view.c', line 523

static VALUE rb_data_view_get_float64(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return rb_float_new(rb_type_array_get_float64(buf->buf, index, little_endian));
}

#get_int16(2) ⇒ Fixnum

Gets an int16 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int16(2, 20)       =>  nil
view.get_int16(2)           =>  20

Returns:

  • (Fixnum)


304
305
306
307
308
# File 'ext/type_array/data_view.c', line 304

static VALUE rb_data_view_get_int16(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return INT2FIX(rb_type_array_get_int16(buf->buf, index, little_endian));
}

#get_int32(2) ⇒ Fixnum

Gets an int32 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int32(2, 758)      =>  nil
view.get_int32(2)           =>  758

Returns:

  • (Fixnum)


381
382
383
384
385
# File 'ext/type_array/data_view.c', line 381

static VALUE rb_data_view_get_int32(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return INT2FIX(rb_type_array_get_int32(buf->buf, index, little_endian));
}

#get_int8(2) ⇒ Fixnum

Gets an int8 value from a given offset

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int8(2, 5)         =>  nil
view.get_int8(2)            =>  5

Returns:

  • (Fixnum)


211
212
213
214
215
216
217
218
219
220
# File 'ext/type_array/data_view.c', line 211

static VALUE rb_data_view_get_int8(VALUE obj, VALUE offset_)
{
    int argc;
    VALUE argv[2];
    argc = 1;
    argv[0] = offset_;
    argv[1] = Qfalse;
    DataViewAget(obj);
    return TACHR2FIX(rb_type_array_get_int8(buf->buf, index, little_endian));
}

#get_uint16(2) ⇒ Fixnum

Gets an unsigned int16 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint16(2, 20)      =>  nil
view.get_uint16(2)          =>  20

Returns:

  • (Fixnum)


342
343
344
345
346
# File 'ext/type_array/data_view.c', line 342

static VALUE rb_data_view_get_uint16(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return INT2FIX(rb_type_array_get_uint16(buf->buf, index, little_endian));
}

#get_uint32(2) ⇒ Fixnum

Gets an unsigned int32 value from a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint32(2, 758)     =>  nil
view.get_uint32(2)          =>  758

Returns:

  • (Fixnum)


419
420
421
422
423
# File 'ext/type_array/data_view.c', line 419

static VALUE rb_data_view_get_uint32(int argc, VALUE *argv, VALUE obj)
{
    DataViewAget(obj);
    return INT2FIX(rb_type_array_get_uint32(buf->buf, index, little_endian));
}

#get_uint8(2) ⇒ Fixnum

Gets an unsigned int8 value from a given offset.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint8(2, 5)        =>  nil
view.get_uint8(2)           =>  5

Returns:

  • (Fixnum)


260
261
262
263
264
265
266
267
268
269
# File 'ext/type_array/data_view.c', line 260

static VALUE rb_data_view_get_uint8(VALUE obj, VALUE offset_)
{
    int argc;
    VALUE argv[2];
    argc = 1;
    argv[0] = offset_;
    argv[1] = Qfalse;
    DataViewAget(obj);
    return TACHR2FIX(rb_type_array_get_uint8(buf->buf, index, little_endian));
}

#set_float32(2, 0.775) ⇒ nil

Sets a float32 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_float32(2, 0.775)  =>  nil

Returns:

  • (nil)


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'ext/type_array/data_view.c', line 437

static VALUE rb_data_view_set_float32(int argc, VALUE *argv, VALUE obj)
{
    float val;
    DataViewAset(obj);
    switch (TYPE(item)) {
      case T_FIXNUM:
          val = (float)FIX2LONG(item);
          break;
      case T_BIGNUM:
          val = (float)rb_big2dbl(item);
          break;
      case T_FLOAT:
          val = (float)RFLOAT_VALUE(item);
          break;
      default:
          rb_raise(rb_eTypeError, "Type arrays only support Fixnum, Bignum and Float instances");
    }
    rb_type_array_set_float32(buf->buf, index, val, little_endian);
    return Qnil;
}

#set_float64(2, 77.643) ⇒ nil

Sets a float64 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)    =>  ArrayBuffer
view = DataView.new(buf)      =>  DataView
view.set_float64(2, 77.643)   =>  nil

Returns:

  • (nil)


489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'ext/type_array/data_view.c', line 489

static VALUE rb_data_view_set_float64(int argc, VALUE *argv, VALUE obj)
{
    double val;
    DataViewAset(obj);
    switch (TYPE(item)) {
      case T_FIXNUM:
          val = (double)FIX2LONG(item);
          break;
      case T_BIGNUM:
          val = rb_big2dbl(item);
          break;
      case T_FLOAT:
          val = RFLOAT_VALUE(item);
          break;
      default:
          rb_raise(rb_eTypeError, "Type arrays only support Fixnum, Bignum and Float instances");
    }
    rb_type_array_set_float64(buf->buf, index, val, little_endian);
    return Qnil;
}

#set_int16(2, 20) ⇒ nil

Sets an int16 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int16(2, 20)       =>  nil
view.set_int16(3, -2)       =>  nil

Returns:

  • (nil)


284
285
286
287
288
289
# File 'ext/type_array/data_view.c', line 284

static VALUE rb_data_view_set_int16(int argc, VALUE *argv, VALUE obj)
{
    DataViewAset(obj);
    rb_type_array_set_int16(buf->buf, index, (short)NUM2INT(item), little_endian);
    return Qnil;
}

#set_int32(2, 658) ⇒ nil

Sets an int32 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int32(2, 758)      =>  nil
view.set_int32(3, -23)      =>  nil

Returns:

  • (nil)


361
362
363
364
365
366
# File 'ext/type_array/data_view.c', line 361

static VALUE rb_data_view_set_int32(int argc, VALUE *argv, VALUE obj)
{
    DataViewAset(obj);
    rb_type_array_set_int32(buf->buf, index, NUM2INT(item), little_endian);
    return Qnil;
}

#set_int8(2, 2) ⇒ nil

Sets an int8 value at a given offset

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_int8(2, 5)         =>  nil
view.set_int8(3, -3)        =>  nil

Returns:

  • (nil)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/type_array/data_view.c', line 185

static VALUE rb_data_view_set_int8(VALUE obj, VALUE offset_, VALUE item_)
{
    int argc;
    VALUE argv[3];
    argc = 3;
    argv[0] = offset_;
    argv[1] = item_;
    argv[2] = Qfalse;
    DataViewAset(obj);
    rb_type_array_set_int8(buf->buf, index, NUM2CHR(item), little_endian);
    return Qnil;
}

#set_uint16(2, 20) ⇒ nil

Sets an unsigned int16 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint16(2, 20)      =>  nil

Returns:

  • (nil)


322
323
324
325
326
327
# File 'ext/type_array/data_view.c', line 322

static VALUE rb_data_view_set_uint16(int argc, VALUE *argv, VALUE obj)
{
    DataViewAset(obj);
    rb_type_array_set_uint16(buf->buf, index, (unsigned short)NUM2INT(item), little_endian);
    return Qnil;
}

#set_uint32(2, 20) ⇒ nil

Sets an unsigned int32 value at a given offset, using the provided endianness.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint32(2, 758)     =>  nil

Returns:

  • (nil)


399
400
401
402
403
404
# File 'ext/type_array/data_view.c', line 399

static VALUE rb_data_view_set_uint32(int argc, VALUE *argv, VALUE obj)
{
    DataViewAset(obj);
    rb_type_array_set_uint32(buf->buf, index, (unsigned int)NUM2INT(item), little_endian);
    return Qnil;
}

#set_uint8(2, 2) ⇒ nil

Sets an unsigned int8 value at a given offset.

Examples

buf = ArrayBuffer.new(100)  => ArrayBuffer
view = DataView.new(buf)    =>  DataView
view.set_uint8(2, 5)        =>  nil

Returns:

  • (nil)


234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/type_array/data_view.c', line 234

static VALUE rb_data_view_set_uint8(VALUE obj, VALUE offset_, VALUE item_)
{
    int argc;
    VALUE argv[3];
    argc = 3;
    argv[0] = offset_;
    argv[1] = item_;
    argv[2] = Qfalse;
    DataViewAset(obj);
    rb_type_array_set_uint8(buf->buf, index, (unsigned char)NUM2CHR(item), little_endian);
    return Qnil;
}

#to_sString

Returns a String (binary) representation of the underlying buffer managed by this DataView instance.

Examples

buf = ArrayBuffer.new("buffer")  =>  ArrayBuffer
view = DataView.new(buf)         =>  DataView
view.to_s                        =>  "buffer"

Returns:

  • (String)


541
542
543
544
545
# File 'ext/type_array/data_view.c', line 541

static VALUE rb_data_view_to_s(VALUE obj)
{
    GetDataView(obj);
    return rb_array_buffer_to_s(view->buf);
}