ffi-bitfield 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -8
- data/lib/ffi/bit_field.rb +0 -1
- data/lib/ffi/bit_field/layout.rb +29 -0
- data/lib/ffi/bit_field/property.rb +18 -0
- data/lib/ffi/bit_field/version.rb +4 -2
- data/lib/ffi/bit_struct.rb +12 -2
- data/lib/ffi/managed_bit_struct.rb +13 -3
- metadata +3 -2
- data/lib/ffi/bit_field/bit_field_supporter.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d75494c09a1b688169fd2630a7ea299a5c58ad312f9ae369a4c0c11165c46e6
|
4
|
+
data.tar.gz: 60840b874151b38faa6ef56cf6f7f6f179cf2415053edce96f08f32e82874ceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d1d86bda1e5c71a59bc801eb35ee64522bc5d6bd49452afd01583240d4358d370fadc5b3bfa6c619ebb3b65c3cc116d14e149cce2c8977f923365708a8d20bb
|
7
|
+
data.tar.gz: 6c7642a18712a0959273d6a46a5c46b3f6c2bd1b0861cace828f9d6fb717aa76ab3643dc9af509c5478e80e05371609a9a5f871132016f9767b50cb77985c794
|
data/README.md
CHANGED
@@ -61,15 +61,7 @@ require 'ffi/managed_bit_struct'
|
|
61
61
|
```md
|
62
62
|
* module FFI
|
63
63
|
* class BitStruct < FFI::Struct
|
64
|
-
* include BitFieldSupporter
|
65
|
-
|
66
64
|
* class ManagedBitStruct < FFI::ManagedStruct
|
67
|
-
* include BitFieldSupporter
|
68
|
-
|
69
|
-
* module BitField
|
70
|
-
* module BitFieldSupporter
|
71
|
-
* bit_fields
|
72
|
-
* bit_field <- alias of bit_fields
|
73
65
|
```
|
74
66
|
|
75
67
|
## Development
|
data/lib/ffi/bit_field.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FFI
|
4
|
+
module BitField
|
5
|
+
module Layout
|
6
|
+
def bit_fields(*args)
|
7
|
+
# The reason for using class instance variable here instead of
|
8
|
+
# class variable is that sub-class of FFI::Struct cannot be inherited again,
|
9
|
+
# not because class instance variables are clean.
|
10
|
+
@bit_field_hash_table = {} unless instance_variable_defined?(:@bit_field_hash_table)
|
11
|
+
|
12
|
+
parent_name = args.shift
|
13
|
+
member_names = []
|
14
|
+
widths = []
|
15
|
+
args.each_slice(2) do |name, width|
|
16
|
+
member_names << name
|
17
|
+
widths << width
|
18
|
+
end
|
19
|
+
starts = widths.inject([0]) do |result, width|
|
20
|
+
result << (result.last + width)
|
21
|
+
end
|
22
|
+
member_names.zip(starts, widths).each do |name, start, width|
|
23
|
+
@bit_field_hash_table[name] = [parent_name, start, width]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
alias bit_field bit_fields
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FFI
|
4
|
+
module BitField
|
5
|
+
module Property
|
6
|
+
def [](member_name)
|
7
|
+
bit_fields = self.class.instance_variable_get(:@bit_field_hash_table)
|
8
|
+
parent_name, start, width = bit_fields[member_name]
|
9
|
+
if parent_name
|
10
|
+
value = get_member_value(parent_name)
|
11
|
+
(value >> start) & ((1 << width) - 1)
|
12
|
+
else
|
13
|
+
get_member_value(member_name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/ffi/bit_struct.rb
CHANGED
@@ -2,10 +2,20 @@
|
|
2
2
|
|
3
3
|
require 'ffi'
|
4
4
|
require_relative 'bit_field/version'
|
5
|
-
require_relative 'bit_field/
|
5
|
+
require_relative 'bit_field/layout'
|
6
|
+
require_relative 'bit_field/property'
|
6
7
|
|
7
8
|
module FFI
|
8
9
|
class BitStruct < Struct
|
9
|
-
|
10
|
+
# [] is defined in FFI::Struct
|
11
|
+
alias get_member_value []
|
12
|
+
extend BitField::Layout
|
13
|
+
# The Property module included in the FFI::ManagedBitStruct class is
|
14
|
+
# * behind the FFI::ManagedBitStruct class, but is
|
15
|
+
# * in FRONT of the FFI::Struct class.
|
16
|
+
# `MStruct.ancestors`
|
17
|
+
# # => [MStruct, FFI::ManagedBitStruct, FFI::BitField::Property, FFI::ManagedStruct, FFI::Struct...]
|
18
|
+
# So you do not need to use `prepend` instead of `include`.
|
19
|
+
include BitField::Property
|
10
20
|
end
|
11
21
|
end
|
@@ -1,11 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ffi'
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
4
|
+
require_relative 'bit_field/version'
|
5
|
+
require_relative 'bit_field/layout'
|
6
|
+
require_relative 'bit_field/property'
|
6
7
|
|
7
8
|
module FFI
|
8
9
|
class ManagedBitStruct < ManagedStruct
|
9
|
-
|
10
|
+
# [] is defined in FFI::Struct
|
11
|
+
alias get_member_value []
|
12
|
+
extend BitField::Layout
|
13
|
+
# The Property module included in the FFI::BitStruct class is
|
14
|
+
# * behind the FFI::BitStruct class, but is
|
15
|
+
# * in FRONT of the FFI::Struct class.
|
16
|
+
# `YourStruct.ancestors`
|
17
|
+
# # => [YourStruct, FFI::BitStruct, FFI::BitField::Property, FFI::Struct...]
|
18
|
+
# So you do not need to use `prepend` instead of `include`.
|
19
|
+
include BitField::Property
|
10
20
|
end
|
11
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kojix2
|
@@ -104,7 +104,8 @@ files:
|
|
104
104
|
- LICENSE.txt
|
105
105
|
- README.md
|
106
106
|
- lib/ffi/bit_field.rb
|
107
|
-
- lib/ffi/bit_field/
|
107
|
+
- lib/ffi/bit_field/layout.rb
|
108
|
+
- lib/ffi/bit_field/property.rb
|
108
109
|
- lib/ffi/bit_field/version.rb
|
109
110
|
- lib/ffi/bit_struct.rb
|
110
111
|
- lib/ffi/managed_bit_struct.rb
|
@@ -1,44 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module FFI
|
4
|
-
module BitField
|
5
|
-
module BitFiledSupporter
|
6
|
-
module BitFieldModule
|
7
|
-
def [](name)
|
8
|
-
bit_fields = self.class.bit_fields_hash_table
|
9
|
-
parent, start, width = bit_fields[name]
|
10
|
-
if parent
|
11
|
-
(super(parent) >> start) & ((1 << width) - 1)
|
12
|
-
else
|
13
|
-
super(name)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
private_constant :BitFieldModule
|
18
|
-
|
19
|
-
attr_reader :bit_fields_hash_table
|
20
|
-
|
21
|
-
def bit_fields(*args)
|
22
|
-
unless instance_variable_defined?(:@bit_fields_hash_table)
|
23
|
-
@bit_fields_hash_table = {}
|
24
|
-
prepend BitFieldModule
|
25
|
-
end
|
26
|
-
|
27
|
-
parent = args.shift
|
28
|
-
labels = []
|
29
|
-
widths = []
|
30
|
-
args.each_slice(2) do |l, w|
|
31
|
-
labels << l
|
32
|
-
widths << w
|
33
|
-
end
|
34
|
-
starts = widths.inject([0]) do |result, w|
|
35
|
-
result << (result.last + w)
|
36
|
-
end
|
37
|
-
labels.zip(starts, widths).each do |l, s, w|
|
38
|
-
@bit_fields_hash_table[l] = [parent, s, w]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
alias bit_field bit_fields
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|