Method: Snow::Mat4Array#fetch

Defined in:
ext/snow-math/snow-math.c

#fetch(sm_index) ⇒ Object Also known as: []

Fetches a Mat4 from the array at the index and returns it. The returned Mat4 may be a cached object. In all cases, values returned from a typed array are associated with the memory of the array and not given their own memory. So, modifying a Mat4 fetched from an array modifies the array’s data.

As a result, objects returned by a Mat4Array should not be considered thread-safe, nor should manipulating a Mat4Array be considered thread-safe either. If you want to work with data returned from an array without altering the array data, you should call Mat4#dup or Mat4#copy to get a new Mat4 with a copy of the array object’s data.

call-seq: fetch(index) -> mat4



1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
# File 'ext/snow-math/snow-math.c', line 1434

static VALUE sm_mat4_array_fetch(VALUE sm_self, VALUE sm_index)
{
  mat4_t *arr;
  size_t length = NUM2SIZET(sm_mathtype_array_length(sm_self));
  size_t index = NUM2SIZET(sm_index);
  VALUE sm_inner;
  VALUE sm_cache;
  if (index >= length) {
    rb_raise(rb_eRangeError,
      "Index %zu out of bounds for array with length %zu",
      index, length);
  }

  sm_cache = rb_ivar_get(sm_self, kRB_IVAR_MATHARRAY_CACHE);
  if (!RTEST(sm_cache)) {
    rb_raise(rb_eRuntimeError, "No cache available");
  }
  sm_inner = rb_ary_entry(sm_cache, (long)index);

  if (!RTEST(sm_inner)) {
    /* No cached value, create one. */
    Data_Get_Struct(sm_self, mat4_t, arr);
    sm_inner = Data_Wrap_Struct(s_sm_mat4_klass, 0, 0, arr[index]);
    rb_ivar_set(sm_inner, kRB_IVAR_MATHARRAY_SOURCE, sm_self);
    /* Store the Mat4 in the cache */
    rb_ary_store(sm_cache, (long)index, sm_inner);
  }

  if (OBJ_FROZEN(sm_self)) {
    rb_funcall2(sm_inner, kRB_NAME_FREEZE, 0, 0);
  }

  return sm_inner;
}