Skip to content

Commit

Permalink
Fixed checkedCast is MATLAB (#2540)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Jul 22, 2024
1 parent 6bba3eb commit b38903c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
58 changes: 25 additions & 33 deletions matlab/lib/+Ice/ObjectPrx.m
Original file line number Diff line number Diff line change
Expand Up @@ -1134,45 +1134,37 @@ function iceThrowUserException(~, is, varargin) % Varargs are user exception typ

methods(Static,Access=protected)
function r = iceCheckedCast(p, id, cls, varargin)
try
hasFacet = false;
facet = [];
context = {};
if length(varargin) == 1
if isa(varargin{1}, 'containers.Map')
context = { varargin{1} };
elseif isempty(varargin{1}) || isa(varargin{1}, 'char')
hasFacet = true;
facet = varargin{1};
else
throw(LocalException('Ice:ArgumentException', 'expecting string or containers.Map'));
end
elseif length(varargin) == 2
hasFacet = false;
facet = [];
context = {};
if length(varargin) == 1
if isa(varargin{1}, 'containers.Map')
context = { varargin{1} };
elseif isempty(varargin{1}) || isa(varargin{1}, 'char')
hasFacet = true;
facet = varargin{1};
context = { varargin{2} };
elseif length(varargin) > 2
throw(LocalException('Ice:ArgumentException', 'too many arguments to checkedCast'));
end
if ~isempty(p)
if hasFacet
p = p.ice_facet(facet);
end
if p.ice_isA(id, context{:})
constructor = str2func(cls);
r = constructor(p.communicator, '', p.clone_(), p.encoding);
else
r = [];
end
else
r = p;
throw(LocalException('Ice:ArgumentException', 'expecting string or containers.Map'));
end
catch ex
if isa(ex, 'Ice.FacetNotExistException')
r = [];
elseif length(varargin) == 2
hasFacet = true;
facet = varargin{1};
context = { varargin{2} };
elseif length(varargin) > 2
throw(LocalException('Ice:ArgumentException', 'too many arguments to checkedCast'));
end
if ~isempty(p)
if hasFacet
p = p.ice_facet(facet);
end
if p.ice_isA(id, context{:})
constructor = str2func(cls);
r = constructor(p.communicator, '', p.clone_(), p.encoding);
else
ex.throwAsCaller();
r = [];
end
else
r = p;
end
end

Expand Down
7 changes: 6 additions & 1 deletion matlab/test/Ice/proxy/AllTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,12 @@
loc = Ice.LocatorPrx.checkedCast(base);
assert(isempty(loc));

assert(isempty(MyClassPrx.checkedCast(cl, 'facet')));
try
MyClassPrx.checkedCast(cl, 'facet');
assert(false);
catch ex
assert(isa(ex, 'Ice.FacetNotExistException'));
end

%
% Upcasting
Expand Down

0 comments on commit b38903c

Please sign in to comment.