Ejemplo n.º 1
0
            public NamespaceDecorator(PropertyTypeNode container, StringBuffer sb)
            {
                _sb        = sb;
                _container = container;

                GenerateHeader();
            }
Ejemplo n.º 2
0
        private static string GenerateInitializerFromProperty(
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType)
        {
            // No initializer for structs
            if (containerTypeTag == PropertyTypeNode.TypeTag.Struct)
            {
                return(string.Empty);
            }

            var type = TypeDeclarationStringForProperty(propertyType);

            if (PropertyTypeNode.IsEnumerableType(propertyType.Tag) || propertyType.Tag == PropertyTypeNode.TypeTag.Struct)
            {
                // TODO value type etc.
                return($"new {type} {{}}");
            }

            if (propertyType.Tag == PropertyTypeNode.TypeTag.Class)
            {
                return($"new {type} ()");
            }

            return(!string.IsNullOrEmpty(propertyType.DefaultValue)
                ? propertyType.DefaultValue
                : string.Empty);
        }
Ejemplo n.º 3
0
        private void GeneratePropertyBag(PropertyTypeNode c, List <string> propertyNames, Scope scope)
        {
            var gen = scope.Code;

            gen.Append(' ', Style.Space * 1);
            gen.Append($"public IPropertyBag PropertyBag => {PropertyBagStaticVarName};");
            gen.Append(Environment.NewLine); gen.Append(Environment.NewLine);

            gen.Append(' ', Style.Space * 1);

            const AccessModifiers modifiers = AccessModifiers.Private | AccessModifiers.Static | AccessModifiers.Readonly;

            gen.Append($"{string.Join(" ", ModifiersToStrings(modifiers))} PropertyBag {PropertyBagStaticVarName};");

            // TODO should be ordered (after property wrappers creation)

            var propertyBagInitializers = propertyNames != null
                ? string.Join(", ", propertyNames)
                : string.Empty;

            var initializer = $@"new PropertyBag(new List<IProperty>{{
                        {propertyBagInitializers}
                    }}.ToArray())";

            AddStaticConstructorInStageFragment(
                ConstructorStage.PropertyInitializationStage,
                new CSharpGenerationFragmentContext()
            {
                Scope    = scope,
                Fragment = $"{PropertyBagStaticVarName} = {initializer};"
            })
            ;

            gen.Append(Environment.NewLine); gen.Append(Environment.NewLine);
        }
Ejemplo n.º 4
0
        private static string GeneratePropertyBackingField(
            string propertyName,
            PropertyTypeNode propertyType,
            out string propertyAccessorName)
        {
            if (propertyType.IsCustomProperty)
            {
                propertyAccessorName = string.Empty;
                return(string.Empty);
            }

            propertyAccessorName = $"m_{propertyName}";

            if (string.IsNullOrEmpty(propertyType.PropertyBackingAccessor))
            {
                var modifiers = AccessModifiers.Private;

                // @TODO
                //    | (PropertyTypeNode.IsCompositeType(propertyType.Tag) ? AccessModifiers.Readonly : AccessModifiers.None);

                return(GenerateBackingField(
                           TypeDeclarationStringForProperty(propertyType),
                           propertyName,
                           modifiers
                           ));
            }

            // TODO check & get by id instead of by name
            var propertyAccessorDelegate = propertyType.PropertyBackingAccessor;

            propertyAccessorName = $"{propertyAccessorDelegate}";

            return(string.Empty);
        }
Ejemplo n.º 5
0
        private static string GetPropertyWrapperTypeFor(
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType)
        {
            // @TODO warning we rely on type definitions here

            var propertyWrapperTypeStringPrefix = "";

            if (containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct))
            {
                propertyWrapperTypeStringPrefix = "Struct";
            }

            switch (propertyType.Tag)
            {
            case PropertyTypeNode.TypeTag.Struct:
                return($"{propertyWrapperTypeStringPrefix}MutableContainerProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.Class:
                return($"{propertyWrapperTypeStringPrefix}ContainerProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.Enum:
                return($"{propertyWrapperTypeStringPrefix}EnumProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.List:
            {
                var t = TypeStringFromEnumerableProperty(propertyType);

                switch (propertyType.Of.Tag)
                {
                case PropertyTypeNode.TypeTag.Primitive:
                    return($"{propertyWrapperTypeStringPrefix}ListProperty<{containerName}, {t}, {propertyType.Of.TypeName}>");

                case PropertyTypeNode.TypeTag.Struct:
                    return($"{propertyWrapperTypeStringPrefix}MutableContainerListProperty<{containerName}, {t}, {propertyType.Of.TypeName}>");

                case PropertyTypeNode.TypeTag.Class:
                    return($"{propertyWrapperTypeStringPrefix}ContainerListProperty<{containerName}, {t}, {propertyType.Of.TypeName}>");

                case PropertyTypeNode.TypeTag.Enum:
                    return($"{propertyWrapperTypeStringPrefix}EnumListProperty<{containerName}, {t}, {propertyType.Of.TypeName}>");

                case PropertyTypeNode.TypeTag.Unknown:
                case PropertyTypeNode.TypeTag.List:
                default:
                    throw new Exception($"Invalid property tag of list property name {propertyType.PropertyName}");
                }
            }

            case PropertyTypeNode.TypeTag.Primitive:
                return($"{propertyWrapperTypeStringPrefix}Property<{containerName}, {propertyType.TypeName}>");

            default:
                break;
            }

            return($"{propertyWrapperTypeStringPrefix}Property<{containerName}, {propertyType.TypeName}>");
        }
Ejemplo n.º 6
0
        private void GenerateStaticConstructorFor(
            PropertyTypeNode c,
            StringBuffer gen)
        {
            var containerTypeName = c.TypeName;

            gen.Append(' ', Style.Space * 1);
            gen.Append($"static {containerTypeName} (){Environment.NewLine}");
            gen.Append(' ', Style.Space * 1);
            gen.Append("{");
            gen.Append(Environment.NewLine);

            // Handle property initializers generation stage

            StaticConstructorStagePrePostFragments stageFragments;

            if (StaticConstructorInitializerFragments.TryGetValue(ConstructorStage.PropertyInitializationStage, out stageFragments))
            {
                var fragments = stageFragments.InStageFragments;

                if (fragments.Count != 0)
                {
                    gen.Append(' ', Style.Space * 2);
                    gen.Append("// Property wrappers initializers");
                    gen.Append(Environment.NewLine);

                    GenerateFragments(fragments, gen);
                }

                // Handle property initializers post generation stage (user hooks etc.)

                fragments = stageFragments.PostStageFragments;

                if (fragments.Count != 0)
                {
                    gen.Append(Environment.NewLine);
                    gen.Append(' ', Style.Space * 2);
                    gen.Append("// User Hooks");
                    gen.Append(Environment.NewLine);

                    GenerateFragments(fragments, gen);
                }
            }

            gen.Append(Environment.NewLine);
            gen.Append(' ', Style.Space * 2);
            gen.Append("// Freeze property bag items");

            foreach (var item in PropertyBagItemNames)
            {
                gen.Append(Environment.NewLine);
                gen.Append(' ', Style.Space * 2);
                gen.Append($"// {item}.Freeze();");
            }
            gen.Append(Environment.NewLine);

            gen.Append(' ', Style.Space * 1);
            gen.Append("}"); gen.Append(Environment.NewLine);
        }
Ejemplo n.º 7
0
 private static string TypeDeclarationStringForProperty(PropertyTypeNode propertyType)
 {
     if (PropertyTypeNode.IsEnumerableType(propertyType.Tag))
     {
         return(TypeStringFromEnumerableProperty(propertyType));
     }
     return(propertyType.TypeName);
 }
Ejemplo n.º 8
0
        private static string GeneratePropertyWrapperInitializers(
            PropertyTypeNode propertyType,
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            string propertyWrapperTypeString,
            string propertyAccessorName,
            string propertyTypeString,
            Scope code
            )
        {
            var initializerParams = new List <string>();

            var accessors = GetPropertyWrapperAccessors(
                propertyType,
                containerName,
                containerTypeTag,
                propertyWrapperTypeString,
                propertyTypeString,
                propertyAccessorName);

            // @TODO shouldn't be done here, and be located in a cleaner place

#if ENABLE_CUSTOM_PROPERTY_PARTIALS
            if (propertyType.IsCustomProperty)
            {
                var containerAsAParemeterType =
                    containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct) ? $"ref {containerName}" : $"{containerName}";

                code.AddLine("");

                if (!string.IsNullOrEmpty(accessors.RefGetter))
                {
                    code.AddLine(
                        $"partial void {accessors.RefGetter}({containerAsAParemeterType} value, IPropertyVisitor visitor);");
                }

                code.AddLine($"partial void {accessors.ValueSetter}({containerAsAParemeterType} container, {propertyTypeString} value);");

                code.AddLine(
                    $"partial {propertyTypeString} {accessors.ValueGetter}({containerAsAParemeterType} container);");

                code.AddLine("");
            }
#endif

            initializerParams.Add($"nameof({propertyType.PropertyName})");
            initializerParams.Add(accessors.ValueGetter);
            initializerParams.Add(accessors.ValueSetter);

            if (!string.IsNullOrEmpty(accessors.RefGetter))
            {
                initializerParams.Add(accessors.RefGetter);
            }

            return($@"new { propertyWrapperTypeString }( {string.Join(", ", initializerParams)} )");
        }
        private static void ParsePropertyContainer(
            PropertyTypeNode s,
            Dictionary <string, PropertyTypeNode> symbolsTable)
        {
            if (s == null || s.RawNode == null)
            {
                return;
            }

            var d = s.RawNode;

            var containerName = s.Name;

            if (d.ContainsKey(SerializedKeys.ConstructedFromeKey))
            {
                var constructorParams = d[SerializedKeys.ConstructedFromeKey] as IEnumerable;

                var paramTypes = new List <KeyValuePair <string, string> >();

                if (constructorParams != null)
                {
                    paramTypes.AddRange(from object p in constructorParams
                                        select p as IDictionary <string, object>
                                        into dp
                                        let paramType = dp.ContainsKey(SerializedKeys.PropertyTypeKey)
                            ? (dp[SerializedKeys.PropertyTypeKey] as string)
                            : ""
                                                        let paramName = dp.ContainsKey(SerializedKeys.PropertyNameKey)
                            ? (dp[SerializedKeys.PropertyNameKey] as string)
                            : ""
                                                                        where !string.IsNullOrEmpty(paramName) && !string.IsNullOrEmpty(paramType)
                                                                        select new KeyValuePair <string, string>(paramType, paramName));
                }

                s.Constructor.ParameterTypes = paramTypes;
            }

            if (d.ContainsKey(SerializedKeys.PropertiesListKey))
            {
                // Empty props if not
                var properties = d[SerializedKeys.PropertiesListKey] as IDictionary <string, object>;
                if (properties == null)
                {
                    return;
                }

                foreach (var k in properties.Keys)
                {
                    var propertyName = k;

                    var property = properties[propertyName] as IDictionary <string, object>;

                    s.Children.Add(ParseProperty(propertyName, property, symbolsTable));
                }
            }
        }
Ejemplo n.º 10
0
        private static string TypeDeclarationStringForProperty(PropertyTypeNode propertyType)
        {
            if (PropertyTypeNode.IsEnumerableType(propertyType.Tag))
            {
                if (propertyType.Tag == PropertyTypeNode.TypeTag.Array)
                {
                    return($"{propertyType.Of.TypeName}[]");
                }
                // TODO value type etc.
                return(TypeStringFromEnumerableProperty(propertyType));
            }

            return(propertyType.TypeName);
        }
        private static Dictionary <string, object> SerializeTypeTreeToJson(PropertyTypeNode node)
        {
            var o = new List <Dictionary <string, object> >();

            foreach (var child in node.NestedContainers)
            {
                o.Add(SerializeTypeTreeToJson(child));
            }

            var serializedContainer = SerializePropertyContainerToJson(node);

            serializedContainer[JsonSchema.Keys.NestedTypesKey] = o;

            return(serializedContainer);
        }
Ejemplo n.º 12
0
        public void GeneratePropertyContainer(
            PropertyTypeNode container,
            Func <string, CSharpGenerationCache.CodeInfo> dependancyLookupFunc = null)
        {
            ResetInternalGenerationStates();

            StringBuffer gen = new StringBuffer();

            using (new NamespaceDecorator(container, DoGenerateNamespace ? gen : null))
            {
                GeneratePropertyContainerFor(
                    container,
                    dependancyLookupFunc,
                    gen);
            }

            Code = gen;
        }
        private List <PropertyTypeNode> TypeDefinitionsToPropertyTypes(Dictionary <string, TypeDefinition> definitions)
        {
            var roots = new List <PropertyTypeNode>();

            foreach (var kv in _typesBeingSerialized)
            {
                var typeName = kv.Key;

                PropertyTypeNode node = null;
                if (GetOrCreate(typeName, out node))
                {
                    Assert.IsNotNull(node);
                    roots.Add(node);
                }
            }

            return(roots);
        }
Ejemplo n.º 14
0
            public PropertyContainerDataTypeDecorator(
                PropertyTypeNode containerNode,
                StringBuffer sb,
                List <string> baseClasses = null)
            {
                _sb            = sb;
                _containerNode = containerNode;

                if (baseClasses != null)
                {
                    foreach (var bc in baseClasses)
                    {
                        WithBaseClass(bc);
                    }
                }

                GenerateHeader();
            }
Ejemplo n.º 15
0
        private static PropertyWrapperAccessors GetPropertyWrapperAccessors(
            PropertyTypeNode propertyType,
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            string propertyWrapperTypeString,
            string propertyTypeString,
            string propertyAccessorName)
        {
            if (propertyType.IsCustomProperty)
            {
                return(new PropertyWrapperAccessors()
                {
                    ValueGetter = $"GetValue_{CleanupNameForMethod(propertyType.PropertyName)}",
                    ValueSetter = $"SetValue_{CleanupNameForMethod(propertyType.PropertyName)}",
                    RefGetter = propertyType.Tag == PropertyTypeNode.TypeTag.Struct
                        ? $"GetRef_{CleanupNameForMethod(propertyType.PropertyName)}"
                        : string.Empty
                });
            }

            var containerAsAParemeterType =
                containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct) ? $"ref {containerName}" : $"{containerName}";

            var propertySetter = "/* SET */ null";

            if (!PropertyTypeNode.IsCompositeType(propertyType.Tag) && !propertyType.IsReadonly)
            {
                propertySetter = $"/* SET */ ({containerAsAParemeterType} container, {propertyTypeString} value) => container.{propertyAccessorName} = value";
            }

            var propertyRefGetter = string.Empty;

            if (propertyType.Tag == PropertyTypeNode.TypeTag.Struct)
            {
                propertyRefGetter = $"/* REF */ ({containerAsAParemeterType} container, {propertyWrapperTypeString}.RefVisitMethod a, IPropertyVisitor v) => a(ref container.m_{propertyType.PropertyName}, v)";
            }

            return(new PropertyWrapperAccessors()
            {
                ValueGetter = $"/* GET */ ({containerAsAParemeterType} container) => container.{propertyAccessorName}",
                ValueSetter = propertySetter,
                RefGetter = propertyRefGetter
            });
        }
        SerializePropertyFieldsForType(PropertyTypeNode type)
        {
            var properties = new List <object>();

            foreach (var property in type.Properties)
            {
                var propertyFielsMap =
                    new Dictionary <string, object>
                {
                    [JsonSchema.Keys.PropertyNameKey] = property.PropertyName,
                    [JsonSchema.Keys.PropertyTypeKey] = property.TypeName
                };

                if (property.Of != null)
                {
                    propertyFielsMap[JsonSchema.Keys.PropertyItemTypeKey] = property.Of != null ? property.Of.TypeName : string.Empty;
                }

                if (property.IsReadonly != PropertyTypeNode.Defaults.IsReadonly)
                {
                    propertyFielsMap[JsonSchema.Keys.IsReadonlyPropertyKey] = property.IsReadonly;
                }

                if (property.IsCustomProperty != PropertyTypeNode.Defaults.IsCustomProperty)
                {
                    propertyFielsMap[JsonSchema.Keys.IsCustomPropertyKey] = property.IsCustomProperty;
                }

                if (property.IsPublicProperty != PropertyTypeNode.Defaults.IsPublicProperty)
                {
                    propertyFielsMap[JsonSchema.Keys.PropertyIsPublicKey] = property.IsPublicProperty;
                }

                if (property.DontInitializeBackingField != PropertyTypeNode.Defaults.DontInitializeBackingField)
                {
                    propertyFielsMap[JsonSchema.Keys.DontInitializeBackingFieldKey] = property.DontInitializeBackingField;
                }

                properties.Add(propertyFielsMap);
            }
            return(properties);
        }
Ejemplo n.º 17
0
        private static string GeneratePropertyWrapperBackingVariable(
            string propertyWrapperTypeName,
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType)
        {
            var accessModifiers = AccessModifiers.Static | AccessModifiers.Readonly;

            if (containerTypeTag == PropertyTypeNode.TypeTag.Class)
            {
                accessModifiers |= (propertyType.IsPublicProperty ? AccessModifiers.Public : AccessModifiers.Protected);
            }
            else
            {
                accessModifiers |= AccessModifiers.Private;
            }
            var modifiers = string.Join(" ", ModifiersToStrings(accessModifiers));

            var propertyWrapperVariableName = GetPropertyWrapperVariableName(propertyType);

            return($@"{modifiers} {propertyWrapperTypeName} {propertyWrapperVariableName};");
        }
Ejemplo n.º 18
0
        private static string GeneratePropertyWrapperInitializer(
            string propertyName,
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            string propertyWrapperTypeString,
            bool isReadonlyProperty,
            string propertyAccessorName,
            string propertyTypeString,
            PropertyTypeNode.TypeTag propertyTypeTag
            )
        {
            List <string> initializerParams = new List <string>();

            var containerAsAParemeterType =
                containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct) ? $"ref {containerName}" : $"{containerName}";

            initializerParams.Add($"nameof({propertyName})");

            initializerParams.Add($"/* GET */ ({containerAsAParemeterType} container) => container.{propertyAccessorName}");

            var propertySetter = "/* SET */ null";

            if (!PropertyTypeNode.IsCompositeType(propertyTypeTag) && !isReadonlyProperty)
            {
                propertySetter = $"/* SET */ ({containerAsAParemeterType} container, {propertyTypeString} value) => container.{propertyAccessorName} = value";
            }
            initializerParams.Add(propertySetter);

            if (propertyTypeTag == PropertyTypeNode.TypeTag.Struct)
            {
                initializerParams.Add(
                    $"/* REF */ ({containerAsAParemeterType} container, {propertyWrapperTypeString}.RefVisitMethod a, IPropertyVisitor v) => a(ref container.m_{propertyName}, v)"
                    );
            }

            string parameters = string.Join(", ", initializerParams);

            return($@"new { propertyWrapperTypeString }( {parameters} )");
        }
        private static List <string> DependantAssemblyNamesFor(PropertyTypeNode typeNode)
        {
            var assemblyNames = new List <string>();

            if (typeNode == null)
            {
                return(assemblyNames);
            }
            if (typeNode.NativeType != null)
            {
                assemblyNames.Add(typeNode.NativeType.Assembly.GetName().Name);
            }
            foreach (var t in typeNode.NestedContainers)
            {
                assemblyNames.AddRange(DependantAssemblyNamesFor(t));
            }
            foreach (var p in typeNode.Properties)
            {
                assemblyNames.AddRange(DependantAssemblyNamesFor(p));
            }
            return(assemblyNames);
        }
Ejemplo n.º 20
0
        private static void GenerateClassPropertiesForPropertyAccessor(
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType,
            Scope code
            )
        {
            var containerAsAParamTokens = new List <string> {
            };

            if (containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct))
            {
                containerAsAParamTokens.Add("ref");
            }
            containerAsAParamTokens.Add("this");

            var getSetValueCallContainerParam = string.Join(" ", containerAsAParamTokens);

            var propertyWrapperVariableName = GetPropertyWrapperVariableName(propertyType);

            var propertyTypeString = TypeDeclarationStringForProperty(propertyType);

            var isCompositeType = PropertyTypeNode.IsCompositeType(propertyType.Tag);

            var modifiers = string.Join(" ", ModifiersToStrings(AccessModifiers.Public));

            code.AddLine($"{modifiers} {propertyTypeString} {propertyType.PropertyName}");

            {
                code.AddLine("{");
                var accessorScope = new Scope(code);
                accessorScope.AddLine($"get {{ return {propertyWrapperVariableName}.GetValue({getSetValueCallContainerParam}); }}");
                if (!isCompositeType)
                {
                    accessorScope.AddLine($"set {{ {propertyWrapperVariableName}.SetValue({getSetValueCallContainerParam}, value); }}");
                }
                code.AddLine("}");
            }
        }
Ejemplo n.º 21
0
        private void GenerateUserHooksFor(PropertyTypeNode c, Scope scope)
        {
            if (c.UserHooks.HasFlag(UserHookFlags.OnPropertyBagConstructed))
            {
                GenerateUserHook(
                    scope.Code,
                    OnPropertyBagConstructedMethodName,
                    new List <string>()
                {
                    "IPropertyBag bag"
                });

                // Add a post property construction stage for that hook call

                AddStaticConstructorPostStageFragment(
                    ConstructorStage.PropertyInitializationStage,
                    new CSharpGenerationFragmentContext()
                {
                    Scope    = scope,
                    Fragment = $"{OnPropertyBagConstructedMethodName}({PropertyBagStaticVarName});"
                });
            }
        }
        SerializePropertyContainerToJson(PropertyTypeNode type)
        {
            var serializedContainer = new Dictionary <string, object>
            {
                [JsonSchema.Keys.ContainerNameKey]     = type.TypeName,
                [JsonSchema.Keys.ContainerIsStructKey] = type.Tag == PropertyTypeNode.TypeTag.Struct,
                [JsonSchema.Keys.PropertiesListKey]    = SerializePropertyFieldsForType(type)
            };

            if (!string.IsNullOrEmpty(type.TypePath.Namespace))
            {
                serializedContainer[JsonSchema.Keys.NamespaceKey] = type.TypePath.Namespace;
            }

            if (type.IsAbstractClass != PropertyTypeNode.Defaults.IsAbstractClass)
            {
                serializedContainer[JsonSchema.Keys.IsAbstractClassKey] = type.IsAbstractClass;
            }

            if (type.NoDefaultImplementation != PropertyTypeNode.Defaults.NoDefaultImplementation)
            {
                serializedContainer[JsonSchema.Keys.NoDefaultImplementationKey] = type.NoDefaultImplementation;
            }

            if (!string.IsNullOrEmpty(type.OverrideDefaultBaseClass))
            {
                serializedContainer[JsonSchema.Keys.OverrideDefaultBaseClassKey] = type.OverrideDefaultBaseClass;
            }

            if (type.UserHooks.HasFlag(UserHookFlags.OnPropertyBagConstructed))
            {
                serializedContainer[JsonSchema.Keys.GeneratedUserHooksKey] =
                    UserHookFlags.OnPropertyBagConstructed.ToString();
            }

            return(serializedContainer);
        }
Ejemplo n.º 23
0
        private static string GetPropertyWrapperTypeFor(
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType)
        {
            // TODO warning we rely on type definitions here
            var propertyWrapperTypeStringPrefix = "";

            if (containerTypeTag.HasFlag(PropertyTypeNode.TypeTag.Struct))
            {
                propertyWrapperTypeStringPrefix = "Struct";
            }

            switch (propertyType.Tag)
            {
            case PropertyTypeNode.TypeTag.Struct:
                return($"{propertyWrapperTypeStringPrefix}MutableContainerProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.Class:
                return($"{propertyWrapperTypeStringPrefix}ContainerProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.Enum:
                return($"{propertyWrapperTypeStringPrefix}EnumProperty<{containerName}, {propertyType.TypeName}>");

            case PropertyTypeNode.TypeTag.Array:
            case PropertyTypeNode.TypeTag.List:
            {
                var t = TypeStringFromEnumerableProperty(propertyType);
                return($"{propertyWrapperTypeStringPrefix}ListProperty<{containerName}, {t}, {propertyType.Of.TypeName}>");
            }

            default:
                break;
            }

            return($"{propertyWrapperTypeStringPrefix}Property<{containerName}, {propertyType.TypeName}>");
        }
Ejemplo n.º 24
0
        private static string GetPropertyWrapperVariableName(PropertyTypeNode propertyType)
        {
            string prefix = propertyType.IsPublicProperty ? string.Empty : "s_";

            return($"{prefix}{propertyType.PropertyName}Property");
        }
Ejemplo n.º 25
0
        private void GeneratePropertyContainerFor(
            PropertyTypeNode c,
            Func <string, CSharpGenerationCache.CodeInfo> dependancyLookupFunc,
            StringBuffer gen)
        {
            var containerName = c.TypeName;

            var containerTypeTag = c.Tag;

            var baseClass = string.IsNullOrEmpty(c.OverrideDefaultBaseClass)
                ? "IPropertyContainer" : c.OverrideDefaultBaseClass;

            var rootScope = new Scope();

            var shouldGeneratePRopertyContainerImplementation = !c.NoDefaultImplementation;

            using (var d = new PropertyContainerDataTypeDecorator(c, rootScope.Code, new List <string> {
                baseClass
            }))
                using (var scope = new Scope(rootScope))
                {
                    if (shouldGeneratePRopertyContainerImplementation)
                    {
                        if (c.Properties.Count != 0)
                        {
                            foreach (var propertyType in c.Properties)
                            {
                                GenerateProperty(
                                    containerName,
                                    containerTypeTag,
                                    propertyType,
                                    scope
                                    );
                                scope.AddLine("");
                            }

                            scope.AddLine("");
                        }

                        GenerateUserHooksFor(c, scope);

                        // Add inherited properties if it applies

                        var propertyBagElementNames = PropertyBagItemNames;
                        if (!string.IsNullOrEmpty(c.OverrideDefaultBaseClass) && dependancyLookupFunc != null)
                        {
                            var cachedContainer = dependancyLookupFunc(c.OverrideDefaultBaseClass);
                            if (cachedContainer != null)
                            {
                                propertyBagElementNames = PropertyBagItemNames.Select(n => n).ToList();

                                propertyBagElementNames.AddRange(cachedContainer.GeneratedPropertyFieldNames);
                            }
                        }

                        GeneratePropertyBag(
                            c,
                            propertyBagElementNames,
                            scope);

                        GenerateConstructorFor(c, scope.Code);

                        GenerateStaticConstructorFor(c, scope.Code);

                        // @TODO Cleanup
                        // Recurse to collect nested container definitions

                        foreach (var nestedContainer in c.NestedContainers)
                        {
                            if (nestedContainer == null)
                            {
                                continue;
                            }

                            var g = new CSharpContainerGenerator()
                            {
                                DoGenerateNamespace = false
                            };
                            g.GeneratePropertyContainer(nestedContainer, dependancyLookupFunc);

                            if (!string.IsNullOrEmpty(g.Code.ToString()))
                            {
                                scope.AddLine(string.Empty);
                                scope.AddLine(string.Empty);

                                scope.AddLine(g.Code.ToString());

                                scope.AddLine(string.Empty);
                                scope.AddLine(string.Empty);
                            }
                        }

                        scope.AddLine("public IVersionStorage VersionStorage => DefaultVersionStorage.Instance;");
                        scope.AddLine(Environment.NewLine);
                    }
                }

            gen.Append(rootScope.Code);
        }
Ejemplo n.º 26
0
 public override void OnPropertyContainerGenerationCompleted(PropertyTypeNode c)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 27
0
 private static string TypeStringFromEnumerableProperty(PropertyTypeNode propertyType)
 {
     return($"{propertyType.TypeName}<{propertyType.Of.TypeName}>");
 }
Ejemplo n.º 28
0
        private void GenerateConstructorFor(
            PropertyTypeNode c,
            StringBuffer gen)
        {
            var constructor = c.Constructor;

            if (constructor == null)
            {
                return;
            }

            if (c.Tag == PropertyTypeNode.TypeTag.Struct && constructor.ParameterTypes.Count == 0)
            {
                // baild out for struct + no param constructor
                return;
            }

            if (c.Tag == PropertyTypeNode.TypeTag.Class && c.Properties.Count == 0)
            {
                // baild out for class + no properties
                return;
            }

            var containerTypeName = c.TypeName;

            int i  = 0;
            var i1 = i;

            gen.Append(' ', Style.Space * 1);
            gen.Append(
                $"public {containerTypeName} ({string.Join(",", constructor.ParameterTypes.Select(p => $"{p.Key} p{i1}"))}){Environment.NewLine}"
                );

            gen.Append(' ', Style.Space * 1);
            gen.Append("{");

            // Generate constructor param -> field init

            i = 0;
            foreach (var paramType in constructor.ParameterTypes)
            {
                gen.Append(' ', Style.Space * 2);
                gen.Append($"{paramType.Value} = p{i};\n");
            }

            // Generate property initializers

            if (ConstructorInitializerFragments.Count != 0)
            {
                gen.Append(Environment.NewLine);
                gen.Append(' ', Style.Space * 2);
                gen.Append("// Property backing field initializers");
                gen.Append(Environment.NewLine);

                foreach (var fragment in ConstructorInitializerFragments)
                {
                    var codeFragment = fragment as CSharpGenerationFragmentContext;
                    if (codeFragment != null)
                    {
                        gen.Append(' ', Style.Space * 2);
                        gen.Append(codeFragment.Fragment);
                        gen.Append(Environment.NewLine);
                    }
                }
            }

            gen.Append(' ', Style.Space * 1);
            gen.Append("}"); gen.Append(Environment.NewLine);
        }
Ejemplo n.º 29
0
        public void GenerateProperty(
            string containerName,
            PropertyTypeNode.TypeTag containerTypeTag,
            PropertyTypeNode propertyType,
            Scope code)
        {
            // Public C# Property & backing field

            GenerateClassPropertiesForPropertyAccessor(containerTypeTag, propertyType, code);

            var propertyWrapperTypeName = GetPropertyWrapperTypeFor(
                containerName,
                containerTypeTag,
                propertyType);

            code.AddLine(GeneratePropertyWrapperBackingVariable(
                             propertyWrapperTypeName, containerTypeTag, propertyType));

            // Backing field if any

            var propertyAccessorName = string.Empty;
            var backingField         = GeneratePropertyBackingField(
                propertyType.PropertyName,
                propertyType,
                out propertyAccessorName
                );

            if (!string.IsNullOrEmpty(backingField))
            {
                // If we have a class we delegate init to constructor otherwise we default construct here

                var initializer = GenerateInitializerFromProperty(containerTypeTag, propertyType);

                var initializerFragment = backingField;

                if (!string.IsNullOrEmpty(initializer) && !propertyType.DontInitializeBackingField)
                {
                    initializerFragment += $" = {initializer}";
                }

                initializerFragment += ";";

                code.AddLine(initializerFragment);
            }

            //      -> Add constructor initializer fragments for later stage for that property

            var propertyTypeString          = TypeDeclarationStringForProperty(propertyType);
            var propertyWrapperVariableName = GetPropertyWrapperVariableName(propertyType);

            {
                var initializer = GeneratePropertyWrapperInitializers(
                    propertyType,
                    containerName,
                    containerTypeTag,
                    propertyWrapperTypeName,
                    propertyAccessorName,
                    propertyTypeString,
                    code);

                AddStaticConstructorInStageFragment(
                    ConstructorStage.PropertyInitializationStage,
                    new CSharpGenerationFragmentContext()
                {
                    Scope    = code,
                    Fragment = $"{propertyWrapperVariableName} = {initializer};"
                });
            }

            PropertyBagItemNames.Add(propertyWrapperVariableName);
        }
Ejemplo n.º 30
0
 public override void OnGenerateNestedContainer(PropertyTypeNode container, PropertyTypeNode nestedContainer)
 {
     throw new NotImplementedException();
 }