/// <summary> /// used by <see cref="CreateFieldsAuto"/> /// </summary> static private void AddToIO(string iName, DGField fld, //AppControl ctrl, IDictionary <string, FieldOpts> FieldOptions, ICollection <DGField> IOFields, InstantiateFromControlFileAttribute at) { FieldOpts fopts = FieldOptions.Where(kv => WildcardToRegex(kv.Key).IsMatch(fld.Identification)).SingleOrDefault().Value; if (fopts != null) { if (at.m_ioListOpt == IOListOption.Always && fopts.SaveToDB == FieldOpts.SaveToDBOpt.FALSE) { throw new ApplicationException("IO for field '" + fld.Identification + "' cannot be turned OFF, i.e. 'SaveToDB==false' is illegal."); } if (at.m_ioListOpt == IOListOption.Never && fopts.SaveToDB == FieldOpts.SaveToDBOpt.TRUE) { throw new ApplicationException("IO for field '" + fld.Identification + "' cannot be turned ON, i.e. 'SaveToDB==true' is illegal"); } if (at.m_ioListOpt == IOListOption.Always || fopts.SaveToDB == FieldOpts.SaveToDBOpt.TRUE) { IOFields.Add(fld); } } else { if (at.m_ioListOpt == IOListOption.Always) { IOFields.Add(fld); } } }
/// <summary> /// used by <see cref="CreateFieldsAuto"/> /// </summary> static private void AddToIO(string iName, DGField fld, //AppControl ctrl, IDictionary <string, FieldOpts> FieldOptions, ICollection <DGField> IOFields, InstantiateFromControlFileAttribute at) { FieldOpts fopts; bool isSpec = FieldOptions.TryGetValue(iName, out fopts); if (isSpec) { if (at.m_ioListOpt == IOListOption.Always && fopts.SaveToDB == FieldOpts.SaveToDBOpt.FALSE) { throw new ApplicationException("IO for field '" + fld.Identification + "' cannot be turned OFF, i.e. 'SaveToDB==false' is illegal."); } if (at.m_ioListOpt == IOListOption.Never && fopts.SaveToDB == FieldOpts.SaveToDBOpt.TRUE) { throw new ApplicationException("IO for field '" + fld.Identification + "' cannot be turned ON, i.e. 'SaveToDB==true' is illegal"); } if (at.m_ioListOpt == IOListOption.Always || fopts.SaveToDB == FieldOpts.SaveToDBOpt.TRUE) { IOFields.Add(fld); } } else { if (at.m_ioListOpt == IOListOption.Always) { IOFields.Add(fld); } } }
/// <summary> /// still a hack... /// </summary> static object InstantiateFromAttribute(FieldInfo f, InstantiateFromControlFileAttribute at, Type type, ICollection <DGField> IOFields, ICollection <DGField> RegisteredFields, //AppControl ctrl, IDictionary <string, FieldOpts> FieldOptions, IGridData ctx, LevelSetTracker lstrk) { // create instance // =============== object member_value = null; Type HistoryType = null; Type VectorType = null; Type ComponentType = null; GetTypes(f, out HistoryType, out VectorType, out ComponentType); if (VectorType != null) { // vector field branch // +++++++++++++++++++ if (at.m_IsScalarField) { throw new ApplicationException("illegal use of 'InstantiateFromControlFileAttribute' (with Scalar Declaration) on a Vector class"); } int D = ctx.SpatialDimension; string[] cName = at.GetControlFileNames(f, D); string[] iName = at.GetInCodeIdentifications(f, D); // determine DG polynomial degree of basis int[] Deg = new int[D]; for (int d = 0; d < D; d++) { Deg[d] = GetDegree(cName[d], iName[d], FieldOptions); } if (at.m_DegreesMustBeEqual) { int deg0 = Deg[0]; for (int d = 1; d < D; d++) { if (Deg[d] != deg0) { StringWriter errMsg = new StringWriter(); errMsg.Write("DG Polynomial degree of fields {"); for (int dd = 0; dd < D; dd++) { errMsg.Write(cName[dd]); if (dd < D - 1) { errMsg.Write(", "); } } errMsg.Write("} must be equal, but found {"); for (int dd = 0; dd < D; dd++) { errMsg.Write(Deg[dd]); if (dd < D - 1) { errMsg.Write(", "); } } errMsg.Write("} in control file."); throw new ApplicationException(errMsg.ToString()); } } } // create instance: components SinglePhaseField[] fld = new SinglePhaseField[D]; XDGField[] xfld = new XDGField[D]; DGField[] _fld = new DGField[D]; for (int d = 0; d < D; d++) { if (ComponentType == typeof(SinglePhaseField)) { fld[d] = new SinglePhaseField(new Basis(ctx, Deg[d]), iName[d]); _fld[d] = fld[d]; } else if (ComponentType == typeof(XDGField)) { xfld[d] = new XDGField(new XDGBasis(lstrk, Deg[d]), iName[d]); _fld[d] = xfld[d]; fld = null; } else { throw new NotSupportedException("unknown type."); } RegisteredFields.Add(_fld[d]); } // create instance: Vector-Field container var ci = VectorType.GetConstructor(new Type[] { ComponentType.MakeArrayType() }); member_value = ci.Invoke(new object[] { (fld != null) ? ((object)fld) : ((object)xfld) }); //member_value = ci.Invoke( new object[] { ((object)fld) }); // io for (int d = 0; d < D; d++) { AddToIO(iName[d], _fld[d], FieldOptions, IOFields, at); } } else { // scalar field branch // +++++++++++++++++++ if (at.m_IsVectorField) { throw new ApplicationException("illegal use of 'InstantiateFromControlFileAttribute' (with Vector Declaration) on a Non-Vector class"); } // identification string cName = at.GetControlFileName(f); string iName = at.GetInCodeIdentification(f); // create basis int Deg = GetDegree(cName, iName, FieldOptions); Basis b = new Basis(ctx, Deg); // create instance DGField fld = null; if (ComponentType == typeof(SinglePhaseField)) { fld = new SinglePhaseField(b, iName); } else if (ComponentType == typeof(LevelSet)) { fld = new LevelSet(b, iName); } else if (ComponentType == typeof(XDGField)) { fld = new XDGField(new XDGBasis(lstrk, Deg), iName); } else { throw new NotImplementedException(); } RegisteredFields.Add(fld); AddToIO(iName, fld, FieldOptions, IOFields, at); member_value = fld; } // History, if desired if (HistoryType != null) { member_value = (HistoryType.GetConstructors()[0]).Invoke(new object[] { member_value }); } return(member_value); }