Class: CGLM::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cglm/base.rb,
ext/cglm/rb_cglm.c

Direct Known Subclasses

AABB, MatrixType, VectorType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr: CGLM.alloc(self.class).tap { |ptr| # Init malloc'd values to 0; there is a PR to fiddle for this, then we # can delete this code. https://github.com/ruby/fiddle/pull/14 ptr[0, self.class.size] = "\x00".b * self.class.size }) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/cglm/base.rb', line 8

def initialize(addr: CGLM.alloc(self.class).tap { |ptr|
  # Init malloc'd values to 0; there is a PR to fiddle for this, then we
  # can delete this code. https://github.com/ruby/fiddle/pull/14
  ptr[0, self.class.size] = "\x00".b * self.class.size
})
  # Even if addr is present, but points to NULL, this is a problem that
  # will lead to segfaults. If not present, `nil.to_i == 0`.
  raise ArgumentError, 'BUG: object initialized without a backing store?' if addr.to_i == 0
  self.addr = addr
end

Instance Attribute Details

#addrObject Also known as: to_ptr

Returns the value of attribute addr.



5
6
7
# File 'lib/cglm/base.rb', line 5

def addr
  @addr
end

Instance Method Details

#copy_to(other) ⇒ Object



19
20
21
22
23
# File 'lib/cglm/base.rb', line 19

def copy_to(other)
  size = addr.size > other.addr.size ? other.addr.size : addr.size
  other.addr[0, size] = addr
  other
end

#dup(other = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/cglm/base.rb', line 25

def dup(other = nil)
  if other
    copy_to other
  else
    super()
  end
end

#initialize_dup(other) ⇒ Object



33
34
35
36
37
# File 'lib/cglm/base.rb', line 33

def initialize_dup(other)
  super
  @addr = CGLM.alloc(self.class)
  other.copy_to(self)
end