function result = coder_options(method,varargin)

%% This is a centralized repository of various coder options
%% that drive Stateflow Code Generation.
%% Usage:
%% sfc('coder_options') 
%% without any arguments returns the options strcuture
%% sfc('coder_options','ignoreChecksums') returns the value of this option
%% sfc('coder_options','ignoreChecksums',1) sets the option to 1
%% For more info on the options, look at the comments in this file.

%   Copyright 1995-2012 The MathWorks, Inc.


persistent sfCoderOptions
mlock;

if(nargin==0)
    method = '';
end

isReset = strcmpi(method,'reset');

if( isReset || isempty(sfCoderOptions))
    sfCoderOptions.ignoreChecksums = 0;
    sfCoderOptions.debugBuilds = 0;
    sfCoderOptions.inlineThreshold = 10;
    sfCoderOptions.inlineThresholdMax = 200;
    sfCoderOptions.inlineStackLimit = 1000000;
    sfCoderOptions.maxStackUsage = 200000;
    sfCoderOptions.recursionLimit = 500;
    sfCoderOptions.ignoreUnresolvedSymbols = 0;
    sfCoderOptions.algorithmWordsizes = [8 16 32 32 64];
    sfCoderOptions.targetWordsizes = [8 16 32 32 64];
    sfCoderOptions.forceNonJitBuild = 0;
    sfCoderOptions.forceDebugOff = 0;
    sfCoderOptions.parallelJITFcnTensorCreation = 0;
    sfCoderOptions.debugGeneratedMex = 0;
end

if(nargin==0 || isempty(method) || isReset)
    result = sfCoderOptions;
    return;
end

names = fieldnames(sfCoderOptions);
for i=1:length(names)
    if strcmpi(names{i},method)
        if ~isempty(varargin)
            % setting new value
            newVal = [];
            
            if isnumeric(varargin{1})
                newVal = varargin{1};
            elseif ischar(varargin{1})
                newVal = str2double(varargin{1});
            end
            
            if ~isempty(newVal)
                sfCoderOptions.(names{i}) = newVal;
                if strcmpi(names{i}, 'debugGeneratedMex') 
                    if newVal > 0
                        sfCoderOptions.debugBuilds = 1;
                        sfCoderOptions.forceNonJitBuild = 1;
                        cgxe('Feature', 'JIT', 0);
                        cgxe('Feature', 'debugBuilds', 1); 
                    else
                        sfCoderOptions.debugBuilds = 0;
                        sfCoderOptions.forceNonJitBuild = 0;
                        cgxe('Feature', 'JIT', 1);
                        cgxe('Feature', 'debugBuilds', 0); 
                    end
                end
            end
        end
        result = sfCoderOptions.(names{i});
        return;
    end
end

error('Stateflow:Coder:InvalidCoderOptions', getString(message('Stateflow:sfprivate:InvalidCoderOptionPassed' ,method)));
