Module: Assimp::StructAccessors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



15
16
17
# File 'lib/assimp/base.rb', line 15

def self.extended(mod)
  mod.instance_variable_set(:@__has_ref, false)
end

Instance Method Details

#has_ref?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/assimp/base.rb', line 19

def has_ref?
  @__has_ref
end

#struct_array_attr_accessor(*args) ⇒ Object



152
153
154
155
156
# File 'lib/assimp/base.rb', line 152

def struct_array_attr_accessor(*args)
  struct_array_attr_reader(*args)
  struct_array_attr_writer(*args)
  struct_array_attr_checker(*args)
end

#struct_array_attr_checker(*args) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/assimp/base.rb', line 143

def struct_array_attr_checker(*args)
  args.each { |attr, klass, count|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    define_method(:"#{attr}?") do
      ! self[attr].null?
    end
  }
end

#struct_array_attr_reader(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/assimp/base.rb', line 53

def struct_array_attr_reader(*args)
  args.each { |attr, klass, count|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    t = nil
    s = nil
    if klass.kind_of? Symbol
      t = Assimp::find_type(klass)
      s = t.size
      define_method(attr) do
        n = ( count ? self[count] : self[:"num_#{attr}"] )
        p = self[attr]
        if n == 0 || p.null?
          []
        else
          n.times.collect { |i|
            p.get(t, i*s) 
          }
        end
      end
    elsif klass.kind_of?(Class) && klass < FFI::Struct
      s = klass.size
      define_method(attr) do
        n = ( count ? self[count] : self[:"num_#{attr}"] )
        p = self[attr]
        if n == 0 || p.null?
          []
        else
          n.times.collect { |i|
            klass.new(p+i*s)
          }
        end
      end
    else
      raise "Invalid type: #{klass.inspect} for #{attr.inspect}!"
    end
  }
end

#struct_array_attr_writer(*args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/assimp/base.rb', line 91

def struct_array_attr_writer(*args)
  args.each { |attr, klass, count|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    @__has_ref = true
    t = nil
    s = nil
    if klass.kind_of? Symbol
      t = Assimp::find_type(klass)
      s = t.size
      define_method(:"#{attr}=") do |values|
        values = [] if values.nil?
        if count
          self[count] = values.length
        else
          self[:"num_#{attr}"] = values.length
        end
        ptr = (values.length == 0 ? nil : FFI::MemoryPointer::new(t, values.length))
        values.each_with_index { |v, i|
          ptr.put(t, i*s, v)
        }
        self.instance_variable_set(:"@#{attr}", ptr)
        self[attr] = ptr
        values
      end
    elsif klass.kind_of?(Class) && klass < FFI::Struct
      s = klass.size
      k = klass
      define_method(:"#{attr}=") do |values|
        values = [] if values.nil?
        if count
          self[count] = values.length
        else
          self[:"num_#{attr}"] = values.length
        end
        ptr = (values.length == 0 ? nil : FFI::MemoryPointer::new(klass, values.length))
        values.each_with_index { |v, i|
          ptr.put_array_of_uint8(i*s, v.pointer.read_array_of_uint8(s))
        }
        if k.has_ref?
          self.instance_variable_set(:"@#{attr}", [ptr, values])
        else
          self.instance_variable_set(:"@#{attr}", ptr)
        end
        self[attr] = ptr
        values
      end
    else
      raise "Invalid type: #{klass.inspect} for #{attr.inspect}!"
    end
  }
end

#struct_attr_accessor(*args) ⇒ Object



48
49
50
51
# File 'lib/assimp/base.rb', line 48

def struct_attr_accessor(*args)
  struct_attr_reader(*args)
  struct_attr_writer(*args)
end

#struct_attr_reader(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/assimp/base.rb', line 23

def struct_attr_reader(*args)
  args.each { |attr|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    if @layout[attr].type.kind_of?( FFI::StructByValue ) && @layout[attr].type.struct_class == Assimp::String
      define_method(attr) { self[attr].data }
    else
      define_method(attr) { self[attr] }
    end
  }
end

#struct_attr_writer(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/assimp/base.rb', line 34

def struct_attr_writer(*args)
  args.each { |attr|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    if @layout[attr].type.kind_of?( FFI::StructByValue ) && @layout[attr].type.struct_class == Assimp::String
      define_method(attr.to_s+"=") { |o| self[attr].data = o }
    elsif @layout[attr].type.kind_of?( FFI::StructByValue ) && @layout[attr].type.struct_class.has_ref?
      @__has_ref = true
      define_method(attr.to_s+"=") { |o| self.instance_variable_set(:"@#{attr}", o); self[attr] = o }
    else
      define_method(attr.to_s+"=") { |o| self[attr] = o }
    end
  }
end

#struct_ref_array_attr_accessor(*args) ⇒ Object



194
195
196
197
198
# File 'lib/assimp/base.rb', line 194

def struct_ref_array_attr_accessor(*args)
  struct_ref_array_attr_reader(*args)
  struct_ref_array_attr_writer(*args)
  struct_array_attr_checker(*args)
end

#struct_ref_array_attr_reader(*args) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/assimp/base.rb', line 158

def struct_ref_array_attr_reader(*args)
  args.each { |attr, klass, count|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    define_method(attr) do
      n = ( count ? self[count] : self[:"num_#{attr}"] )
      p = self[attr]
      if n == 0 || p.null?
        []
      else
        ptrs = p.read_array_of_pointer(self[:"num_#{attr}"])
        ptrs.collect { |ptr| ptr.null? ? nil : klass::new(ptr) }
      end
    end
  }
end

#struct_ref_array_attr_writer(*args) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/assimp/base.rb', line 174

def struct_ref_array_attr_writer(*args)
  args.each { |attr, klass, count|
    raise "Invalid attribute #{attr.inspect}!" unless @layout.members.include?(attr)
    @__has_ref = true
    define_method(:"#{attr}=") do |values|
      values = [] if values.nil?
      if count
        self[count] = values.length
      else
        self[:"num_#{attr}"] = values.length
      end
      ptr = (values.length == 0 ? nil : FFI::MemoryPointer::new(:pointer, values.length))
      ptr.write_array_of_pointer(values.collect(&:pointer)) if ptr
      self.instance_variable_set(:"@#{attr}", [ptr, values.dup])
      self[attr] = ptr
      values
    end
  }
end