//////////////////////////////////////////////////////////////////////////////////////////////////// // traits that change attributes //////////////////////////////////////////////////////////////////////////////////////////////////// internal ResolvedAttributeSet ApplyTraits(ResolvedTraitSet traits, ResolveOptions resOpt, CdmAttributeResolutionGuidance resGuide, List <AttributeResolutionApplier> actions) { ResolvedAttributeSet rasResult = this; ResolvedAttributeSet rasApplied; if (this.RefCnt > 1 && rasResult.CopyNeeded(traits, resOpt, resGuide, actions)) { rasResult = rasResult.Copy(); } rasApplied = rasResult.Apply(traits, resOpt, resGuide, actions); // now we are that rasResult.ResolvedName2resolvedAttribute = rasApplied.ResolvedName2resolvedAttribute; rasResult.BaseTrait2Attributes = null; rasResult.Set = rasApplied.Set; return(rasResult); }
ResolvedAttributeSet Apply(ResolvedTraitSet traits, CdmAttributeResolutionGuidance resGuide, List <AttributeResolutionApplier> actions) { if (traits == null && actions.Count == 0) { // nothing can change return(this); } // for every attribute in the set run any attribute appliers ResolvedAttributeSet appliedAttSet = new ResolvedAttributeSet(); int l = this.Set.Count; appliedAttSet.AttributeContext = this.AttributeContext; // check to see if we need to make a copy of the attributes // do this when building an attribute context and when we will modify the attributes (beyond traits) // see if any of the appliers want to modify bool makingCopy = false; if (l > 0 && appliedAttSet.AttributeContext != null && actions != null && actions.Count > 0) { ResolvedAttribute resAttTest = this.Set[0]; foreach (AttributeResolutionApplier traitAction in actions) { ApplierContext ctx = new ApplierContext { ResOpt = traits.ResOpt, ResAttSource = resAttTest, ResGuide = resGuide }; if (traitAction.WillAttributeModify(ctx) == true) { makingCopy = true; break; } } } if (makingCopy) { // fake up a generation round for these copies that are about to happen AttributeContextParameters acp = new AttributeContextParameters { under = appliedAttSet.AttributeContext, type = Enums.CdmAttributeContextType.GeneratedSet, Name = "_generatedAttributeSet" }; appliedAttSet.AttributeContext = CdmAttributeContext.CreateChildUnder(traits.ResOpt, acp); acp = new AttributeContextParameters { under = appliedAttSet.AttributeContext, type = Enums.CdmAttributeContextType.GeneratedRound, Name = "_generatedAttributeRound0" }; appliedAttSet.AttributeContext = CdmAttributeContext.CreateChildUnder(traits.ResOpt, acp); } for (int i = 0; i < l; i++) { ResolvedAttribute resAtt = this.Set[i]; ResolvedAttributeSet subSet = resAtt.Target as ResolvedAttributeSet; CdmAttributeContext attCtxToMerge = null; if (subSet?.Set != null) { if (makingCopy) { resAtt = resAtt.Copy(); } // the set contains another set. process those resAtt.Target = subSet.Apply(traits, resGuide, actions); } else { ResolvedTraitSet rtsMerge = resAtt.ResolvedTraits.MergeSet(traits); resAtt.ResolvedTraits = rtsMerge; if (actions != null) { foreach (AttributeResolutionApplier traitAction in actions) { ApplierContext ctx = new ApplierContext { ResOpt = traits.ResOpt, ResAttSource = resAtt, ResGuide = resGuide }; if (traitAction.WillAttributeModify(ctx) == true) { // make a context for this new copy if (makingCopy) { AttributeContextParameters acp = new AttributeContextParameters { under = appliedAttSet.AttributeContext, type = Enums.CdmAttributeContextType.AttributeDefinition, Name = resAtt.ResolvedName, Regarding = resAtt.Target }; ctx.AttCtx = CdmAttributeContext.CreateChildUnder(traits.ResOpt, acp); attCtxToMerge = ctx.AttCtx as CdmAttributeContext; } // make a copy of the resolved att if (makingCopy) { resAtt = resAtt.Copy(); } ctx.ResAttSource = resAtt; // modify it traitAction.DoAttributeModify(ctx); } } } } appliedAttSet.Merge(resAtt, attCtxToMerge); } appliedAttSet.AttributeContext = this.AttributeContext; if (!makingCopy) { // didn't copy the attributes or make any new context, so just take the old ones appliedAttSet.Ra2attCtxSet = this.Ra2attCtxSet; appliedAttSet.AttCtx2ra = this.AttCtx2ra; } return(appliedAttSet); }