Module: SGC::Helper::FlagsValue

Included in:
CU::API::Enum, Cuda::API::Enum
Defined in:
lib/helpers/flags.rb

Overview

Provide methods for evaluating the composite value of flags. self which include/extend this module should implement IEnum interface.

Defined Under Namespace

Modules: Pvt

Instance Method Summary (collapse)

Instance Method Details

- (Integer) value(*flags)

Returns The composite value of the flags with respect to self.

Examples:

Compute the composite value of flags with multiple symbols and integers.

CUEventFlags.symbols            #=> [:DEFAULT, :BLOCKING_SYNC, :DISABLE_TIMING]
CUEventFlags[:DEFAULT]          #=> 0
CUEventFlags[0]                 #=> :DEFAULT
CUEventFlags[:BLOCKING_SYNC]    #=> 1
CUEventFlags[1]                 #=> :BLOCKING_SYNC
CUEventFlags.value(:DISABLE_TIMING)                      #=> 2
CUEventFlags.value(:BLOCKING_SYNC, :DISABLE_TIMING)      #=> 3
CUEventFlags.value([:BLOCKING_SYNC, :DISABLE_TIMING])    #=> 3
CUEventFlags.value([1, :DISABLE_TIMING])                 #=> 3

Parameters:

  • *flags (Symbol, Integer, Array<Symbol, Integer>)

    The list of flags to include in the returning value.

Returns:

  • (Integer)

    The composite value of the flags with respect to self.

Raises:

  • (ArgumentError)

    Invalid symbol or value.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/helpers/flags.rb', line 46

def value(*flags)
    flags.empty? == false or raise ArgumentError, "No flags is provided. Expect Array<Symbol, Integer>, Symbol or Integer."

    f = 0
    flags.flatten.each do |x|
        case x
            when Symbol
                v = self[x] or Pvt::raise_invalid_symbol(x)
                f |= v
            when Integer
                self[x] or Pvt::raise_invalid_value(x)
                f |= x
            else
                raise ArgumentError, "Invalid flags: #{x.to_s}. Expect Symbol or Integer in the flags array."
        end
    end
    f
end