Znav |
---|
next | MATLAB Mapping for Operations |
---|
prev | MATLAB Mapping for Exceptions |
---|
|
The mapping of Slice interfaces revolves around the idea that, to invoke a remote operation, you call a member function on a local class instance that is a proxy for the remote object. This makes the mapping easy and intuitive to use because making a remote procedure call is no different from making a local procedure call (apart from error semantics).
...
The Slice compiler generates the following definition for use by the client:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef SimplePrx < Ice.ObjectPrx
methods
function op(obj, varargin)
...
end
end
methods(Static)
function id = ice_staticId()
...
end
function r = checkedCast(proxy, varargin)
...
end
function r = uncheckedCast(proxy, varargin)
...
end
end
...
end |
...
An empty array denotes the null proxy. The null proxy is a dedicated value that indicates that a proxy points "nowhere" (denotes no object).
Ztop |
---|
Anchor |
---|
ObjectPrx | ObjectPrx |
...
Interface Inheritance in MATLAB
Inheritance relationships among Slice interfaces are maintained in the generated MATLAB classes. For example:
Code Block |
---|
|
interface A { ... }
interface B { ... }
interface C extends A, B { ... } |
The generated code for CPrx
reflects the inheritance hierarchy:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef CPrx < APrx & BPrx
...
end |
Given a proxy for C
, a client can invoke any operation defined for interface C
, as well as any operation inherited from C
's base interfaces.
The ObjectPrx
Interface in MATLAB
All Ice objects have Object
as the ultimate ancestor type, so all proxies inherit from ObjectPrx
.All Ice objects have Object
as the ultimate ancestor type, so all proxies inherit from ObjectPrx
. ObjectPrx
provides a number of methods::
Code Block |
---|
|
code |
classdef ObjectPrx < handle
methods
function r = eq(obj, other)
function r = ice_getIdentity(obj)
function r = ice_isA(obj, id, varargin)
function r = ice_ids(obj, varargin)
function r = ice_id(obj, varargin)
function ice_ping(obj, varargin)
% ...
end
end |
...
ice_getIdentity
This method returns the identity of the object denoted by the proxy. The identity of an Ice object has the following Slice type:
Code Block |
---|
|
module Ice
{
struct Identity
{
string name;
string category;
}
}
|
To see whether two proxies denote the same object, first obtain the identity for each object and then compare the identities:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy1 = ...;
proxy2 = ...;
id1 = proxy1.ice_getIdentity();
id2 = proxy2.ice_getIdentity();
if isequal(id1, id2)
% proxy1 and proxy2 denote the same object
else
% proxy1 and proxy2 denote different objects
end |
ice_isA
The ice_isA
method determines whether the object denoted by the proxy supports a specific interface. The argument to ice_isA
is a type ID. For example, to see whether a proxy of type ObjectPrx
denotes a Printer
object, we can write:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy = ...;
if ~isempty(proxy) && proxy.ice_isA("::Printer")
% o denotes a Printer object
else
% o denotes some other type of object
end |
Note that we are testing whether the proxy is null before attempting to invoke the ice_isA
method. This avoids getting an error if the proxy is an empty array.
...
As we saw earlier, the Slice-to-MATLAB compiler generates static helper methods that support down-casting and type discovery:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef SimplePrx < Ice.ObjectPrx
...
methods(Static)
function id = ice_staticId()
...
end
function r = checkedCast(proxy, varargin)
...
end
function r = uncheckedCast(proxy, varargin)
...
end
end
...
end |
...
Given a proxy of any type, you can use a checkedCast
to determine whether the corresponding object supports a given type, for example:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy = ...; % Get a proxy from somewhere...
simple = SimplePrx.checkedCast(proxy);
if ~isempty(simple)
% Object supports the Simple interface...
else
% Object is not of type Simple...
end |
...
Suppose you expect to receive a proxy for a Process
object and use an uncheckedCast
to down-cast the proxy:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy = ...; % Get proxy...
process = ProcessPrx.uncheckedCast(proxy); % No worries...
process.launch(40, 60); % Oops...
|
...
The base proxy class ObjectPrx
supports a variety of methods for customizing a proxy. Since proxies are immutable, each of these "factory methods" returns a copy of the original proxy that contains the desired modification. For example, you can obtain a proxy configured with a ten second invocation timeout as shown below:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy = communicator.stringToProxy(...);
proxy = proxy.ice_invocationTimeout(10000);
|
A factory method returns a new proxy object if the requested modification differs from the current proxy, otherwise it returns the current proxy. With few exceptions, factory methods return a proxy of the same type as the current proxy, therefore it is generally not necessary to repeat a checkedCast
or uncheckedCast
after using a factory method. Furthermore, the mapping generates type-specific factory methods so that no casts are necessary:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
base = communicator.stringToProxy(...);
hello = HelloPrx.checkedCast(base);
hello = hello.ice_invocationTimeout(10000); % No cast is necessary
hello.sayHello(); |
...
Proxies can be compared for equality using the ==
operator. Note that proxy comparison with ==
uses all of the information in a proxy for the comparison. This means that not only the object identity must match for a comparison to succeed, but other details inside the proxy, such as the protocol and endpoint information, must be the same. In other words, comparison with ==
tests for proxy identity, not object identity. A common mistake is to write code along the following lines:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy1 = ...; % Get a proxy...
proxy2 = ...; % Get another proxy...
if proxy1 == proxy2
% proxy1 and proxy2 denote the same object % Correct
else
% proxy1 and proxy2 denote different objects % WRONG!
end
|
...
To compare the object identities of two proxies, you can use helper functions in the Ice
package:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
function r = proxyIdentityCompare(lhs, rhs)
function r = proxyIdentityAndFacetCompare(lhs, rhs)
|
proxyIdentityCompare
allows you to correctly compare proxies for identity:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
proxy1 = ...; % Get a proxy...
proxy2 = ...; % Get another proxy...
if Ice.proxyIdentityCompare(p1, p2) == 0
% proxy1 and proxy2 denote the same object % Correct
else
% proxy1 and proxy2 denote different objects % Correct
end
|
...
Znav |
---|
next | MATLAB Mapping for Operations |
---|
prev | MATLAB Mapping for Exceptions |
---|
|