Bind.Structures.Constant.TranslateConstantWithReference C# (CSharp) Method

TranslateConstantWithReference() public static method

Replces the Value of the given constant with the value referenced by the [c.Reference, c.Value] pair.
public static TranslateConstantWithReference ( Constant c, EnumCollection enums, EnumCollection auxEnums ) : bool
c Constant The Constant to translate
enums EnumCollection The list of enums to check.
auxEnums EnumCollection The list of auxilliary enums to check.
return bool
        public static bool TranslateConstantWithReference(Constant c, EnumCollection enums, EnumCollection auxEnums)
        {
            if (!String.IsNullOrEmpty(c.Reference))
            {
                Constant referenced_constant;

                if (enums.ContainsKey(c.Reference) && enums[c.Reference].ConstantCollection.ContainsKey(c.Value))
                {
                    TranslateConstantWithReference(enums[c.Reference].ConstantCollection[c.Value] as Constant, enums, auxEnums);
                    referenced_constant = (enums[c.Reference].ConstantCollection[c.Value] as Constant);
                }
                else if (auxEnums.ContainsKey(c.Reference) && auxEnums[c.Reference].ConstantCollection.ContainsKey(c.Value))
                {
                    TranslateConstantWithReference(auxEnums[c.Reference].ConstantCollection[c.Value] as Constant, enums, auxEnums);
                    referenced_constant = (auxEnums[c.Reference].ConstantCollection[c.Value] as Constant);
                }
                else
                {
                    Console.WriteLine("[Warning] Reference {0} not found for token {1}.", c.Reference, c);
                    return false;
                }
                //else throw new InvalidOperationException(String.Format("Unknown Enum \"{0}\" referenced by Constant \"{1}\"",
                //                                                       c.Reference, c.ToString()));

                c.Value = referenced_constant.Value;
                c.Reference = null;
                c.Unchecked = referenced_constant.Unchecked;
            }

            return true;
        }

Usage Example

Beispiel #1
0
        internal void Translate(XPathDocument overrides)
        {
            if (overrides == null)
            {
                throw new ArgumentNullException("overrides");
            }

            string path = "/overrides/replace/enum[@name='{0}']";

            // Translate enum names.
            {
                List <string> keys_to_update = new List <string>();
                foreach (Enum e in Values)
                {
                    string name = e.Name;

                    XPathNavigator enum_override = overrides.CreateNavigator().SelectSingleNode(String.Format(path, name));
                    if (enum_override != null)
                    {
                        XPathNavigator name_override = enum_override.SelectSingleNode("name");
                        if (name_override != null)
                        {
                            name = name_override.Value;
                        }
                    }

                    name = Enum.TranslateName(name);
                    if (name != e.Name)
                    {
                        keys_to_update.Add(e.Name);
                        e.Name = name;
                    }
                }

                foreach (string name in keys_to_update)
                {
                    Enum e = this[name];
                    Remove(name);
                    Add(e.Name, e);
                }

                keys_to_update = null;
            }

            foreach (Enum e in Values)
            {
                XPathNavigator enum_override = overrides.CreateNavigator().SelectSingleNode(String.Format(path, e.Name));
                foreach (Constant c in e.ConstantCollection.Values)
                {
                    if (enum_override != null)
                    {
                        XPathNavigator constant_override = enum_override.SelectSingleNode(String.Format("token[@name='{0}']", c.PreviousName)) ??
                                                           enum_override.SelectSingleNode(String.Format("token[@name={0}]", c.Name));
                        if (constant_override != null)
                        {
                            foreach (XPathNavigator node in constant_override.SelectChildren(XPathNodeType.Element))
                            {
                                switch (node.Name)
                                {
                                case "name": c.Name = (string)node.TypedValue; break;

                                case "value": c.Value = (string)node.TypedValue; break;
                                }
                            }
                        }
                    }

                    // There are cases when a value is an aliased constant, with no enum specified.
                    // (e.g. FOG_COORD_ARRAY_TYPE = GL_FOG_COORDINATE_ARRAY_TYPE)
                    // In this case try searching all enums for the correct constant to alias (stupid opengl specs).
                    if (String.IsNullOrEmpty(c.Reference) && !Char.IsDigit(c.Value[0]))
                    {
                        foreach (Enum @enum in Values)
                        {
                            // Skip generic GLenum
                            if (@enum.Name == "GLenum")
                            {
                                continue;
                            }

                            if (@enum.ConstantCollection.ContainsKey(c.Value))
                            {
                                c.Reference = @enum.Name;
                                break;
                            }
                        }
                    }
                }
            }

            foreach (Enum e in Values)
            {
restart:
                foreach (Constant c in e.ConstantCollection.Values)
                {
                    bool result = Constant.TranslateConstantWithReference(c, Enum.GLEnums, Enum.AuxEnums);
                    if (!result)
                    {
                        e.ConstantCollection.Remove(c.Name);
                        goto restart;
                    }
                }
            }

            if (Settings.DropMultipleTokens)
            {
                // When there are multiple tokens with the same value but different extension
                // drop the duplicates. Order of preference: core > ARB > EXT > vendor specific

                List <Constant> removed_tokens = new List <Constant>();

                foreach (Enum e in Values)
                {
                    if (e.Name == "All")
                    {
                        continue;
                    }

                    // This implementation is a not very bright O(n^2).
                    foreach (Constant c in e.ConstantCollection.Values)
                    {
                        foreach (Constant c2 in e.ConstantCollection.Values)
                        {
                            if (c.Name != c2.Name && c.Value == c2.Value)
                            {
                                int prefer = OrderOfPreference(Utilities.GetGL2Extension(c.Name), Utilities.GetGL2Extension(c2.Name));
                                if (prefer == -1)
                                {
                                    removed_tokens.Add(c2);
                                }
                                else if (prefer == 1)
                                {
                                    removed_tokens.Add(c);
                                }
                            }
                        }
                    }

                    foreach (Constant c in removed_tokens)
                    {
                        e.ConstantCollection.Remove(c.Name);
                    }
                    removed_tokens.Clear();
                }
            }
        }