ffi-bitfield 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a7ec1a2c9da825cb036e42ad7b0e3745704e5bb9fba89394993c3043da9e521
|
4
|
+
data.tar.gz: 10d2ecd8efcb3312228d684b0e15ccc32654de39dc4399065267ad5866e2d0cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 688c1d48d37dc1d9f619af9da8ee5d8ac5546d8b6dcab618db5df90d603c73a4c80aac58dfc91b385da9f4e8ea90f2442e47d73441564f5e75e579d7b2636b4c
|
7
|
+
data.tar.gz: 230ed815df33fe597fdf4fa9dc61afd6b69fa625de30826cb795db58aeebeb31ffecf68ebceed6b764450169dee91949ea86006ee4238d6e058e93ad84ed0b9d
|
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
module FFI
|
4
4
|
module BitField
|
5
|
-
#
|
5
|
+
# ClassMethods provides methods for defining bit field layouts.
|
6
6
|
# This module is extended by BitStruct and ManagedBitStruct classes.
|
7
|
-
module
|
7
|
+
module ClassMethods
|
8
8
|
# Returns a hash of bit fields grouped by parent field.
|
9
9
|
#
|
10
10
|
# @return [Hash] A hash where keys are parent field names and values are arrays of bit field names
|
@@ -71,6 +71,54 @@ module FFI
|
|
71
71
|
result
|
72
72
|
end
|
73
73
|
|
74
|
+
# Returns a hash of bit fields with their bit offsets, grouped by parent field.
|
75
|
+
#
|
76
|
+
# @return [Hash] A hash where keys are parent field names and values are arrays of [bit_field_name, bit_offset] pairs
|
77
|
+
#
|
78
|
+
# @example Get bit field offsets in a struct
|
79
|
+
# class Flags < FFI::BitStruct
|
80
|
+
# layout \
|
81
|
+
# :value, :uint8
|
82
|
+
#
|
83
|
+
# bit_fields :value,
|
84
|
+
# :read, 1,
|
85
|
+
# :write, 1,
|
86
|
+
# :execute, 1,
|
87
|
+
# :unused, 5
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# Flags.bit_field_offsets
|
91
|
+
# # => {
|
92
|
+
# # :value => [[:read, 0], [:write, 1], [:execute, 2], [:unused, 3]]
|
93
|
+
# # }
|
94
|
+
def bit_field_offsets
|
95
|
+
return {} unless instance_variable_defined?(:@bit_field_hash_table)
|
96
|
+
|
97
|
+
result = {}
|
98
|
+
|
99
|
+
# Get byte offsets of parent fields
|
100
|
+
field_offsets = offsets.to_h
|
101
|
+
|
102
|
+
# Process each bit field
|
103
|
+
@bit_field_hash_table.each do |field_name, info|
|
104
|
+
parent_name, start, _width = info
|
105
|
+
|
106
|
+
# Get byte offset of parent field
|
107
|
+
parent_offset = field_offsets[parent_name]
|
108
|
+
next unless parent_offset
|
109
|
+
|
110
|
+
# Convert byte offset to bit offset and add bit field's start position
|
111
|
+
bit_offset = parent_offset * 8 + start
|
112
|
+
|
113
|
+
# Add to result
|
114
|
+
result[parent_name] ||= []
|
115
|
+
result[parent_name] << [field_name, bit_offset]
|
116
|
+
end
|
117
|
+
|
118
|
+
# Return result
|
119
|
+
result
|
120
|
+
end
|
121
|
+
|
74
122
|
# Defines bit fields within a parent field.
|
75
123
|
#
|
76
124
|
# @param [Array] layout_args An array where the first element is the parent field name,
|
@@ -2,9 +2,35 @@
|
|
2
2
|
|
3
3
|
module FFI
|
4
4
|
module BitField
|
5
|
-
#
|
5
|
+
# InstanceMethods provides methods for reading and writing bit field values.
|
6
6
|
# This module is included in BitStruct and ManagedBitStruct classes.
|
7
|
-
module
|
7
|
+
module InstanceMethods
|
8
|
+
# Returns a hash of bit fields grouped by parent field.
|
9
|
+
# Instance method version of the class method with the same name.
|
10
|
+
#
|
11
|
+
# @return [Hash] A hash where keys are parent field names and values are arrays of bit field names
|
12
|
+
#
|
13
|
+
# @example Get bit field members in a struct instance
|
14
|
+
# flags = Flags.new
|
15
|
+
# flags.bit_field_members # => {:value => [:read, :write, :execute, :unused]}
|
16
|
+
def bit_field_members
|
17
|
+
self.class.bit_field_members
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns a hash of bit fields with their bit offsets, grouped by parent field.
|
21
|
+
#
|
22
|
+
# @return [Hash] A hash where keys are parent field names and values are arrays of [bit_field_name, bit_offset] pairs
|
23
|
+
#
|
24
|
+
# @example Get bit field offsets in a struct instance
|
25
|
+
# flags = Flags.new
|
26
|
+
# flags.bit_field_offsets
|
27
|
+
# # => {
|
28
|
+
# # :value => [[:read, 0], [:write, 1], [:execute, 2], [:unused, 3]]
|
29
|
+
# # }
|
30
|
+
def bit_field_offsets
|
31
|
+
self.class.bit_field_offsets
|
32
|
+
end
|
33
|
+
|
8
34
|
# Reads a value from a bit field or regular field.
|
9
35
|
#
|
10
36
|
# @param [Symbol] member_name The name of the field to read
|
data/lib/ffi/bit_struct.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'ffi'
|
4
4
|
require_relative 'bit_field/version'
|
5
|
-
require_relative 'bit_field/
|
6
|
-
require_relative 'bit_field/
|
5
|
+
require_relative 'bit_field/class_methods'
|
6
|
+
require_relative 'bit_field/instance_methods'
|
7
7
|
|
8
8
|
module FFI
|
9
9
|
# Subclass of FFI::Struct that supports bit fields.
|
@@ -29,13 +29,13 @@ module FFI
|
|
29
29
|
# [] is defined in FFI::Struct
|
30
30
|
alias get_member_value []
|
31
31
|
alias set_member_value []=
|
32
|
-
extend BitField::
|
33
|
-
# The
|
32
|
+
extend BitField::ClassMethods
|
33
|
+
# The InstanceMethods module included in the FFI::ManagedBitStruct class is
|
34
34
|
# * behind the FFI::ManagedBitStruct class, but is
|
35
35
|
# * in FRONT of the FFI::Struct class.
|
36
36
|
# `MStruct.ancestors`
|
37
|
-
# # => [MStruct, FFI::ManagedBitStruct, FFI::BitField::
|
37
|
+
# # => [MStruct, FFI::ManagedBitStruct, FFI::BitField::InstanceMethods, FFI::ManagedStruct, FFI::Struct...]
|
38
38
|
# So you do not need to use `prepend` instead of `include`.
|
39
|
-
include BitField::
|
39
|
+
include BitField::InstanceMethods
|
40
40
|
end
|
41
41
|
end
|
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'ffi'
|
4
4
|
require_relative 'bit_field/version'
|
5
|
-
require_relative 'bit_field/
|
6
|
-
require_relative 'bit_field/
|
5
|
+
require_relative 'bit_field/class_methods'
|
6
|
+
require_relative 'bit_field/instance_methods'
|
7
7
|
|
8
8
|
module FFI
|
9
9
|
# Subclass of FFI::ManagedStruct that supports bit fields.
|
@@ -32,13 +32,13 @@ module FFI
|
|
32
32
|
# [] is defined in FFI::Struct
|
33
33
|
alias get_member_value []
|
34
34
|
alias set_member_value []=
|
35
|
-
extend BitField::
|
36
|
-
# The
|
35
|
+
extend BitField::ClassMethods
|
36
|
+
# The InstanceMethods module included in the FFI::BitStruct class is
|
37
37
|
# * behind the FFI::BitStruct class, but is
|
38
38
|
# * in FRONT of the FFI::Struct class.
|
39
39
|
# `YourStruct.ancestors`
|
40
|
-
# # => [YourStruct, FFI::BitStruct, FFI::BitField::
|
40
|
+
# # => [YourStruct, FFI::BitStruct, FFI::BitField::InstanceMethods, FFI::Struct...]
|
41
41
|
# So you do not need to use `prepend` instead of `include`.
|
42
|
-
include BitField::
|
42
|
+
include BitField::InstanceMethods
|
43
43
|
end
|
44
44
|
end
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ffi-bitfield
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kojix2
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date: 2025-04-
|
10
|
+
date: 2025-04-12 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: ffi
|
@@ -34,8 +33,8 @@ files:
|
|
34
33
|
- LICENSE.txt
|
35
34
|
- README.md
|
36
35
|
- lib/ffi/bit_field.rb
|
37
|
-
- lib/ffi/bit_field/
|
38
|
-
- lib/ffi/bit_field/
|
36
|
+
- lib/ffi/bit_field/class_methods.rb
|
37
|
+
- lib/ffi/bit_field/instance_methods.rb
|
39
38
|
- lib/ffi/bit_field/version.rb
|
40
39
|
- lib/ffi/bit_struct.rb
|
41
40
|
- lib/ffi/managed_bit_struct.rb
|
@@ -43,7 +42,6 @@ homepage: https://github.com/kojix2/ffi-bitfield
|
|
43
42
|
licenses:
|
44
43
|
- MIT
|
45
44
|
metadata: {}
|
46
|
-
post_install_message:
|
47
45
|
rdoc_options: []
|
48
46
|
require_paths:
|
49
47
|
- lib
|
@@ -58,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
56
|
- !ruby/object:Gem::Version
|
59
57
|
version: '0'
|
60
58
|
requirements: []
|
61
|
-
rubygems_version: 3.
|
62
|
-
signing_key:
|
59
|
+
rubygems_version: 3.6.2
|
63
60
|
specification_version: 4
|
64
61
|
summary: bit fields for Ruby-FFI
|
65
62
|
test_files: []
|