Module: GMSEC::API

Extended by:
API
Included in:
API, Config, ConfigFile, Connection, Field, Message, Status
Defined in:
lib/gmsec/api.rb

Defined Under Namespace

Modules: Finalizer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gmsec/api.rb', line 29

def self.extended(base)
  base.extend FFI::Library
  base.extend FFI::DataConverter
  base.send :prepend, Finalizer

  class << base
    def to_native(value, context=nil)
      value.instance_variable_get("@native_object")
    end

    def from_native(value, context=nil)
      new(native_object: value)
    end

    def size
      find_type(native_type).size
    end

    def destroy!(instance)
      if instance.respond_to? :destroy!
        instance.destroy!
      end
    end

    def api_version
      gmsec_GetAPIVersion
    end
  end

  # Load the library for each time the API module is extended.
  base.send(:load_library)
  base.include GMSEC::Definitions
end

Instance Method Details

#bind(object_name, &block) ⇒ Object



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
90
91
92
# File 'lib/gmsec/api.rb', line 63

def bind(object_name, &block)
  native_type find_type(object_name)

  if block_given?
    define_method(:native_object_initializer, &block)
  end

  protected

  # Proxy #find_type through class
  define_method(:find_type) do |type|
    self.class.find_type(type)
  end

  define_method(:initialize_native_object) do |&block|
    instance_eval do
      pointer = FFI::MemoryPointer.new(self.class.native_type)
      block.call(pointer)
      @native_object = pointer.read_pointer
    end
  end

  define_method(:with_string_pointer) do |&block|
    pointer = FFI::MemoryPointer.new :pointer
    block.call(pointer)
    if pointer.read_pointer != nil
      pointer.read_pointer.read_string_to_null
    end
  end
end

#has(*objects) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gmsec/api.rb', line 94

def has(*objects)
  mapping = {
    config: GMSEC::Config,
    status: GMSEC::Status}

  objects.select{|object| mapping.keys.include? object}.each do |object|
    attr_accessor object

    # Overwrite the getter method to memoize a new instance of the mapped object.
    define_method(object) do
      name = "@#{object}"
      instance_variable_set(name, instance_variable_get(name) || mapping[object].new)
    end
  end
end