ffi-bit_masks-0.1.1/0000755000004100000410000000000012670546371014272 5ustar www-datawww-dataffi-bit_masks-0.1.1/Rakefile0000644000004100000410000000051112670546371015734 0ustar www-datawww-data# encoding: utf-8 begin require 'bundler/setup' rescue LoadError => error abort error.message end require 'rake' require 'rubygems/tasks' Gem::Tasks.new require 'rspec/core/rake_task' RSpec::Core::RakeTask.new task :test => :spec task :default => :spec require 'yard' YARD::Rake::YardocTask.new task :doc => :yard ffi-bit_masks-0.1.1/Gemfile0000644000004100000410000000012312670546371015561 0ustar www-datawww-datasource 'https://rubygems.org' gemspec group :development do gem 'kramdown' end ffi-bit_masks-0.1.1/ChangeLog.md0000644000004100000410000000037112670546371016444 0ustar www-datawww-data### 0.1.1 / 2016-03-08 * Added {FFI::BitMasks::BitMask#reference_required?} needed by JRuby. (@mvz) ### 0.1.0 / 2013-08-27 * Initial release: * Can map a Hash of flags to their bitmask value. * Can map an Integer bitmask to a Hash of flags. ffi-bit_masks-0.1.1/.rspec0000644000004100000410000000004012670546371015401 0ustar www-datawww-data--colour --format documentation ffi-bit_masks-0.1.1/LICENSE.txt0000644000004100000410000000204512670546371016116 0ustar www-datawww-dataCopyright (c) 2012-2013 Hal Brodigan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ffi-bit_masks-0.1.1/spec/0000755000004100000410000000000012670546371015224 5ustar www-datawww-dataffi-bit_masks-0.1.1/spec/spec_helper.rb0000644000004100000410000000010712670546371020040 0ustar www-datawww-datarequire 'rspec' require 'ffi/bit_masks/version' include FFI::BitMasks ffi-bit_masks-0.1.1/spec/bit_masks_spec.rb0000644000004100000410000000125012670546371020535 0ustar www-datawww-datarequire 'spec_helper' require 'ffi/bit_masks' describe FFI::BitMasks do it "should have a VERSION constant" do expect(subject.const_get('VERSION')).not_to be_empty end describe "#bit_mask" do subject do mod = Module.new mod.extend FFI::Library mod.extend FFI::BitMasks end let(:flags) { {:foo => 0x1, :bar => 0x2, :baz => 0x4} } it "should return a BitMask object" do expect(subject.bit_mask(:flags,flags).flags).to eq(flags) end it "should define a typedef for the bitmask" do bitmask = subject.bit_mask(:flags,flags) expect(subject.find_type(:flags)).to be_kind_of(FFI::Type::Mapped) end end end ffi-bit_masks-0.1.1/spec/bit_mask_spec.rb0000644000004100000410000000761312670546371020363 0ustar www-datawww-datarequire 'spec_helper' require 'ffi/bit_masks/bit_mask' describe FFI::BitMasks::BitMask do let(:flags) { {:foo => 0x1, :bar => 0x2, :baz => 0x4} } subject { described_class.new(flags) } describe "#initialize" do subject { described_class.new(flags) } it "should initialize the flags" do expect(subject.flags).to eq(flags) end it "should invert the flags into bitmasks" do expect(subject.bitmasks).to eq(flags.invert) end it "should default type to uint" do expect(subject.native_type).to eq(FFI::Type::UINT) end context "when given a custom type" do subject { described_class.new(flags,:ushort) } it "should set native type" do expect(subject.native_type).to eq(FFI::Type::USHORT) end end end describe "#symbols" do it "should return the names of the flags" do expect(subject.symbols).to eq(flags.keys) end end describe "#symbol_map" do it "should return the flags" do expect(subject.symbol_map).to eq(flags) end end describe "#to_h" do it "should return the flags" do expect(subject.to_h).to eq(flags) end end describe "#to_hash" do it "should return the flags" do expect(subject.to_hash).to eq(flags) end end describe "#[]" do context "when given a Symbol" do it "should lookup the bitmask" do expect(subject[:bar]).to eq(flags[:bar]) end end context "when given an Integer" do it "should lookup the flag" do expect(subject[flags[:bar]]).to eq(:bar) end end context "otherwise" do it "should return nil" do expect(subject[Object.new]).to be_nil end end end describe "#to_native" do context "when given a Hash" do let(:hash) { {:foo => true, :bar => true, :baz => false} } it "should bitwise or together the flag masks" do expect(subject.to_native(hash)).to eq(flags[:foo] | flags[:bar]) end context "when one of the keys does not correspond to a flag" do let(:hash) { {:foo => true, :bug => true, :baz => true} } it "should ignore the key" do expect(subject.to_native(hash)).to eq(flags[:foo] | flags[:baz]) end end end context "when an Object that respnds to #to_int" do let(:int) { 0x3 } it "should call #to_int" do expect(subject.to_native(int)).to eq(0x3) end context "when given a bitmask that contains unknown masks" do let(:int) { flags[:foo] | flags[:bar] | 0x8 | 0x10 } it "should filter out the unknown masks" do expect(subject.to_native(int)).to eq( flags[:foo] | flags[:bar] ) end end end context "when given an Object that does not respond to #to_int" do it "should raise an ArgumentError" do expect { subject.to_native(Object.new) }.to raise_error(ArgumentError) end end end describe "#from_native" do let(:value) { flags[:foo] | flags[:baz] } it "should set the flags from the value" do expect(subject.from_native(value)).to eq({ :foo => true, :bar => false, :baz => true }) end context "when one flag is a combination of other flags" do let(:flags) { {:foo => 0x1, :bar => 0x2, :baz => 0x3} } let(:value) { flags[:foo] | flags[:bar] } it "should set all flags whose bitmasks are present" do expect(subject.from_native(value)).to eq({ :foo => true, :bar => true, :baz => true }) end end context "when given a value that contains unknown masks" do let(:value) { flags[:foo] | flags[:baz] | 0x8 | 0x10 } it "should ignore the unknown flags" do expect(subject.from_native(value)).to eq({ :foo => true, :bar => false, :baz => true }) end end end end ffi-bit_masks-0.1.1/.travis.yml0000644000004100000410000000011612670546371016401 0ustar www-datawww-datalanguage: ruby rvm: - 1.9.3 - 2.3.0 - jruby - rbx script: rake spec ffi-bit_masks-0.1.1/lib/0000755000004100000410000000000012670546371015040 5ustar www-datawww-dataffi-bit_masks-0.1.1/lib/ffi/0000755000004100000410000000000012670546371015604 5ustar www-datawww-dataffi-bit_masks-0.1.1/lib/ffi/bit_masks/0000755000004100000410000000000012670546371017560 5ustar www-datawww-dataffi-bit_masks-0.1.1/lib/ffi/bit_masks/version.rb0000644000004100000410000000013112670546371021565 0ustar www-datawww-datamodule FFI module BitMasks # ffi-bit_masks version VERSION = "0.1.1" end end ffi-bit_masks-0.1.1/lib/ffi/bit_masks/bit_mask.rb0000644000004100000410000000756312670546371021711 0ustar www-datawww-datarequire 'ffi' module FFI module BitMasks # # The bitmask data converter. # class BitMask include DataConverter # # Flags of the bitmask. # # @return [Hash{Symbol => Integer}] # The mapping of bit-flags to bitmasks. # attr_reader :flags # # The masks of the bitmask. # # @return [Hash{Integer => Symbol}] # The mapping of bitmasks to bit-flags. # attr_reader :bitmasks # # The underlying native type. # # @return [FFI::Type] # The FFI primitive. # attr_reader :native_type # # Initializes a new bitmask. # # @param [Hash{Symbol => Integer}] flags # The flags and their masks. # # @param [Symbol] type # The underlying type. # def initialize(flags,type=:uint) @flags = flags @bitmasks = flags.invert @native_type = FFI.find_type(type) end # # The Symbols that can be passed to the data converter. # # @return [Array] # The Array of Symbols. # # @note For compatibility with `FFI::Enum`. # def symbols @flags.keys end # # The mapping of acceptable Symbols to their Integer equivalents. # # @return [Hash{Symbol => Integer}] # The mapping of Symbols. # # @note For compatibility with `FFI::Enum`. # def symbol_map @flags end # # @note For compatibility with `FFI::Enum`. # alias to_h symbol_map # # @note For compatibility with `FFI::Enum`. # alias to_hash symbol_map # # Maps flags to masks and vice versa. # # @overload [](query) # # @param [Symbol] query # The flag name. # # @return [Integer] # The mask for the flag. # # @overload [](query) # # @param [Integer] query # The mask. # # @return [Symbol] # The flag for the mask. # def [](query) case query when Symbol then @flags[query] when Integer then @bitmasks[query] end end # # @note For compatibility with `FFI::Enum`. # alias find [] # # Converts flags to a bitmask. # # @overload to_native(value) # # @param [Hash{Symbol => Boolean}] value # The flags and their values. # # @return [Integer] # The bitmask for the given flags. # # @overload to_native(value) # # @param [#to_int] value # The raw bitmask. # # @return [Integer] # The bitmask. # # @raise [ArgumentError] # def to_native(value,ctx=nil) uint = 0 case value when Hash uint = 0 value.each do |flag,value| if (@flags.has_key?(flag) && value) uint |= @flags[flag] end end return uint else if value.respond_to?(:to_int) int = value.to_int @bitmasks.each_key do |mask| uint |= (int & mask) end return uint else raise(ArgumentError,"invalid bitmask value #{value.inspect}") end end end # # Converts a bitmask into multiple flags. # # @param [Integer] value # The raw bitmask. # # @return [Hash{Symbol => Boolean}] # The flags for the bitmask. # def from_native(value,ctx=nil) flags = {} @flags.each do |flag,bitmask| flags[flag] ||= ((value & bitmask) == bitmask) end return flags end def reference_required? false end end end end ffi-bit_masks-0.1.1/lib/ffi/bit_masks.rb0000644000004100000410000000113512670546371020105 0ustar www-datawww-datarequire 'ffi/bit_masks/bit_mask' require 'ffi/bit_masks/version' module FFI # # Adds bitmask types to FFI libraries. # module BitMasks # # Defines a new bitmask. # # @param [Symbol] name # The name of the bitmask. # # @param [Hash{Symbol => Integer}] flags # The flags and their masks. # # @param [Symbol] type # The underlying type. # # @return [BitMask] # The new bitmask. # def bit_mask(name,flags,type=:uint) bit_mask = BitMask.new(flags,type) typedef(bit_mask,name) return bit_mask end end end ffi-bit_masks-0.1.1/ffi-bit_masks.gemspec0000644000004100000410000000376212670546371020365 0ustar www-datawww-data# encoding: utf-8 require 'yaml' Gem::Specification.new do |gem| gemspec = YAML.load_file('gemspec.yml') gem.name = gemspec.fetch('name') gem.version = gemspec.fetch('version') do lib_dir = File.join(File.dirname(__FILE__),'lib') $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir) require 'ffi/bit_masks/version' FFI::BitMasks::VERSION end gem.summary = gemspec['summary'] gem.description = gemspec['description'] gem.licenses = Array(gemspec['license']) gem.authors = Array(gemspec['authors']) gem.email = gemspec['email'] gem.homepage = gemspec['homepage'] glob = lambda { |patterns| gem.files & Dir[*patterns] } gem.files = `git ls-files`.split($/) gem.files = glob[gemspec['files']] if gemspec['files'] gem.executables = gemspec.fetch('executables') do glob['bin/*'].map { |path| File.basename(path) } end gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.' gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb'] gem.test_files = glob[gemspec['test_files'] || '{test/{**/}*_test.rb'] gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}'] gem.require_paths = Array(gemspec.fetch('require_paths') { %w[ext lib].select { |dir| File.directory?(dir) } }) gem.requirements = gemspec['requirements'] gem.required_ruby_version = gemspec['required_ruby_version'] gem.required_rubygems_version = gemspec['required_rubygems_version'] gem.post_install_message = gemspec['post_install_message'] split = lambda { |string| string.split(/,\s*/) } if gemspec['dependencies'] gemspec['dependencies'].each do |name,versions| gem.add_dependency(name,split[versions]) end end if gemspec['development_dependencies'] gemspec['development_dependencies'].each do |name,versions| gem.add_development_dependency(name,split[versions]) end end end ffi-bit_masks-0.1.1/gemspec.yml0000644000004100000410000000063312670546371016442 0ustar www-datawww-dataname: ffi-bit_masks summary: Adds support for bit-masked types in FFI. description: FFI plugin which adds support for bitmasked types (or flags) to FFI. license: MIT authors: Postmodern email: postmodern.mod3@gmail.com homepage: https://github.com/postmodern/ffi-bit_masks#readme dependencies: ffi: ~> 1.0 development_dependencies: rspec: ~> 3.0 rubygems-tasks: ~> 0.2 yard: ~> 0.8 rake: ~> 10.4 ffi-bit_masks-0.1.1/.gitignore0000644000004100000410000000002712670546371016261 0ustar www-datawww-datadoc/ pkg/ Gemfile.lock ffi-bit_masks-0.1.1/.yardopts0000644000004100000410000000010412670546371016133 0ustar www-datawww-data--markup markdown --title "ffi-bit_masks Documentation" --protected ffi-bit_masks-0.1.1/README.md0000644000004100000410000000160312670546371015551 0ustar www-datawww-data# ffi-bit_masks * [Homepage](https://github.com/postmodern/ffi-bit_masks#readme) * [Issues](https://github.com/postmodern/ffi-bit_masks/issues) * [Documentation](http://rubydoc.info/gems/ffi-bit_masks/frames) * [Email](mailto:postmodern.mod3 at gmail.com) ## Description FFI plugin which adds support for bitmasked types (or flags) to FFI. ## Features * Can map a Hash of flags to their bitmask value. * Can map an Integer bitmask to a Hash of flags. ## Examples require 'ffi/bit_masks' module MyLibrary extend FFI::Library ffi_lib 'foo' bit_mask :flags, {foo: 0x1, bar: 0x2, baz: 0x4} attach_function :my_func, [:pointer, :size_t, :flags], :int end ## Requirements * [ffi](https://github.com/ffi/ffi#readme) ~> 1.0 ## Install $ gem install ffi-bit_masks ## Copyright Copyright (c) 2012-2013 Hal Brodigan See {file:LICENSE.txt} for details. ffi-bit_masks-0.1.1/.document0000644000004100000410000000003412670546371016106 0ustar www-datawww-data- ChangeLog.md LICENSE.txt