| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
module Qpid::Proton |
| 20 |
module Util |
| 21 |
|
| 22 |
# @private |
| 23 |
# Class methods to help wrapper classes define forwarding methods to proton-C functions |
| 24 |
# |
| 25 |
# The extending class must define PROTON_METHOD_PREFIX, the functions here |
| 26 |
# make it easy to define ruby methods to forward calls to C functions. |
| 27 |
# |
| 28 |
module SWIGClassHelper |
| 29 |
# Define ruby method +name+ to forward arguments to |
| 30 |
# CProton.PROTON_METHOD_PREFIX_+pn_name+(@impl, ...) |
| 31 |
def proton_forward(name, pn_name) |
| 32 |
pn_name = pn_name[0..-2] if pn_name.to_s.end_with? "?" # Drop trailing ? for ruby bool methods |
| 33 |
pn_name = "#{self::PROTON_METHOD_PREFIX}_#{pn_name}".to_sym |
| 34 |
define_method(name.to_sym) { |*args| Cproton.__send__(pn_name, @impl, *args) } |
| 35 |
end |
| 36 |
|
| 37 |
def proton_caller(*names) names.each { |name| proton_forward(name, name) }; end |
| 38 |
def proton_set(*names) names.each { |name| proton_forward("#{name}=", "set_#{name}") }; end |
| 39 |
def proton_get(*names) names.each { |name| proton_forward(name, "get_#{name}") }; end |
| 40 |
def proton_is(*names) names.each { |name| proton_forward("#{name}?", "is_#{name}") }; end |
| 41 |
def proton_set_get(*names) names.each { |name| proton_get(name); proton_set(name) }; end |
| 42 |
def proton_set_is(*names) names.each { |name| proton_is(name); proton_set(name) }; end |
| 43 |
|
| 44 |
# Store ruby wrappers as attachments so they can be retrieved from the C pointer. |
| 45 |
# |
| 46 |
# Wrappers are stored in a registry using a key. The key is then attached to |
| 47 |
# the Proton structure as a record. That record lives for as long as the |
| 48 |
# Proton structure lives, and when the structure is released the record acts |
| 49 |
# as hook to also delete the Ruby wrapper object from the registry. |
| 50 |
|
| 51 |
@@registry = {} |
| 52 |
|
| 53 |
# @private |
| 54 |
def get_key(impl) |
| 55 |
("%032x" % Cproton.pni_address_of(impl)) |
| 56 |
end |
| 57 |
|
| 58 |
# @private |
| 59 |
# Stores the given object for later retrieval. |
| 60 |
# |
| 61 |
# @param object [Object] The object. |
| 62 |
# @param attachment_method [Symbol] The Proton attachment method. |
| 63 |
# |
| 64 |
def store_instance(object, attachment_method = nil) |
| 65 |
# ensure the impl has a reference to the wrapper object |
| 66 |
object.impl.instance_eval { @proton_wrapper = object } |
| 67 |
registry_key = get_key(object.impl) |
| 68 |
unless attachment_method.nil? |
| 69 |
record = Cproton.__send__(attachment_method, object.impl) |
| 70 |
rbkey = Cproton.Pn_rbkey_new |
| 71 |
Cproton.Pn_rbkey_set_registry(rbkey, Cproton.pn_rb2void(Qpid::Proton::Util::Wrapper.registry)) |
| 72 |
Cproton.Pn_rbkey_set_method(rbkey, "delete") |
| 73 |
Cproton.Pn_rbkey_set_key_value(rbkey, registry_key) |
| 74 |
Cproton.pn_record_def(record, RBCTX, Cproton.Pn_rbkey__class()); |
| 75 |
Cproton.pn_record_set(record, RBCTX, rbkey) |
| 76 |
end |
| 77 |
@@registry[registry_key] = object |
| 78 |
end |
| 79 |
|
| 80 |
# Retrieves the wrapper object with the supplied Proton struct. |
| 81 |
# |
| 82 |
# @param impl [Object] The wrapper for the Proton struct. |
| 83 |
# @param attachment_method [Symbol] The Proton attachment method. |
| 84 |
# |
| 85 |
# @return [Object] The Ruby wrapper object. |
| 86 |
# |
| 87 |
def fetch_instance(impl, attachment_method = nil) |
| 88 |
# if the impl has a wrapper already attached, then return it |
| 89 |
if impl.instance_variable_defined?(:@proton_wrapper) |
| 90 |
return impl.instance_variable_get(:@proton_wrapper) |
| 91 |
end |
| 92 |
unless attachment_method.nil? |
| 93 |
record = Cproton.__send__(attachment_method, impl) |
| 94 |
rbkey = Cproton.pni_void2rbkey(Cproton.pn_record_get(record, RBCTX)) |
| 95 |
# if we don't have a key, then we don't have an object |
| 96 |
return nil if rbkey.nil? |
| 97 |
registry_key = Cproton.Pn_rbkey_get_key_value(rbkey) |
| 98 |
else |
| 99 |
registry_key = get_key(impl) |
| 100 |
end |
| 101 |
# if the object's not in the registry then return |
| 102 |
return nil unless @@registry.has_key?(registry_key) |
| 103 |
|
| 104 |
result = @@registry[registry_key] |
| 105 |
# result = nil unless result.weakref_alive? |
| 106 |
if result.nil? |
| 107 |
raise Qpid::Proton::ProtonError.new("missing object for key=#{registry_key}") |
| 108 |
else |
| 109 |
# update the impl since the Swig wrapper for it may have changed |
| 110 |
result.impl = impl |
| 111 |
end |
| 112 |
return result |
| 113 |
end |
| 114 |
RBCTX = self.hash.to_i |
| 115 |
end |
| 116 |
|
| 117 |
# @private |
| 118 |
# |
| 119 |
# Instance methods to include in classes that wrap pn_object types |
| 120 |
# that support pn_inspect etc. Automatically extends SWIGClassHelper |
| 121 |
# |
| 122 |
module Wrapper |
| 123 |
|
| 124 |
def self.included(base) |
| 125 |
base.extend(SWIGClassHelper) |
| 126 |
end |
| 127 |
|
| 128 |
attr_accessor :impl |
| 129 |
|
| 130 |
def inspect |
| 131 |
return "#{self.class}<nil>" unless @impl |
| 132 |
pstr = Cproton.pn_string("") |
| 133 |
begin |
| 134 |
Cproton.pn_inspect(@impl, pstr) |
| 135 |
return Cproton.pn_string_get(pstr) |
| 136 |
ensure |
| 137 |
Cproton.pn_free(pstr) |
| 138 |
end |
| 139 |
end |
| 140 |
|
| 141 |
def to_s() inspect; end |
| 142 |
|
| 143 |
def self.registry |
| 144 |
@registry ||= {} |
| 145 |
end |
| 146 |
end |
| 147 |
end |
| 148 |
end |