Module: GRCommons::GRCommonUtils

Included in:
GR, GR3, GRM
Defined in:
lib/gr_commons/gr_common_utils.rb

Overview

This module provides functionality common to GR and GR3.

Constant Summary collapse

SUPPORTED_TYPES =

This constants is used in the test.

%i[uint8 uint16 int uint double float].freeze

Class Method Summary collapse

Class Method Details

.create_ffi_pointer(type) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/gr_commons/gr_common_utils.rb', line 104

def create_ffi_pointer(type)
  case type
  when Hash
    typ = type.keys[0]
    len = type.values[0]
    Fiddley::MemoryPointer.new(typ, len)
  else
    Fiddley::MemoryPointer.new(type)
  end
end

.double(data) ⇒ Object

convert Ruby Array or NArray into packed string.



60
61
62
63
64
65
66
# File 'lib/gr_commons/gr_common_utils.rb', line 60

def double(data)
  if narray?(data)
    Numo::DFloat.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:double, data.to_a.flatten)
  end
end

.equal_length(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/gr_commons/gr_common_utils.rb', line 10

def equal_length(*args)
  lengths = args.map(&:length)
  unless lengths.all? { |l| l == lengths[0] }
    raise ArgumentError,
          'Sequences must have same length.'
  end

  lengths[0]
end

.float(data) ⇒ Object

convert Ruby Array or NArray into packed string.



69
70
71
72
73
74
75
# File 'lib/gr_commons/gr_common_utils.rb', line 69

def float(data)
  if narray?(data)
    Numo::SFloat.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:float, data.to_a.flatten)
  end
end

.inquiry(types) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gr_commons/gr_common_utils.rb', line 89

def inquiry(types)
  case types
  when Hash, Symbol
    ptr = create_ffi_pointer(types)
    yield(ptr)
    read_ffi_pointer(ptr, types)
  when Array
    pts = types.map { |type| create_ffi_pointer(type) }
    yield(*pts)
    pts.zip(types).map { |pt, type| read_ffi_pointer(pt, type) }
  else
    raise ArgumentError
  end
end

.inquiry_double(&block) ⇒ Object



85
86
87
# File 'lib/gr_commons/gr_common_utils.rb', line 85

def inquiry_double(&block)
  inquiry(:double, &block)
end

.inquiry_int(&block) ⇒ Object



77
78
79
# File 'lib/gr_commons/gr_common_utils.rb', line 77

def inquiry_int(&block)
  inquiry(:int, &block)
end

.inquiry_uint(&block) ⇒ Object



81
82
83
# File 'lib/gr_commons/gr_common_utils.rb', line 81

def inquiry_uint(&block)
  inquiry(:uint, &block)
end

.int(data) ⇒ Object

convert Ruby Array or NArray into packed string.



42
43
44
45
46
47
48
# File 'lib/gr_commons/gr_common_utils.rb', line 42

def int(data)
  if narray?(data)
    Numo::Int32.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:int32, data.to_a.flatten)
  end
end

.narray?(data) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/gr_commons/gr_common_utils.rb', line 126

def narray?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
end

.read_ffi_pointer(pt, type) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/gr_commons/gr_common_utils.rb', line 115

def read_ffi_pointer(pt, type)
  case type
  when Hash
    typ = type.keys[0]
    len = type.values[0]
    pt.public_send("read_array_of_#{typ}", len)
  else
    pt.public_send("read_#{type}")
  end
end

.uint(data) ⇒ Object

convert Ruby Array or NArray into packed string.



51
52
53
54
55
56
57
# File 'lib/gr_commons/gr_common_utils.rb', line 51

def uint(data)
  if narray?(data)
    Numo::UInt32.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint32, data.to_a.flatten)
  end
end

.uint16(data) ⇒ Object

convert Ruby Array or NArray into packed string.



33
34
35
36
37
38
39
# File 'lib/gr_commons/gr_common_utils.rb', line 33

def uint16(data)
  if narray?(data)
    Numo::UInt16.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint16, data.to_a.flatten)
  end
end

.uint8(data) ⇒ Object

convert Ruby Array or NArray into packed string.



24
25
26
27
28
29
30
# File 'lib/gr_commons/gr_common_utils.rb', line 24

def uint8(data)
  if narray?(data)
    Numo::UInt8.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint8, data.to_a.flatten)
  end
end