classdef (Sealed, Hidden = true) SimulinkProfilingStore < handle
    % SIMULINKPROFILINGSTORE stores all generated instances of
    % coder.profile.ExecutionTime for the last simulation of a model
    
    % Copyright 2021 The MathWorks, Inc.
    
    methods(Access = private, Static)
        function res = getExecutionProfile(modelName, cmd, profVar)
            persistent ExecutionProfilingCache;
            
            if isempty(ExecutionProfilingCache)
                ExecutionProfilingCache = containers.Map;
            end
            
            switch cmd
                case 'reset'
                    ExecutionProfilingCache(modelName) = [];
                case 'get'
                    if ~ExecutionProfilingCache.isKey(modelName)
                        ExecutionProfilingCache(modelName) = [];
                    end
                case 'set'
                    ExecutionProfilingCache(modelName) = profVar;
                otherwise
                    msg = 'Unknown command to profiling store. Supported commands are: reset, get and set';
                    assert(false, msg);
            end
            
            res = ExecutionProfilingCache(modelName);
        end
    end
    
    methods(Static)
        function reset(modelName)
            coder.profile.SimulinkProfilingStore.getExecutionProfile(modelName, 'reset');
        end
        
        function res = get(modelName)
            res = coder.profile.SimulinkProfilingStore.getExecutionProfile(modelName, 'get');
        end
        
        function obj = set(modelName, profVar)
            obj = coder.profile.SimulinkProfilingStore.getExecutionProfile(modelName, 'get');
            if isempty(obj)
                coder.profile.SimulinkProfilingStore.getExecutionProfile(modelName, 'set', profVar);
            else
                obj.attachExecutionProfile(profVar);
                coder.profile.SimulinkProfilingStore.getExecutionProfile(modelName, 'set', obj);
            end
        end
    end
end
