Class: AutoC::Type

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

Direct Known Subclasses

Collection, UserDefinedType

Instance Attribute 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.



57
58
59
60
# File 'lib/autoc/type.rb', line 57

def initialize(type, visibility = :public)
  @type = type.to_s
  @visibility = [:public, :private, :static].include?(visibility) ? visibility : raise("unsupported visibility")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/autoc/type.rb', line 62

def method_missing(method, *args)
  str = method.to_s
  func = @type + str[0,1].capitalize + str[1..-1] # Ruby 1.8 compatible
  if args.empty?
    func # Emit bare function name
  elsif args.size == 1 && args.first == nil
    func + "()" # Use sole nil argument to emit function call with no arguments
  else
    func + "(" + args.join(", ") + ")" # Emit normal function call with supplied arguments
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



53
54
55
# File 'lib/autoc/type.rb', line 53

def type
  @type
end

Instance Method Details

#abortObject



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

def abort; "abort" end

#assertObject



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

def assert; "assert" end

#callocObject



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

def calloc; "calloc" end

#entitiesObject



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

def entities; [CommonCode] end

#externObject



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

def extern; "AUTOC_EXTERN" end

#freeObject



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

def free; "free" end

#inlineObject



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

def inline; "AUTOC_INLINE" end

#mallocObject



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

def malloc; "malloc" end

#staticObject



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

def static; "AUTOC_STATIC" end

#write_decls(stream) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/autoc/type.rb', line 82

def write_decls(stream)
  case @visibility
    when :private
      write_exported_types(stream)
      write_exported_declarations(stream, extern, inline)
    when :static
      write_exported_types(stream)
      write_exported_declarations(stream, static, inline)
  end
end

#write_defs(stream) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/autoc/type.rb', line 93

def write_defs(stream)
  case @visibility
    when :public, :private
      write_implementations(stream, nil)
    when :static
      write_implementations(stream, static)
  end
end

#write_exported_declarations(stream, declare, define) ⇒ Object



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

def write_exported_declarations(stream, declare, define) end

#write_exported_types(stream) ⇒ Object



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

def write_exported_types(stream) end

#write_implementations(stream, define) ⇒ Object



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

def write_implementations(stream, define) end

#write_intf(stream) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/autoc/type.rb', line 74

def write_intf(stream)
  case @visibility
    when :public
      write_exported_types(stream)
      write_exported_declarations(stream, extern, inline)
  end
end