%%
%%Copyright2015-2017TheMathWorks,Inc.
%%
%%Abstract:
%%ThissystemTLClibraryfilecontainsfunctionsthatarerelatedto
%%SimulinkVarGroups
%%
%if EXISTS("_VARGROUPLIB_") == 0
%assign _VARGROUPLIB_ = 1
 
%%Function:SLibVarGroupIsStruct================================================
%%Abstract:
%%ReturnsTLC_TRUEifagivenvargroupisgeneratedasastructure
%%
%function SLibVarGroupIsStruct(aVarGroupIdx) void
  %assert ::CompiledModel.VarGroups.NumVarGroups > aVarGroupIdx
  %assert -1 < aVarGroupIdx
  %assign varGroup = ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx]
  %assert 0 == varGroup.PackageAsStruct || -1 < varGroup.CGTypeIdx
  %return 1 == varGroup.PackageAsStruct
%endfunction
 
%%Function:SLibVarGroupLocalName===============================================
%%Abstract:
%%Returnsthelocalnameofagivenvargroup
%%
%function SLibVarGroupLocalName(aVarGroupIdx) void
  %assert ::CompiledModel.VarGroups.NumVarGroups > aVarGroupIdx
  %assert -1 < aVarGroupIdx
  %return ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx].LocalName
%endfunction
 
%%Function:SLibVarGroupSystemIdx=============================================
%%Abstract:
%%Returnstheownersystemindexofagivenvargroup
%%
%function SLibVarGroupSystemIdx(aVarGroupIdx) void
  %assert ::CompiledModel.VarGroups.NumVarGroups > aVarGroupIdx
  %assert -1 < aVarGroupIdx
  %return ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx].SysIdx
%endfunction
 
 
%%Function:SLibVarGroupNumElements=============================================
%%Abstract:
%%Returnsthenumberofelementsinagivenvargroup
%%
%function SLibVarGroupNumElements(aVarGroupIdx) void
  %assert ::CompiledModel.VarGroups.NumVarGroups > aVarGroupIdx
  %assert -1 < aVarGroupIdx
  %return ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx].NumVarGroupElements
%endfunction
 
%%Function:SLibVarGroupElementSource===========================================
%%Abstract:
%%Returnsthesourcerecordidnumstringforagivenvargroupandelementindex
%%
%function SLibVarGroupElementSource(aVarGroupIdx, aElementIdx) void
  %assert SLibVarGroupNumElements(aVarGroupIdx) > aElementIdx
  %assert -1 < aElementIdx
  %return ...
    ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx].VarGroupElements[aElementIdx]
%endfunction
 
%%Function:SLibDefineVarGroupElement===========================================
%%Abstract:
%%Returnstrueifagivenvargoupelementisdefinedtheassociateddata
%%structure.
%%
%function SLibDefineVarGroupElement(aVarGroupIdx, aElementIdx) void
  %assign systemIdx = SLibVarGroupSystemIdx(aVarGroupIdx)
  %assign varGroupElement = SLibVarGroupElementSource(aVarGroupIdx, aElementIdx)
  %assign res = SLibNeedGenDataDefFromVarGroup(varGroupElement, systemIdx)
  %return TLC_TRUE == res[0] || !WHITE_SPACE(res[2])
%endfunction
 
%%Function:SLibVarGroupCGTypeIndex==================================================
%%Abstract:
%%ReturnstheCGTypeindexforavargoup
%%
%function SLibVarGroupCGTypeIdx(aVarGroupIdx) void
  %assign varGroupTypeIdx = ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx].CGTypeIdx
  %if LibCGTypeIsMatrix(varGroupTypeIdx)
    %assign varGroupTypeIdx = SLibGetBaseTypeForVarGroupUsedAsMatrix(varGroupTypeIdx)
  %endif
  %return varGroupTypeIdx
%endfunction
 
%%Function:SLibVarGroupElementName=============================================
%%Abstract:
%%Returnsthevargroupelementnameforagivenvargroupandelementindex
%%
%function SLibVarGroupElementName(aVarGroupIdx, aElementIdx) void
  %assert SLibVarGroupNumElements(aVarGroupIdx) > aElementIdx
  %assert -1 < aElementIdx
  %assign varGroup = ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx]
  %if 0 == varGroup.PackageAsStruct
    %return varGroup.VarGroupElementIds[aElementIdx]
  %endif
  %assign cgTypeIdx = SLibVarGroupCGTypeIdx(aVarGroupIdx)
  %return LibCGTypeMemberName(cgTypeIdx, aElementIdx)
%endfunction
 
%%Function:SLibVarGroupSetElementName==========================================
%%Abstract:
%%Returnsthevargroupelementnameforagivenvargroupandelementindex
%%
%function SLibVarGroupSetElementName(aVarGroupIdx,aElementIdx,aNewIdentifier) void
  %assert SLibVarGroupNumElements(aVarGroupIdx) > aElementIdx
  %assert -1 < aElementIdx
  %assign varGroup = ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx]
  %if 0 == varGroup.PackageAsStruct
    %assign varGroup.VarGroupElementIds[aElementIdx] = aNewIdentifier
  %endif
  %assign cgTypeIdx = SLibVarGroupCGTypeIdx(aVarGroupIdx)
  %return CGMODEL_ACCESS("CGType.Member.SetName", STRING(cgTypeIdx), STRING(aElementIdx), aNewIdentifier)
%endfunction
 
%%Function:SLibVarGroupElementType=============================================
%%Abstract:
%%ReturnsthevargroupelementCGtypeindexforagivenvargroup
%%andelementindex
%%
%function SLibVarGroupElementType(aVarGroupIdx, aElementIdx) void
  %assert SLibVarGroupNumElements(aVarGroupIdx) > aElementIdx
  %assert -1 < aElementIdx
  %assign varGroup = ::CompiledModel.VarGroups.VarGroup[aVarGroupIdx]
  %if 0 == varGroup.PackageAsStruct
    %return varGroup.VarGroupElementCGTypes[aElementIdx]
  %endif
  %assign cgTypeIdx = SLibVarGroupCGTypeIdx(aVarGroupIdx)
  %return LibCGTypeMemberCGTypeIdx(cgTypeIdx, aElementIdx)
%endfunction
 
%%Function:SLibVarGroupElementIsVolatile====================================
%%Abstract:
%%Returnswhetherthevargroupelementisvolatile
%%
%function SLibVarGroupElementIsVolatile(aVarGroupIdx, aElementIdx) void
  %assert SLibVarGroupNumElements(aVarGroupIdx) > aElementIdx
  %assert -1 < aElementIdx
  %assign source = SLibVarGroupElementSource(aVarGroupIdx,aElementIdx)
  %assign idnum = IDNUM(source)
  %if idnum[0] == "W"
    %assign dwRec = ::CompiledModel.DWorks.DWork[idnum[1]]
    %return ISFIELD(dwRec, "IsDataTransferVolatileDWork") && ...
      (dwRec.IsDataTransferVolatileDWork == 1)
  %else
    %return TLC_FALSE
  %endif
%endfunction
 
%%Function:LibGetVarGroupCategory==========================================
%%Abstract:
%%Returnsthevargroupcategoryforagivenvargroupindex
%%
%function LibGetVarGroupCategory(varGroupIdx) void
  %assert varGroupIdx >= 0
  %return CGMODEL_ACCESS("VarGroups.Category",varGroupIdx)
%endfunction
 
%%Function:SLibGetRecordVarGroupIdx===========================================
%%Abstract:
%%ReturnstheVarGroupIdxofadatarecord.Returns[-1,-1]ifitdoesn'texist.
%%
%function SLibGetRecordVarGroupIdx(aDataRec) void
  %assign retVal = [-1, -1]
  %if ISFIELD(aDataRec, "VarGroupIdx")
    %assign retVal = aDataRec.VarGroupIdx
  %endif
  %return retVal
%endfunction
 
%%Function:SLibIsLocalVariableRecordType=====================================
%%Abstract:
%%Returnstrueifandonlyiftheprovidedrecordtypecorrespondstoalocalvariable
%%
%function SLibIsLocalVariableRecordType(aVarGroupIdx, aElementIdx) void
  %assign idNum = IDNUM(SLibVarGroupElementSource(aVarGroupIdx, aElementIdx))
  %assign recType = idNum[0]
  %return recType == "L" || recType == "LDW" || recType == "S"
%endfunction
 
 
%endif%% _VARGROUPLIB_