...
The Slice compiler generates the following definition for use by the client:
Code Block | ||||
---|---|---|---|---|
| ||||
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 |
...
All Ice objects have Object
as the ultimate ancestor type, so all proxies inherit from ObjectPrx
. ObjectPrx
provides a number of methods:
Code Block | ||||
---|---|---|---|---|
| ||||
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 language slice title Slice 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
Theice_isA
method determines whether the object denoted by the proxy supports a specific interface. The argument toice_isA
is a type ID. For example, to see whether a proxy of typeObjectPrx
denotes aPrinter
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
function r = proxyIdentityCompare(lhs, rhs) function r = proxyIdentityAndFacetCompare(lhs, rhs) |
proxyIdentityCompare
allows you to correctly compare proxies for identity:
Code Block | ||||
---|---|---|---|---|
| ||||
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 |
...