Beispiel #1
0
        /// <summary>
        /// Maps a C++ Enum to a C# enum.
        /// </summary>
        /// <param name="cppEnum">the c++ enum.</param>
        private void Process(CsEnum newEnum)
        {
            var cppEnum = (CppEnum) newEnum.CppElement;

            // Get tag from C++ enum
            var tag = cppEnum.GetTagOrDefault<MappingRule>();

            // Determine enum type. Default is int
            string typeName = cppEnum.GetTypeNameWithMapping();
            switch (typeName)
            {
                case "byte":
                    newEnum.Type = typeof(byte);
                    newEnum.SizeOf = 1;
                    break;
                case "short":
                    newEnum.Type = typeof(short);
                    newEnum.SizeOf = 1;
                    break;
                case "int":
                    newEnum.Type = typeof(int);
                    newEnum.SizeOf = 4;
                    break;
                default:
                    Logger.Error("Invalid type [{0}] for enum [{1}]. Types supported are : int, byte, short", typeName, cppEnum);
                    break;
            }

            // Find Root Name of this enum
            // All enum items should start with the same root name and the root name should be at least 4 chars
            string rootName = cppEnum.Name;
            string rootNameFound = null;
            bool isRootNameFound = false;
            for (int i = rootName.Length; i >= 4 && !isRootNameFound; i--)
            {
                rootNameFound = rootName.Substring(0, i);

                isRootNameFound = true;
                foreach (var cppEnumItem in cppEnum.EnumItems)
                {
                    if (!cppEnumItem.Name.StartsWith(rootNameFound))
                    {
                        isRootNameFound = false;
                        break;
                    }
                }
            }
            if (isRootNameFound)
                rootName = rootNameFound;

            // Create enum items for enum
            foreach (var cppEnumItem in cppEnum.EnumItems)
            {
                string enumName = NamingRules.Rename(cppEnumItem, rootName);
                string enumValue = cppEnumItem.Value;

                var csharpEnumItem = new CsEnumItem(enumName, enumValue) { CppElement = cppEnumItem };

                newEnum.Add(csharpEnumItem);

                if (cppEnumItem.Name != "None")
                    Manager.BindType(cppEnumItem.Name, csharpEnumItem);
            }

            bool tryToAddNone = tag.EnumHasNone.HasValue ? tag.EnumHasNone.Value : false;

            // If C++ enum name is ending with FLAG OR FLAGS
            // Then tag this enum as flags and add None if necessary
            if (cppEnum.Name.EndsWith("FLAG") || cppEnum.Name.EndsWith("FLAGS"))
            {
                newEnum.IsFlag = true;

                if (!tag.EnumHasNone.HasValue)
                    tryToAddNone = !newEnum.Items.Cast<CsEnumItem>().Any(item => item.Name == "None");
            }

            // Add None value
            if (tryToAddNone)
            {
                var csharpEnumItem = new CsEnumItem("None", "0");
                csharpEnumItem.CppElement = new CppElement { Description = "None." };
                newEnum.Add(csharpEnumItem);
            }
        }