Module: CTypes::Type
- Defined in:
- lib/ctypes/type.rb
Overview
interface for all supported types
Instance Attribute Summary collapse
-
#dry_type ⇒ Object
readonly
private
Dry::Type used for constraint checking & defaults.
-
#endian ⇒ Object
readonly
endian to use when packing/unpacking.
Instance Method Summary collapse
- #default_endian ⇒ Object private
- #default_value ⇒ Object private
-
#fixed_size? ⇒ Boolean
check if this is a fixed-size type.
- #greedy? ⇒ Boolean
-
#pack(value, endian: default_endian, validate: true) ⇒ ::String
encode a ruby type into a String containing the binary representation of the c type.
-
#pread(io, pos, endian: default_endian) ⇒ Object
read a fixed-sized type from an IO instance at a specific offset and unpack it.
-
#read(io, endian: default_endian) ⇒ Object
read a fixed-sized type from an IO instance and unpack it.
-
#unpack(buf, endian: default_endian) ⇒ Object
convert a String containing the binary represention of a c type into the equivalent ruby type.
-
#unpack_all(buf, endian: default_endian) ⇒ Object
unpack as many instances of Type are present in the supplied string.
-
#unpack_one(buf, endian: default_endian) ⇒ Array(Object, ::String)
convert a String containing the binary represention of a c type into the equivalent ruby type.
-
#with_endian(value) ⇒ Type
get a fixed-endian instance of this type.
- #without_endian ⇒ Object
Instance Attribute Details
#dry_type ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dry::Type used for constraint checking & defaults
11 12 13 |
# File 'lib/ctypes/type.rb', line 11 def dry_type @dry_type end |
#endian ⇒ Object (readonly)
endian to use when packing/unpacking. nil means CTypes.default_endian will be used.
16 17 18 |
# File 'lib/ctypes/type.rb', line 16 def endian @endian end |
Instance Method Details
#default_endian ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
185 186 187 |
# File 'lib/ctypes/type.rb', line 185 def default_endian @endian || CTypes.default_endian end |
#default_value ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
180 181 182 |
# File 'lib/ctypes/type.rb', line 180 def default_value dry_type[] end |
#fixed_size? ⇒ Boolean
check if this is a fixed-size type
175 176 177 |
# File 'lib/ctypes/type.rb', line 175 def fixed_size? !!@size&.is_a?(Integer) end |
#greedy? ⇒ Boolean
170 171 172 |
# File 'lib/ctypes/type.rb', line 170 def greedy? raise NotImplementedError, "Type must implement `.greedy?`: %p" % [self] end |
#pack(value, endian: default_endian, validate: true) ⇒ ::String
encode a ruby type into a String containing the binary representation of the c type
31 32 33 |
# File 'lib/ctypes/type.rb', line 31 def pack(value, endian: default_endian, validate: true) raise NotImplementedError end |
#pread(io, pos, endian: default_endian) ⇒ Object
read a fixed-sized type from an IO instance at a specific offset and unpack it
107 108 109 110 111 112 113 114 |
# File 'lib/ctypes/type.rb', line 107 def pread(io, pos, endian: default_endian) unless fixed_size? raise NotImplementedError, "pread() does not support variable-length types" end unpack(io.pread(@size, pos), endian: default_endian) end |
#read(io, endian: default_endian) ⇒ Object
read a fixed-sized type from an IO instance and unpack it
91 92 93 94 95 96 97 98 |
# File 'lib/ctypes/type.rb', line 91 def read(io, endian: default_endian) unless fixed_size? raise NotImplementedError, "read() does not support variable-length types" end unpack(io.read(@size), endian: default_endian) end |
#unpack(buf, endian: default_endian) ⇒ Object
convert a String containing the binary represention of a c type into the equivalent ruby type
49 50 51 52 |
# File 'lib/ctypes/type.rb', line 49 def unpack(buf, endian: default_endian) o, = unpack_one(buf, endian:) o end |
#unpack_all(buf, endian: default_endian) ⇒ Object
unpack as many instances of Type are present in the supplied string
77 78 79 80 81 82 83 84 |
# File 'lib/ctypes/type.rb', line 77 def unpack_all(buf, endian: default_endian) out = [] until buf.empty? t, buf = unpack_one(buf, endian:) out << t end out end |
#unpack_one(buf, endian: default_endian) ⇒ Array(Object, ::String)
convert a String containing the binary represention of a c type into the equivalent ruby type
68 69 70 |
# File 'lib/ctypes/type.rb', line 68 def unpack_one(buf, endian: default_endian) raise NotImplementedError end |
#with_endian(value) ⇒ Type
get a fixed-endian instance of this type.
If a type has a fixed endian, it will override the default endian set with CTypes.default_endian=.
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ctypes/type.rb', line 149 def with_endian(value) return self if value == @endian endian = Endian[value] @with_endian ||= {} @with_endian[endian] ||= begin o = clone o.instance_variable_set(:@without_endian, self) unless @endian o.instance_variable_set(:@endian, endian) o end end |
#without_endian ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/ctypes/type.rb', line 162 def without_endian @without_endian ||= begin o = clone o.remove_instance_variable(:@endian) o end end |