Exemplo n.º 1
0
        public EnumsGeneratorItem WithEnum(string typeName, string s, ParserKind parserKind = ParserKind.Default)
        {
            var tmp = new EnumsGeneratorItem
            {
                Names          = typeName,
                Options        = OptionsCollection.Parse(s, parserKind),
                TargetAssembly = _currentTargetAssembly
            };

            ItemsToCreate.Add(tmp);
            return(tmp);
        }
Exemplo n.º 2
0
        private string Extensions_CheckConflictsCode(EnumsGeneratorItem item)
        {
            var conflicts = item.Options.IncompatibleValues.Where(a => a != null).ToArray();

            if (conflicts.Length == 0)
            {
                return(null);
            }
            var code = new CsCodeWriter();
            // check conflicts
            var optionToItem = item.Options.GetMap();

            var filter = new CodeVariable("flagsFilter");

            var duplicates = new HashSet <string>();

            foreach (var conflict in conflicts)
            {
                var first        = optionToItem[conflict.Value1];
                var second       = optionToItem[conflict.Value2];
                var argItemNames = new[] { first.GetCsName(), second.GetCsName() };
                if (argItemNames[0] == argItemNames[1])
                {
                    throw new Exception("Both options means the same");
                }

                if (!duplicates.Add(argItemNames[0] + "," + argItemNames[1]))
                {
                    continue;
                }

                filter.Expression = GetMask(MyEnum, argItemNames);
                var errorMessage = string.Format("options {0} can't be used together",
                                                 string.Join(" and ", first.AnyWithMinus, second.AnyWithMinus))
                                   .CsEncode();

                code.WriteLine(filter.GetCodeAndMarkAsDeclared());
                code.SingleLineIf($"({valueVariable} & {filter.Name}) == {filter.Name}",
                                  "throw new Exception(" + errorMessage + ");");
            }

            var m = ExtensionsClass
                    .AddMethod("CheckConflicts", "void")
                    .WithStatic()
                    .WithBody(code);

            m.AddParam(valueVariable, MyEnum.Name).UseThis = true;
            return(m.Name);
        }
Exemplo n.º 3
0
 private SingleTaskEnumsGenerator(ISingleTaskEnumsGeneratorContext context, EnumsGeneratorItem item)
 {
     _item         = item;
     _context      = context;
     _lazyMyStruct = new Lazy <CsClass>(() =>
     {
         var res       = MyNamespace.GetOrCreateClass(OptionsClassName);
         res.IsPartial = true;
         return(res);
     });
     _lazyMyNamespace     = new Lazy <CsNamespace>(() => _context.GetOrCreateNamespace(_item.Namespace));
     _lazyMyEnum          = new Lazy <CsEnum>(() => new CsEnum(MyEnumTypeName));
     _lazyExtensionsClass = new Lazy <CsClass>(() =>
     {
         var extensionsClass = MyNamespace.GetOrCreateClass(_item.EnumName + "Extensions")
                               .WithStatic();
         extensionsClass.IsPartial = true;
         return(extensionsClass);
     });
 }
Exemplo n.º 4
0
        public static void CreateCode(ISingleTaskEnumsGeneratorContext context, EnumsGeneratorItem item)
        {
            var stat = new SingleTaskEnumsGenerator(context, item);

            stat.CreateCode();
        }