public static CodeTypeContainer CreateInterface(string name)
        {
            var interfaceName = "I" + name;
            var container     = Create(interfaceName);

            container.This.IsInterface = true;
            container.CustomAttributes.Add(Declare.Attribute("Interface", interfaceName));
            container.RequiredImports.Add("DBus");

            return(container);
        }
예제 #2
0
        private CodeAttributeDeclaration ReadAnnotation(Annotation annotation, CodeNamespace nameSpace)
        {
            var declaration = WellKnownAnnotations[annotation.Name];

            if (declaration != null)
            {
                return(declaration);
            }

            var memberName = annotation.Name;

            var fqt           = new FullyQualifiedType(memberName);
            var attributeName = fqt.TypeName + "Attribute";

            var attributeNamespace =
                compileUnit.Namespaces()
                .FirstOrDefault(n => n.Name == fqt.Namespace.Name);

            if (attributeNamespace == null)
            {
                attributeNamespace = fqt.Namespace ?? nameSpace;
                AddNamespace(attributeNamespace);
            }
            ImportNamespace(attributeNamespace.Name, nameSpace);

            // Declare the Attribute only if it doesn't exist already.
            var exists =
                attributeNamespace.Types()
                .Any(a => a.Name == attributeName);

            if (!exists)
            {
                var typeDeclaration = CodeTypeContainer.CreateAttribute(attributeName);

                var valueProperty  = Declare.Property("Value", CodeTypeContainer.Create(typeof(string)).Reference);
                var valueParameter = valueProperty.AsParameter();

                var constructor = new CodeConstructor();
                constructor.Attributes = MemberAttributes.Public;
                constructor.Parameters.Add(valueParameter);
                constructor.Statements.Add(new CodeAssignStatement(valueProperty.Reference(), valueParameter.Reference()));

                typeDeclaration.Members.Add(constructor);
                typeDeclaration.Members.Add(valueProperty);

                attributeNamespace.Types.Add(typeDeclaration);
                ImportNamespaces(typeDeclaration.RequiredImports);
            }

            return(Declare.Attribute(fqt.TypeName, annotation.Value));
        }
예제 #3
0
        private void ProcessArgs(DBusItemCollection items, ICodeDelegate @delegate, CodeNamespace @namespace)
        {
            var hasReturnType = false;
            var p             = 0;

            foreach (var item in items)
            {
                switch (item.DBusItemType)
                {
                case DBusItemType.Arg:
                    var arg  = (Arg)item;
                    var type = GetDBusType(arg.Type, @namespace, @delegate);

                    var declaration = new CodeParameterDeclarationExpression();
                    declaration.Type = type.Reference;

                    if (arg.Parent is Signal)
                    {
                        // Even though signal args are always out, DBusSharp allows us to treat them as in
                        declaration.Direction = FieldDirection.In;
                    }
                    else
                    {
                        declaration.Direction = arg.Direction == Direction.In ?
                                                FieldDirection.In : FieldDirection.Out;
                    }

                    // Becomes the return type if we don't have one yet
                    if (declaration.Direction == FieldDirection.Out && !hasReturnType)
                    {
                        @delegate.ReturnType = declaration.Type;
                        hasReturnType        = true;

                        if (!string.IsNullOrWhiteSpace(arg.Name))
                        {
                            var argumentAttribute = Declare.Attribute("Argument", arg.Name);
                            ImportNamespace("DBus");
                            var method = (CodeMemberMethod)@delegate;
                            method.ReturnTypeCustomAttributes.Add(argumentAttribute);
                        }
                    }
                    else
                    {
                        var name = arg.Name;
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            name = "arg" + p++;
                        }

                        var parameters = @delegate.Parameters();
                        var n          = 2;
                        while (parameters.Any(c => name == c.Name))
                        {
                            name += n++;
                        }

                        declaration.Name = name;

                        @delegate.Parameters.Add(declaration);
                    }
                    break;

                case DBusItemType.Annotation:
                    var attribute = ReadAnnotation((Annotation)item, @namespace);
                    @delegate.CustomAttributes.Add(attribute);
                    break;

                default:
                    throw CreateInvalidDataException(item.DBusItemType, DBusItemType.Arg, DBusItemType.Annotation);
                }
            }
        }