Class: RbSDL2::RWOps
- Inherits:
-
Object
show all
- Defined in:
- lib/rb_sdl2/rw_ops/rw_ops.rb,
lib/rb_sdl2/rw_ops/rw_ops_pointer.rb
Defined Under Namespace
Classes: RWOpsPointer
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ptr) ⇒ RWOps
Returns a new instance of RWOps.
21
22
23
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 21
def initialize(ptr)
@ptr = ptr
end
|
Class Method Details
.open ⇒ Object
8
9
10
11
12
13
14
15
16
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 8
def open(...)
rw = new(...)
return rw unless block_given?
begin
yield(rw)
ensure
rw.close
end
end
|
Instance Method Details
#close ⇒ Object
close 呼び出しの結果によらずポインターは開放されます。 継承先のクラスは close をオーバーライドしてポインターを適切に扱う必要があります。
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 27
def close
unless closed?
err = ::SDL.RWclose(@ptr)
raise RbSDL2Error if err < 0
end
rescue => e
raise e if $DEBUG
ensure
@ptr.autorelease = false
@ptr = nil
end
|
#closed? ⇒ Boolean
43
44
45
46
47
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 43
def closed?
!@ptr&.autorelease?
end
|
#pos=(n) ⇒ Object
49
50
51
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 49
def pos=(n)
seek(n, IO::SEEK_SET)
end
|
#read(length = nil) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 53
def read(length = nil)
raise IOError if closed?
len = length.nil? ? size - tell : length
raise ArgumentError if len < 0
return "" if len == 0
ptr = ::FFI::MemoryPointer.new(len)
num = ::SDL.RWread(@ptr, ptr, 1, len)
raise RbSDL2Error if num == 0
ptr.read_bytes(num)
end
|
#seek(offset, whence = IO::SEEK_SET) ⇒ Object
64
65
66
67
68
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 64
def seek(offset, whence = IO::SEEK_SET)
raise IOError if closed?
raise RbSDL2Error if ::SDL.RWseek(@ptr, offset, whence) == -1
0
end
|
#size ⇒ Object
70
71
72
73
74
75
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 70
def size
raise IOError if closed?
num = ::SDL.RWsize(@ptr)
raise RbSDL2Error if num < 0
num
end
|
#tell ⇒ Object
Also known as:
pos
77
78
79
80
81
82
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 77
def tell
raise IOError if closed?
num = ::SDL.RWtell(@ptr)
raise RbSDL2Error if num == -1
num
end
|
#to_ptr ⇒ Object
close メソッドを呼び出した後、インスタンスからポインターを取り出すことはできません。
86
87
88
89
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 86
def to_ptr
raise TypeError if closed?
@ptr
end
|
#write(*str) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/rb_sdl2/rw_ops/rw_ops.rb', line 91
def write(*str)
raise FrozenError if frozen?
raise IOError if closed?
str.inject(0) do |sum, obj|
bytes = obj.to_s
len = bytes.size
ptr = ::FFI::MemoryPointer.new(len).write_bytes(bytes)
num = ::SDL.RWwrite(@ptr, ptr, 1, len)
raise RbSDL2Error if num < len
sum + len
end
end
|