1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 62 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
%
% Copyright (c) ZeroC, Inc. All rights reserved.
%
classdef (Abstract) EncapsDecoder < handle
methods
function obj = EncapsDecoder(is, encaps, sliceValues, valueFactoryManager, classResolver, classGraphDepthMax)
obj.is = is;
obj.encaps = encaps;
obj.sliceValues = sliceValues;
obj.valueFactoryManager = valueFactoryManager;
obj.classResolver = classResolver;
obj.classGraphDepthMax = classGraphDepthMax;
obj.classGraphDepth = 0;
obj.patchMap = {};
obj.patchMapLength = 0;
obj.unmarshaledMap = {};
obj.typeIdMap = {};
obj.typeIdConstructorMap = {};
obj.valueList = {};
obj.delayedPostUnmarshal = {};
end
function r = readOptional(~, ~, ~)
r = false;
end
function readPendingValues(~)
end
function finish(obj)
%
% This is our opportunity for unmarshaled values to do some post processing after the initial round
% of unmarshaling is complete.
%
if ~isempty(obj.delayedPostUnmarshal)
%
% First call icePostUnmarshal on every instance. This allows the generated code to finish its tasks.
%
for i = 1:length(obj.delayedPostUnmarshal)
v = obj.delayedPostUnmarshal{i};
v.icePostUnmarshal();
end
%
% Then call ice_postUnmarshal on every instance. This is the application's interception point.
%
for i = 1:length(obj.delayedPostUnmarshal)
v = obj.delayedPostUnmarshal{i};
try
v.ice_postUnmarshal();
catch ex
msg = sprintf('exception raised by ice_postUnmarshal:\n%s', getReport(ex, 'extended'));
obj.is.getCommunicator().getLogger().warning(msg);
end
end
obj.delayedPostUnmarshal = {};
end
end
end
methods(Abstract)
readValue(obj, cb)
throwException(obj)
startInstance(obj, sliceType)
r = endInstance(obj, preserve)
r = startSlice(obj)
endSlice(obj)
skipSlice(obj)
end
methods(Access=protected)
function r = getTypeId(obj, typeIdIndex)
if typeIdIndex <= length(obj.typeIdMap)
r = obj.typeIdMap{typeIdIndex};
else
throw(Ice.UnmarshalOutOfBoundsException());
end
end
function [typeIdIndex, typeId] = readTypeId(obj)
typeId = obj.is.readString();
obj.typeIdMap{end + 1} = typeId;
typeIdIndex = length(obj.typeIdMap);
end
function r = newInstance(obj, typeIdIndex, typeId)
if typeIdIndex >= 0 && typeIdIndex <= length(obj.typeIdConstructorMap)
constructor = obj.typeIdConstructorMap{typeIdIndex};
else
constructor = obj.getConstructor(typeId);
if ~isempty(constructor)
obj.typeIdConstructorMap{typeIdIndex} = constructor;
end
end
if ~isempty(constructor)
try
r = constructor(); % Invoke the constructor.
catch e
reason = sprintf('constructor failed for %s', typeId);
ex = Ice.NoValueFactoryException('', reason, reason, typeId);
ex.addCause(e);
throw(ex);
end
else
r = [];
end
end
function r = getConstructor(obj, typeId)
%
% Try to find a factory registered for the specific type.
%
r = [];
userFactory = obj.valueFactoryManager.find(typeId);
if ~isempty(userFactory)
r = @() userFactory(typeId);
else
%
% Last chance: ask the class resolver to find it.
%
constructor = obj.classResolver.resolve(typeId);
if ~isempty(constructor)
r = @() constructor(IceInternal.NoInit.Instance);
end
end
end
function addPatchEntry(obj, index, cb)
%assert(index > 0);
%
% Check if we have already unmarshalled the instance. If that's the case,
% just invoke the callback and we're done.
%
if index <= length(obj.unmarshaledMap)
v = obj.unmarshaledMap{index};
if ~isempty(v)
cb(v);
return;
end
end
%
% Add patch entry if the instance isn't unmarshaled yet,
% the callback will be called when the instance is
% unmarshaled.
%
if index <= length(obj.patchMap)
pl = obj.patchMap{index};
else
%
% We have no outstanding instances to be patched for this
% index, so make a new entry in the patch map.
%
pl = {};
obj.patchMapLength = obj.patchMapLength + 1;
end
%
% Append a patch entry for this instance.
%
e = IceInternal.PatchEntry();
e.cb = cb;
e.classGraphDepth = obj.classGraphDepth;
pl{end + 1} = e;
obj.patchMap{index} = pl;
end
function unmarshal(obj, index, v)
%
% Add the instance to the map of unmarshaled instances, this must
% be done before reading the instances (for circular references).
%
obj.unmarshaledMap{index} = v;
%
% Read the instance.
%
v.iceRead(obj.is);
if index <= length(obj.patchMap)
%
% Patch all instances now that the instance is unmarshaled.
%
l = obj.patchMap{index};
%
% Patch all pointers that refer to the instance.
%
for i = 1:length(l)
e = l{i};
e.cb(v);
end
%
% Clear out the patch map for that index -- there is nothing left
% to patch for that index for the time being.
%
obj.patchMap{index} = [];
obj.patchMapLength = obj.patchMapLength - 1;
end
if obj.patchMapLength == 0 && isempty(obj.valueList)
if v.iceDelayPostUnmarshal()
obj.delayedPostUnmarshal{end + 1} = v; % See finish()
else
try
v.ice_postUnmarshal();
catch ex
msg = sprintf('exception raised by ice_postUnmarshal:\n%s', getReport(ex, 'extended'));
obj.is.getCommunicator().getLogger().warning(msg);
end
end
else
obj.valueList{end + 1} = v;
if obj.patchMapLength == 0
%
% Iterate over the instance list and invoke ice_postUnmarshal on
% each instance. We must do this after all instances have been
% unmarshaled in order to ensure that any instance data members
% have been properly patched.
%
for i = 1:length(obj.valueList)
p = obj.valueList{i};
if p.iceDelayPostUnmarshal()
obj.delayedPostUnmarshal{end + 1} = p; % See finish()
else
try
p.ice_postUnmarshal();
catch ex
msg = sprintf('exception raised by ice_postUnmarshal:\n%s', getReport(ex, 'extended'));
obj.is.getCommunicator().getLogger().warning(msg);
end
end
end
obj.valueList = {};
end
end
end
end
properties(Access=protected)
is
encaps
sliceValues
valueFactoryManager
classResolver
classGraphDepth
classGraphDepthMax
patchMap
patchMapLength
end
properties(Access=private)
unmarshaledMap
typeIdConstructorMap
typeIdMap
valueList
delayedPostUnmarshal
end
end
|