Class: VirtualBox::COM::AbstractInterface

Inherits:
AbstractModel show all
Defined in:
lib/virtualbox/com/abstract_interface.rb

Overview

# Defining an Interface

Defining an interface is done by subclassing AbstractInterface and using the provided class methods to define the COM methods and properties. A small example class is shown below:

class Time < AbstractInterface
  function :now, [[:out, :uint]]
  property :hour, :uint
end

# Accessing an Interface

# Assume `time` was retrieved already
puts time.foo.to_s
time.hour = 20
x = time.now

The above example shows how the properties and functions can be used with a given interface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractModel

iid

Constructor Details

#initialize(*args) ⇒ AbstractInterface

Initializes the interface with the given implementer



103
104
105
106
# File 'lib/virtualbox/com/abstract_interface.rb', line 103

def initialize(*args)
    @args = args
    @implementer = Implementer.new(self, *args)
end

Instance Attribute Details

#implementerObject (readonly)

Returns the value of attribute implementer.



30
31
32
# File 'lib/virtualbox/com/abstract_interface.rb', line 30

def implementer
  @implementer
end

Class Method Details

.extends(model) ⇒ Object

Extends the current model with another one. Note that redefining functions or properties is not supported.



36
37
38
39
40
41
42
43
44
45
# File 'lib/virtualbox/com/abstract_interface.rb', line 36

def extends(model)
    Model.get(model).members.each do |spec|
        if h.include?(spec.name)
            raise "redefining of #{spec.name} is not supported"
        end
        h[spec.name] = spec
    end
rescue ModelNotFoundException
    raise "trying to extend an unknown model (#{model})"
end

.function(name, type, args, opts = {}) ⇒ Object

Adds a function to the interface



48
49
50
# File 'lib/virtualbox/com/abstract_interface.rb', line 48

def function(name, type, args, opts={})
    h[name] = Spec::Function.new(name, type, args, opts)
end

.functionsObject

List of functions (Spec::Function)



87
88
89
# File 'lib/virtualbox/com/abstract_interface.rb', line 87

def functions
    members.select {|s| s.kind_of?(Spec::Function) }
end

.member(name) ⇒ Object

Get a member by name



77
78
79
# File 'lib/virtualbox/com/abstract_interface.rb', line 77

def member(name)
    h[name]
end

.membersObject

List of members (Spec::*)



82
83
84
# File 'lib/virtualbox/com/abstract_interface.rb', line 82

def members
    h.values
end

.propertiesObject

List if properties (Spec::Property)



92
93
94
# File 'lib/virtualbox/com/abstract_interface.rb', line 92

def properties
    members.select {|s| s.kind_of?(Spec::Property) }
end

.property(name, type, opts = {}) ⇒ Object

Adds a property to the interface



53
54
55
# File 'lib/virtualbox/com/abstract_interface.rb', line 53

def property(name, type, opts={})
    h[name] = Spec::Property.new(name, type, opts)
end

.setupObject

Perform final setup



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/virtualbox/com/abstract_interface.rb', line 58

def setup
    members.each {|spec| name = spec.name
        case spec
        when Spec::Function
            define_method(name) { |*args| 
                @implementer.call_function(spec, *args) 
            } unless spec.hide?
        when Spec::Property
            define_method(name) {
                @implementer.read_property(spec)
            } unless spec.hide?
            define_method(:"#{name}=") { |value|
                @implementer.write_property(spec, value)
            } unless spec.hide? || spec.readonly?
        end
    }
end

Instance Method Details

#call_function(name, *args) ⇒ Object

Calls a function with the given name



130
131
132
133
134
# File 'lib/virtualbox/com/abstract_interface.rb', line 130

def call_function(name, *args)
    spec = self.class.member(name)
    raise "#{name} is not a function" unless spec.kind_of?(Spec::Function)
    @implementer.call_function(spec, args)
end

#cast(name) ⇒ Object

Cast to another model



110
111
112
# File 'lib/virtualbox/com/abstract_interface.rb', line 110

def cast(name)
    @implementer.cast(name, *@args)
end

#inspectObject

Concise inspect



138
139
140
# File 'lib/virtualbox/com/abstract_interface.rb', line 138

def inspect
    "#<#{self.class.name}>"
end

#read_property(name) ⇒ Object

Reads a property with the given name



115
116
117
118
119
# File 'lib/virtualbox/com/abstract_interface.rb', line 115

def read_property(name)
    spec = self.class.member(name)
    raise "#{name} is not a property" unless spec.kind_of?(Spec::Property)
    @implementer.read_property(spec)
end

#write_property(name, value) ⇒ Object

Writes a property with the given name



122
123
124
125
126
127
# File 'lib/virtualbox/com/abstract_interface.rb', line 122

def write_property(name, value)
    spec = self.class.member(name)
    raise "#{name} is not a property" unless spec.kind_of?(Spec::Property)
    raise "property #{name} is readonly" if spec.readonly?
    @implementer.write_property(spec, value)
end