ffi-bitfield 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8718e2b995e05ebbbfc068b45acef79a24b371905ec0afc4fe828d608f56a32
4
+ data.tar.gz: e303e6e22a0927d312934fd9085f6b4e7a19ee10361fafe90c465462d8b1de9d
5
+ SHA512:
6
+ metadata.gz: 54b27c548d1f4d1ec0373f7f0d60a6079b9b3c471be0e773aabe8d5c8dfabee6cdaaa6a2a8982fdec4b3d34cc301817b329e32d734187c1b8bb8dfa44cd427cd
7
+ data.tar.gz: 163eb57089a2b6f88ada99a2ff9909a7c7bf34dc1bf56afa1665441de7232117df779107250ea66086024689a32c5fabb8e611c8d53af53552208f6cac9818bb
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 kojix2
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # ffi-bitfield
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ffi-bitfield.svg)](https://badge.fury.io/rb/ffi-bitfield)
4
+ [![test](https://github.com/kojix2/ffi-bitfield/actions/workflows/ci.yml/badge.svg)](https://github.com/kojix2/ffi-bitfield/actions/workflows/ci.yml)
5
+
6
+ Bit field for [Ruby-FFI](https://github.com/ffi/ffi)
7
+
8
+ :construction: alpha ー Supports reading bit fields only.
9
+
10
+ ## Installation
11
+
12
+ ```sh
13
+ gem install ffi-bitfield
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ruby
19
+ require 'ffi/bit_struct'
20
+
21
+ class Struct1 < FFI::BitStruct
22
+ layout \
23
+ :a, :uint8,
24
+ :b, :uint8
25
+
26
+ bit_fields :a,
27
+ :u, 2,
28
+ :v, 2,
29
+ :w, 1,
30
+ :x, 1,
31
+ :y, 1,
32
+ :z, 1
33
+ end
34
+
35
+ s = Struct1.new
36
+ s[:a] = 63
37
+
38
+ p s[:u] # 3
39
+ p s[:v] # 3
40
+ p s[:w] # 1
41
+ p s[:x] # 1
42
+ p s[:y] # 0
43
+ p s[:z] # 0
44
+ ```
45
+
46
+ ### Loading
47
+
48
+ ```ruby
49
+ require 'ffi/bit_field'
50
+ ```
51
+
52
+ The above is the same as below.
53
+
54
+ ```ruby
55
+ require 'ffi/bit_struct'
56
+ require 'ffi/managed_bit_struct'
57
+ ```
58
+
59
+ ## API Overview
60
+
61
+ ```md
62
+ * module FFI
63
+ * class BitStruct < FFI::Struct
64
+ * include BitFieldSupporter
65
+
66
+ * 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
+ ```
74
+
75
+ ## Development
76
+
77
+ ```
78
+ git clone https://github.com/kojix2/ffi-bitfield
79
+ cd ffi-bitfield
80
+ bundle install
81
+ bundle exec rake test
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kojix2/bitstruct.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bit_field/version'
4
+ require_relative 'bit_field/bit_field_supporter'
5
+ require_relative 'bit_struct'
6
+ require_relative 'managed_bit_struct'
7
+
8
+ module FFI
9
+ module BitField
10
+ end
11
+ end
@@ -0,0 +1,44 @@
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
@@ -0,0 +1,5 @@
1
+ module FFI
2
+ module BitField
3
+ VERSION="0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require_relative 'bit_field/version'
5
+ require_relative 'bit_field/bit_field_supporter'
6
+
7
+ module FFI
8
+ class BitStruct < Struct
9
+ extend BitField::BitFiledSupporter
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require_relative 'bit_struct/version'
5
+ require_relative 'bit_field_supporter'
6
+
7
+ module FFI
8
+ class ManagedBitStruct < ManagedStruct
9
+ extend BitFiledSupporter
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-bitfield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - kojix2
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: irb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: bit fields for Ruby-FFI
98
+ email:
99
+ - 2xijok@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE.txt
105
+ - README.md
106
+ - lib/ffi/bit_field.rb
107
+ - lib/ffi/bit_field/bit_field_supporter.rb
108
+ - lib/ffi/bit_field/version.rb
109
+ - lib/ffi/bit_struct.rb
110
+ - lib/ffi/managed_bit_struct.rb
111
+ homepage: https://github.com/kojix2/ffi-bitfield
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '2.4'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.2.15
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: bit fields for Ruby-FFI
134
+ test_files: []