summaryrefslogtreecommitdiff
path: root/matlab/lib/+IceInternal/ClassResolver.m
blob: 8094df7f1d4779b1943f2c39e4cd40988d74dd77 (plain)
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
%
% Copyright (c) ZeroC, Inc. All rights reserved.
%

classdef ClassResolver < handle
    methods
        function obj = ClassResolver()
            obj.typeToConstructorMap = containers.Map('KeyType', 'char', 'ValueType', 'any');
        end
        function r = resolve(obj, typeId)
            %
            % NOTE: The return value is the constructor function.
            %

            %
            % See if we've already translated this type ID before.
            %
            try
                %
                % A type is only added to this map if its class exists, so we're done.
                %
                r = obj.typeToConstructorMap(typeId);
                return;
            catch
                %
                % The map raises an exception if a key doesn't exist.
                %
            end

            %
            % If it's a new type ID, first convert it into a class name.
            %
            className = IceInternal.Util.idToClass(typeId);

            %
            % See if we can find the class.
            %
            found = exist(className, 'class');

            %
            % If we found the class, update our map so we don't have to translate this type ID again.
            %
            if found
                constructor = str2func(className);
                obj.typeToConstructorMap(typeId) = constructor;
                r = constructor;
            else
                r = [];
            end
        end
    end
    properties(Access=private)
        typeToConstructorMap
    end
end