Class: AutoC::Type

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

Direct Known Subclasses

Collection, Reference, String, 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



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

def initialize(type, visibility = :public)
  @type = type.to_s
  @type_ref = "#{self.type}*"
  @visibility = [:public, :private, :static].include?(visibility) ? visibility : raise("unsupported visibility")
  # Canonic special method signatures
  @signature = {
    :ctor => Function::Signature.new([type^:self]),
    :dtor => Function::Signature.new([type^:self]),
    :copy => Function::Signature.new([type^:dst, type^:src]),
    :equal => Function::Signature.new([type^:lt, type^:rt], :int),
    :identify => Function::Signature.new([type^:self], :size_t),
    :less => 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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/autoc/type.rb', line 185

def method_missing(method, *args)
  str = method.to_s
  str = str.sub(/[\!\?]$/, '') # Strip trailing ? or !
  x = false # Have leading underscore
  if /_(.*)/ =~ str
    str = $1
    x = true
  end
  fn = prefix + str[0,1].capitalize + str[1..-1] # Ruby 1.8 compatible
  fn = "_" << fn if x # Carry over the leading underscore
  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



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

def type
  @type
end

#type_refObject (readonly)

Returns the value of attribute type_ref



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

def type_ref
  @type_ref
end

Class Method Details

.coerce(type) ⇒ Object



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

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

Instance Method Details

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



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

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

#abortObject



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

def abort; :abort end

#assertObject



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

def assert; :assert end

#callocObject



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

def calloc; :calloc end

#comparable?Boolean

Returns true if the type provides a well-defined equality test function

Returns:

  • (Boolean)


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

def comparable?; false end

#constructible?Boolean

Returns true if the type provides a well-defined parameterless default type constructor

Returns:

  • (Boolean)


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

def constructible?; false end

#copyable?Boolean

Returns true if the type provides a well-defined copy constructor to create a clone of an instance

Returns:

  • (Boolean)


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

def copyable?; false end

#destructible?Boolean

Returns true if the type provides a well-defined type destructor

Returns:

  • (Boolean)


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

def destructible?; false end

#entitiesObject



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

def entities; super << CommonCode end

#externObject

def write_impls(stream, define)



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

def extern; :AUTOC_EXTERN end

#freeObject



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

def free; :free end

#hashObject



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

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

#hashable?Boolean

Returns true if the type provides a well-defined hash calculation function

Returns:

  • (Boolean)


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

def hashable?; false end

#initializable?Boolean

Returns true if the type provides a well-defined type constructor which can have extra arguments

Returns:

  • (Boolean)


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

def initializable?; false end

#inlineObject



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

def inline; :AUTOC_INLINE end

#mallocObject



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

def malloc; :malloc end

#orderable?Boolean

Returns true if the type provides a well-defined 'less than' test function

Returns:

  • (Boolean)


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

def orderable?; false end

#prefixObject



164
165
166
167
168
# File 'lib/autoc/type.rb', line 164

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 a valid C identifier
  AutoC.c_id(type)
end

#private?Boolean

Returns:

  • (Boolean)


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

def private?; @visibility == :private end

#public?Boolean

Returns:

  • (Boolean)


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

def public?; @visibility == :public end

#sortable?Boolean

Returns true if the type provides corrset soritng routines

Returns:

  • (Boolean)


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

def sortable?; comparable? && orderable? end

#staticObject



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

def static; :AUTOC_STATIC end

#static?Boolean

Returns:

  • (Boolean)


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

def static?; @visibility == :static end

#write_decls(stream) ⇒ Object



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

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



221
222
223
224
225
226
227
# File 'lib/autoc/type.rb', line 221

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

#write_intf(stream) ⇒ Object



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

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