Class: AutoC::Type

Inherits:
Code
  • Object
show all
Defined in:
lib/autoc/type.rb

Direct Known Subclasses

Collection, Reference, UserDefinedType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code

#attach, #priority, #source_size

Constructor Details

#initialize(type, visibility = :public) ⇒ Type

Returns a new instance of Type.



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/autoc/type.rb', line 168

def initialize(type, visibility = :public)
  @type = type.to_s
  @type_ref = "#{self.type}*"
  @visibility = [:public, :private, :static].include?(visibility) ? visibility : raise("unsupported visibility")
  @capability = Set[:constructible, :destructible, :copyable, :comparable, :hashable, :orderable] # Can be used to disable specific capabilities for a type
  # Canonic special method signatures
  @ctor_signature = Function::Signature.new([type^:self])
  @dtor_signature = Function::Signature.new([type^:self])
  @copy_signature = Function::Signature.new([type^:dst, type^:src])
  @equal_signature = Function::Signature.new([type^:lt, type^:rt], :int)
  @identify_signature = Function::Signature.new([type^:self], :size_t)
  @less_signature = Function::Signature.new([type^:lt, type^:rt], :int)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/autoc/type.rb', line 182

def method_missing(method, *args)
  str = method.to_s
  str = str.sub(/[\!\?]$/, '') # Strip trailing ? or !
  fn = prefix + str[0,1].capitalize + str[1..-1] # Ruby 1.8 compatible
  if args.empty?
    fn # Emit bare function name
  elsif args.size == 1 && args.first == nil
    fn + '()' # Use sole nil argument to emit function call with no arguments
  else
    fn + '(' + args.join(',') + ')' # Emit normal function call with supplied arguments
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



160
161
162
# File 'lib/autoc/type.rb', line 160

def type
  @type
end

#type_refObject (readonly)

Returns the value of attribute type_ref.



160
161
162
# File 'lib/autoc/type.rb', line 160

def type_ref
  @type_ref
end

Class Method Details

.coerce(type) ⇒ Object



148
149
150
# File 'lib/autoc/type.rb', line 148

def self.coerce(type)
  type.is_a?(Type) ? type : UserDefinedType.new(type)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



154
# File 'lib/autoc/type.rb', line 154

def ==(other) self.class == other.class && type == other.type end

#abortObject



240
# File 'lib/autoc/type.rb', line 240

def abort; "abort" end

#assertObject



232
# File 'lib/autoc/type.rb', line 232

def assert; "assert" end

#callocObject



236
# File 'lib/autoc/type.rb', line 236

def calloc; "calloc" end

#comparable?Boolean

Returns:

  • (Boolean)


254
# File 'lib/autoc/type.rb', line 254

def comparable?; @capability.include?(:comparable) end

#constructible?Boolean

Returns:

  • (Boolean)


248
# File 'lib/autoc/type.rb', line 248

def constructible?; @capability.include?(:constructible) end

#copyable?Boolean

Returns:

  • (Boolean)


252
# File 'lib/autoc/type.rb', line 252

def copyable?; @capability.include?(:copyable) end

#destructible?Boolean

Returns:

  • (Boolean)


250
# File 'lib/autoc/type.rb', line 250

def destructible?; @capability.include?(:destructible) end

#entitiesObject



158
# File 'lib/autoc/type.rb', line 158

def entities; super << CommonCode end

#externObject

def write_impls(stream, define)



226
# File 'lib/autoc/type.rb', line 226

def extern; "AUTOC_EXTERN" end

#freeObject



238
# File 'lib/autoc/type.rb', line 238

def free; "free" end

#hashObject



152
# File 'lib/autoc/type.rb', line 152

def hash; self.class.hash ^ type.hash end

#hashable?Boolean

Returns:

  • (Boolean)


258
# File 'lib/autoc/type.rb', line 258

def hashable?; @capability.include?(:hashable) && comparable? end

#inlineObject



228
# File 'lib/autoc/type.rb', line 228

def inline; "AUTOC_INLINE" end

#mallocObject



234
# File 'lib/autoc/type.rb', line 234

def malloc; "malloc" end

#orderable?Boolean

Returns:

  • (Boolean)


256
# File 'lib/autoc/type.rb', line 256

def orderable?; @capability.include?(:orderable) && comparable? end

#prefixObject



162
163
164
165
166
# File 'lib/autoc/type.rb', line 162

def prefix
  # Lazy evaluator for simple types like char* which do not actually use
  # this method and hence do not require the prefix to be valid C identifier
  AutoC.c_id(type)
end

#private?Boolean

Returns:

  • (Boolean)


244
# File 'lib/autoc/type.rb', line 244

def private?; @visibility == :private end

#public?Boolean

Returns:

  • (Boolean)


242
# File 'lib/autoc/type.rb', line 242

def public?; @visibility == :public end

#staticObject



230
# File 'lib/autoc/type.rb', line 230

def static; "AUTOC_STATIC" end

#static?Boolean

Returns:

  • (Boolean)


246
# File 'lib/autoc/type.rb', line 246

def static?; @visibility == :static end

#write_decls(stream) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/autoc/type.rb', line 202

def write_decls(stream)
  if private?
    write_intf_types(stream)
    write_intf_decls(stream, extern, inline)
  elsif static?
    write_intf_types(stream)
    write_intf_decls(stream, static, inline)
  end
end

#write_defs(stream) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/autoc/type.rb', line 212

def write_defs(stream)
  if public? || private?
    write_impls(stream, nil)
  elsif static?
    write_impls(stream, static)
  end
end

#write_intf(stream) ⇒ Object



195
196
197
198
199
200
# File 'lib/autoc/type.rb', line 195

def write_intf(stream)
  if public?
    write_intf_types(stream)
    write_intf_decls(stream, extern, inline)
  end
end