%if EXISTS("_CACHELIB_") == 0
%assign _CACHELIB_ = 1
%include "codecache_api.tlc"
%<LibAddToCompiledModel("ExternsCache", "")>
%<LibAddToCompiledModel("SourceCodeCache", "")>
%<LibAddToCompiledModel("NonFiniteParamAssignmentsCache", "")>
%<LibAddToCompiledModel("NonFiniteBlockIOAssignmentsCache", "")>
%<LibAddToCompiledModel("FunctionPrototypesCache", "")>
%<LibAddToCompiledModel("DefinesCache", "")>
%<LibAddToCompiledModel("IncludesCache", "")>
%<LibAddToCompiledModel("TypedefsCache", "")>
%<LibAddToCompiledModel("GlobalPrmDataCache", "")>
%assign tempVar = tempRec {NumBlocks 0}
%<LibAddToCompiledModel("IntegerOnlyWarningCache", tempRec)>
%undef tempVar
%<LibAddToCompiledModel("NumFiles", 0)>
%function LibTrapCacheAssert(cacheVar) void
%if TYPE(cacheVar) == "Number"
%openfile assertTxt
Attempt to access a cache variable prior to calling its cache /
generation function.
The current cache:
%<cacheVar>
%closefile assertTxt
%<LibReportFatalError(assertTxt)>
%endif
%endfunction
%assign ResetGlobalsErrorString = "#error A TLC Global was used after it was reset. To avoid this error, set the TLC variable 'ResetTLCGlobalsAfterUse' to 0."
%function SLibResetTLCGlobal(name) void
%if ResetTLCGlobalsAfterUse
%if ISFIELD(::CompiledModel, name)
%assign ::CompiledModel.%<name> = ResetGlobalsErrorString + name
%elseif EXISTS(name)
%assign %<name> = ResetGlobalsErrorString + name
%endif
%endif
%endfunction
%function SLibCacheNonFiniteConstBIOAssignment(sysIdx, instIdx, buffer) void
%assign instIdx = (instIdx == -1) ? 0 : instIdx
%assign sysIdx = System[sysIdx].StandaloneParentSysIdx[instIdx]
%if sysIdx != -1
%<LibAddToSystemField(System[sysIdx],"CachedNonFiniteBlockIOAssignments",buffer)>
%else
%assign ::CompiledModel.NonFiniteBlockIOAssignmentsCache = ...
::CompiledModel.NonFiniteBlockIOAssignmentsCache + buffer
%endif
%endfunction
%function SLibCacheNonFiniteParamAssignment(mdlParam, buffer) void
%if SIZE(mdlParam.ReferencedBy,0) == 1
%assign sysIdx = mdlParam.OwnerSysIdx[0]
%assign instIdx = (mdlParam.OwnerSysIdx[1] == -1) ? 0 : mdlParam.OwnerSysIdx[1]
%assign sysIdx = ::CompiledModel.System[sysIdx].StandaloneParentSysIdx[instIdx]
%if sysIdx != -1
%<LibAddToSystemField(System[sysIdx],"CachedNonFiniteParamAssignments",buffer)>
%return
%endif
%endif
%assign ::CompiledModel.NonFiniteParamAssignmentsCache = ...
::CompiledModel.NonFiniteParamAssignmentsCache + buffer
%endfunction
%function LibDumpNonFiniteAssignmentCache() Output
%foreach sysIdx = NumSystems
%if System[sysIdx].StandaloneSubsystem
%assign buffer = LibGetSystemField(System[sysIdx], "CachedNonFiniteParamAssignments") + ...
LibGetSystemField(System[sysIdx], "CachedNonFiniteBlockIOAssignments")
%if !WHITE_SPACE(buffer)
%openfile nonfiniteBuffer
/* non-finite (run-time) assignments */
%<buffer>
%closefile nonfiniteBuffer
%<LibAddToSystemField(System[sysIdx],"CachedInitializeDataBody", ...
nonfiniteBuffer)>
%endif
%endif
%endforeach
%assign buffer = ::CompiledModel.NonFiniteParamAssignmentsCache + ...
::CompiledModel.NonFiniteBlockIOAssignmentsCache
%if (!WHITE_SPACE(buffer))
/* non-finite (run-time) assignments */
%<buffer>
%endif
%endfunction
%function LibCacheFunctionPrototype(buffer) void
%if(CGMODEL_ACCESS("CGModel.TLCServerBlockOperationsInCPP"))
%<LibCacheFunctionPrototypeBuffer(buffer)>
%else
%assign ::CompiledModel.FunctionPrototypesCache = ...
::CompiledModel.FunctionPrototypesCache + buffer
%endif
%endfunction
%function LibDumpFunctionPrototypesCache() Output
%if !WHITE_SPACE(FunctionPrototypesCache)
%<FunctionPrototypesCache>/
%endif
%<SLibResetTLCGlobal("FunctionPrototypesCache")>/
%endfunction
%function LibCacheDefine(buffer) void
%assign ::CompiledModel.DefinesCache = ::CompiledModel.DefinesCache + buffer
%endfunction
%function FcnCacheSFunctionDefinesToRLS(buffer) void
%if !CGMODEL_ACCESS("CGModel.isCodeGenContextBuild")
%return
%endif
%assign sysIdx = CGMODEL_ACCESS("CGModel.getCUTSysIdx")
%assign system = ::CompiledModel.System[sysIdx]
%<SLibCacheSystemCodeToFile("sys_hdr_incl", system, buffer)>
%endfunction
%function LibDumpDefinesCache() Output
%if !WHITE_SPACE(DefinesCache)
%<DefinesCache>/
%endif
%<SLibResetTLCGlobal("DefinesCache")>/
%endfunction
%function FcnScanForDuplicateIncludes(incFileName) void
%assign duplicate = 0
%foreach idx = ::CompiledModel.NumNonSharedPathHeaders
%if (::CompiledModel.NonSharedPathHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%foreach idx = ::CompiledModel.NumNonSharedLocalHeaders
%if (::CompiledModel.NonSharedLocalHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%foreach idx = ::CompiledModel.NumSysPathHeaders
%if (::CompiledModel.SysPathHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%foreach idx = ::CompiledModel.NumSysLocalHeaders
%if (::CompiledModel.SysLocalHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%foreach idx = ::CompiledModel.NumBlockPathHeaders
%if (::CompiledModel.BlockPathHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%foreach idx = ::CompiledModel.NumBlockLocalHeaders
%if (::CompiledModel.BlockLocalHeaders[idx] == incFileName)
%assign duplicate = 1
%break
%endif
%endforeach
%return duplicate
%endfunction
%function LibAddToCommonIncludes(incFileName) void
%if incFileName == ""
%return
%endif
%if SLibInBlockScope()
%assign block = SLibGetCurrentBlock()
%assign blkType = block.Type
%if block.Type == "S-Function"
%assign blkType = ParamSettings.FunctionName
%elseif block.Type == "M-S-Function"
%assign blkType = ParamSettings.MFile
%endif
%endif
%assign duplicate = FcnScanForDuplicateIncludes(incFileName)
%if (!duplicate)
%if incFileName[0] == "<"
%assign ::CompiledModel.BlockPathHeaders = ...
::CompiledModel.BlockPathHeaders + incFileName
%assign ::CompiledModel.NumBlockPathHeaders = ...
::CompiledModel.NumBlockPathHeaders + 1
%else
%assign ::CompiledModel.BlockLocalHeaders = ...
::CompiledModel.BlockLocalHeaders + incFileName
%assign ::CompiledModel.NumBlockLocalHeaders = ...
::CompiledModel.NumBlockLocalHeaders + 1
%endif
%endif
%<FcnAddSfuncHeaderToRLSIncludes(incFileName)>/
%endfunction
%function (incFileName) void
%if EXISTS(::CurrentSFcnBlockName) && (::CurrentSFcnBlockName != "")
%assign blockName = ::CurrentSFcnBlockName
%assign numRLS = CGMODEL_ACCESS("CGModel.getNumRLSFromSfuncMap",blockName)
%foreach idx = numRLS
%assign sysIdx = CGMODEL_ACCESS("CGModel.getRLSSysIdxFromSfuncMap",blockName, STRING(idx))
%assign origModuleIdx = ::CurrentModuleIdx
%assign ::CurrentModuleIdx = System[sysIdx].CGIRModuleIdx
%assign containsSharedTypes = ISFIELD(System[sysIdx], "IncludedSharedDataTypes") && ...
FcnAddHeaderHelper(incFileName, System[sysIdx].IncludedSharedDataTypes)
%if !containsSharedTypes && ::CompiledModel.GenUtilsSrcInSharedLocation == 1
%<FcnTrackHeaderFileUsage(incFileName,TLC_FALSE, TLC_FALSE)>/
%endif
%assign ::CurrentModuleIdx = origModuleIdx
%endforeach
%endif
%if !CGMODEL_ACCESS("CGModel.isCodeGenContextBuild")
%return
%endif
%assign sysIdx = CGMODEL_ACCESS("CGModel.getCUTSysIdx")
%assign system = ::CompiledModel.System[sysIdx]
%openfile incFileBuffer
#include "%<incFileName>"
%closefile incFileBuffer
%<SLibCacheSystemCodeToFile("sys_hdr_incl", system, incFileBuffer)>
%endfunction
%function (incFileName, buffer) void
%foreach idx = SIZE(buffer,1)
%if buffer[idx] == "/"" + incFileName + "/"" || ...
buffer[idx] == "<" + incFileName + ">"
%return TLC_TRUE
%endif
%endforeach
%return TLC_FALSE
%endfunction
%function SLibAddToCommonIncludes(incFileName) void
%if incFileName == ""
%return
%endif
%assign duplicate = FcnScanForDuplicateIncludes(incFileName)
%if (!duplicate)
%if incFileName[0] == "<"
%assign ::CompiledModel.SysPathHeaders = ...
::CompiledModel.SysPathHeaders + incFileName
%assign ::CompiledModel.NumSysPathHeaders = ...
::CompiledModel.NumSysPathHeaders + 1
%else
%assign ::CompiledModel.SysLocalHeaders = ...
::CompiledModel.SysLocalHeaders + incFileName
%assign ::CompiledModel.NumSysLocalHeaders = ...
::CompiledModel.NumSysLocalHeaders + 1
%endif
%endif
%endfunction
%function SLibAddToNonSharedIncludes(incFileName) void
%if incFileName == ""
%return
%endif
%assign duplicate = FcnScanForDuplicateIncludes(incFileName)
%if (!duplicate)
%if incFileName[0] == "<"
%assign ::CompiledModel.NonSharedPathHeaders = ...
::CompiledModel.NonSharedPathHeaders + incFileName
%assign ::CompiledModel.NumNonSharedPathHeaders = ...
::CompiledModel.NumNonSharedPathHeaders + 1
%else
%assign ::CompiledModel.NonSharedLocalHeaders = ...
::CompiledModel.NonSharedLocalHeaders + incFileName
%assign ::CompiledModel.NumNonSharedLocalHeaders = ...
::CompiledModel.NumNonSharedLocalHeaders + 1
%endif
%endif
%endfunction
%function SLibDumpCommonIncludes(system) void
%assign isSFcn = (CodeFormat == "S-Function")
%assign inclBuff = ""
%assign isRLS = LibSystemIsReusedLibraryFcn(system)
%if isRLS
%return
%else
%assign ifndefName = Name
%endif
%openfile inclBuff
%if !CGMODEL_ACCESS("CGModel.RemoveCommonIncludesGuards")
#ifndef %<ifndefName>_COMMON_INCLUDES_
# define %<ifndefName>_COMMON_INCLUDES_
%endif
%foreach idx = ::CompiledModel.NumSysPathHeaders
%assign incFileName = ::CompiledModel.SysPathHeaders[idx]
#include %<incFileName>
%endforeach
%foreach idx = ::CompiledModel.NumNonSharedPathHeaders
%assign incFileName = ::CompiledModel.NonSharedPathHeaders[idx]
#include %<incFileName>
%endforeach
%foreach idx = ::CompiledModel.NumBlockPathHeaders
%assign incFileName = ::CompiledModel.BlockPathHeaders[idx]
#include %<incFileName>
%endforeach
%if isSFcn
%<LibSFcnPreSimstrucDefines(system)>
%endif
%foreach idx = ::CompiledModel.NumSysLocalHeaders
%assign incFileName = ::CompiledModel.SysLocalHeaders[idx]
#include %<"/"%<incFileName>/"">
%endforeach
%if ISEQUAL(::ConfigSet.InferredTypesCompatibility, TLC_TRUE)
#define __TMWTYPES__ /* Inferred types compatibility mode */
%endif
%foreach idx = ::CompiledModel.NumNonSharedLocalHeaders
%assign incFileName = ::CompiledModel.NonSharedLocalHeaders[idx]
#include %<"/"%<incFileName>/"">
%endforeach
%foreach idx = ::CompiledModel.NumBlockLocalHeaders
%assign incFileName = ::CompiledModel.BlockLocalHeaders[idx]
%assign fileVarCond = CGMODEL_ACCESS("FileRepository.getFileVariantCondition",incFileName)
%if !ISEMPTY(fileVarCond)
%
%endif
#include %<"/"%<incFileName>/"">
%if !ISEMPTY(fileVarCond)
%
%endif
%endforeach
%if isSFcn
%<LibSFcnPostSimstrucDefines()>
%endif
%if !CGMODEL_ACCESS("CGModel.RemoveCommonIncludesGuards")
#endif /* %<ifndefName>_COMMON_INCLUDES_ */
%endif
%closefile inclBuff
%return inclBuff
%endfunction
%function LibCacheIncludes(buffer) void
%assign ::CompiledModel.IncludesCache = ...
::CompiledModel.IncludesCache + buffer
%endfunction
%function LibDumpIncludesCache() Output
%if !WHITE_SPACE(IncludesCache)
%<IncludesCache>/
%endif
%<SLibResetTLCGlobal("IncludesCache")>/
%endfunction
%function LibCacheTypedefs(buffer) void
%assign ::CompiledModel.TypedefsCache = ...
::CompiledModel.TypedefsCache + buffer
%endfunction
%function LibDumpTypedefsCache() void
%if !WHITE_SPACE(TypedefsCache)
%return "/n%<TypedefsCache>"
%else
%return ""
%endif
%endfunction
%function LibCacheExtern(buffer) void
%assign ::CompiledModel.ExternsCache = ::CompiledModel.ExternsCache + buffer
%<FcnCacheSFunctionExternDeclToRLS(buffer)>;
%endfunction
%function FcnCacheSFunctionExternDeclToRLS(buffer) void
%if !CGMODEL_ACCESS("CGModel.isCodeGenContextBuild")
%return
%endif
%assign sysIdx = CGMODEL_ACCESS("CGModel.getCUTSysIdx")
%assign system = ::CompiledModel.System[sysIdx]
%<SLibCacheSystemCodeToFile("sys_standalone_extern_data_decl", system, buffer)>
%endfunction
%function LibDumpExternsCache() Output
%if !WHITE_SPACE(ExternsCache)
%<ExternsCache>/
%endif
%<SLibResetTLCGlobal("ExternsCache")>/
%endfunction
%function SLibCacheSourceCode(buffer) void
%assign ::CompiledModel.SourceCodeCache = ...
::CompiledModel.SourceCodeCache + buffer
%endfunction
%function SLibDumpSourceCodeCache() Output
%if !WHITE_SPACE(SourceCodeCache)
%<SourceCodeCache>/
%endif
%<SLibResetTLCGlobal("SourceCodeCache")>/
%foreach idx = ::CompiledModel.NumFiles
%openfile file = ::CompiledModel.Files.Name[idx]
%<::CompiledModel.Files.File[idx]>
%closefile file
%<SLibAddGeneratedFileToList(::CompiledModel.Files.Name[idx] ,"other","other","")>
%if ResetTLCGlobalsAfterUse
%assign ::CompiledModel.Files.File[idx] = 0
%endif
%endforeach
%endfunction
%function LibCacheGlobalPrmData(buffer) void
%if UsingMalloc
%openfile errTxt
Attempting to declare global parameter data using TLC function
LibCacheGlobalPrmData.
Declaring global parameter data is not consistent with dynamic ...
memory allocation. Perhaps you should create the data in the ...
block's start function.
For example, assume a block requires constant and non-constant ...
data. The block creates the data in the start function and stores ...
the location of the data in its PWork. References to the data ...
outside the start function are realized via the block PWork.
%<"%function Start(block, system) Output">
/* initialize data (containing non-finites) */
{
static real_T nonFiniteData[] = { 1.0, 0.0, 27.2, 0.0 };
nonFiniteData[1] = %<"%<LibRealNonFinite(inf)>">;
nonFiniteData[3] = %<"%<LibRealNonFinite(nan)>">;
%<"%<LibBlockPWork(ConstNonFiniteData,"","",0)> = ">
&nonFiniteData[0];
}
/* initialize data (all finite) */
{
static const real_T finiteData[] = { 1.0, 12.0, 27.2, 45.0 };
%<"%<LibBlockPWork(ConstFiniteData,"","",0)> =">
&finiteData[0];
}
/* allocate memory for non-constant data */
{
SomeDataType *data;
data = (SomeDataType *) malloc(sizeof(SomeDataType));
VALIDATE_MEMORY(%<"%<RTMGetModelSS()>">, data);
%<"%<LibBlockPWork(NonConstData,"","",0)> = data;">
}
%<"%endfunction">
Note that static data is only declared with the "const" keyword if ...
the data does not contain non-finites since rtInf, rtMinusInf, and ...
rtNaN are not initialized until run time.
Also note that the TLC references ConstNonFiniteData, ConstFiniteData, ...
and NonConstData are made possible with either the TLC function ...
LibDefinePWork or with the S-function's mdlRTW function.
%closefile errTxt
%<SLibReportErrorWithId("RTW:tlc:GenericMsg", errTxt)>
%endif
%assign ::CompiledModel.GlobalPrmDataCache = ...
::CompiledModel.GlobalPrmDataCache + buffer
%endfunction
%function LibDumpGlobalPrmDataCache() Output
%if !WHITE_SPACE(GlobalPrmDataCache)
%<GlobalPrmDataCache>/
%endif
%<SLibResetTLCGlobal("GlobalPrmDataCache")>/
%endfunction
%function SLibCacheIntegerOnlyWarning(name,component) void
%assign foundBlock = 0
%foreach idx = IntegerOnlyWarningCache.NumBlocks
%if IntegerOnlyWarningCache.Block[idx].Name == name
%assign foundBlock = 1
%assign result = FEVAL("findstr", ...
::CompiledModel.IntegerOnlyWarningCache.Block[idx].Component, component)
%if !SIZE(result,0)
%assign ::CompiledModel.IntegerOnlyWarningCache.Block[idx].Component = ...
IntegerOnlyWarningCache.Block[idx].Component + " %<component>"
%endif
%break
%endif
%endforeach
%if !foundBlock
%assign tmpVar = Block/
{/
Name name;/
Component component/
}
%assign ::CompiledModel.IntegerOnlyWarningCache = ...
IntegerOnlyWarningCache + Block
%undef tmpVar
%assign ::CompiledModel.IntegerOnlyWarningCache.NumBlocks = ...
IntegerOnlyWarningCache.NumBlocks + 1
%endif
%endfunction
%function SLibDumpIntegerOnlyWarning() void
%if IntegerOnlyWarningCache.NumBlocks > 0
%openfile warnTxt
The following blocks contain non-integer data but the Integer Code Only
option is selected. You should replace/correct the blocks or uncheck
Integer Code Only via the Code Generation Options dialog.
%if IntegerOnlyWarningCache.NumBlocks > 25
%assign loop = 20
Total of %<IntegerOnlyWarningCache.NumBlocks> Blocks, only listing first 20.
%else
%assign loop = IntegerOnlyWarningCache.NumBlocks
%endif
Block (details) :
%foreach idx = loop
% ...
(%)
%endforeach
%closefile warnTxt
%selectfile STDOUT
%<warnTxt>
%selectfile NULL_FILE
%<SLibReportWarningWithIdAndArgs("RTW:tlc:GenericWarn", warnTxt)>
%endif
%endfunction
%function LibGetFieldSet(scope, fieldName) void
%if ConfigSet.TLCAssert
%return ISFIELD(scope, "FieldSet" + fieldName)
%else
%return 1
%endif
%endfunction
%function LibSetFieldSet(scope, fieldName) void
%if ConfigSet.TLCAssert
%if !ISFIELD(scope, "FieldSet" + fieldName)
%addtorecord scope FieldSet%<fieldName> 1
%endif
%endif
%endfunction
%endif