// Introduced in DBusSharp v0.8 (API v2.0) public static CodeTypeContainer CreateSignature() { var container = CodeTypeContainer.Create("Signature"); container.RequiredImports.Add("DBus.Protocol"); return(container); }
public static CodeTypeContainer CreateAttribute(string name) { var container = CodeTypeContainer.Create(name); container.This.BaseTypes.Add(CodeTypeContainer.Create("Attribute").Reference); container.RequiredImports.Add("System"); return(container); }
public static CodeTypeContainer CreateIDictionary() { var container = CodeTypeContainer.Create("IDictionary"); container.IsIDictionary = true; container.RequiredImports.Add("System.Collections.Generic"); return(container); }
private CodeTypeContainer ReadSignal(Signal signal, CodeNamespace @namespace) { var typeDeclaration = CodeTypeContainer.CreateDelegate(signal.Name + "Handler"); ProcessArgs(signal.Children, typeDeclaration, @namespace); // Add the type declaration if it doesn't already exist if ([email protected](typeDeclaration)) { @namespace.Types.Add(typeDeclaration); // What should happen if there's a type name conflict? } return(typeDeclaration); }
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)); }
private void ReadInterface(Interface @interface) { var interfaceName = @interface.Name; var fqt = new FullyQualifiedType(interfaceName); var declaration = CodeTypeContainer.CreateInterface(fqt.TypeName); ImportNamespaces(declaration.RequiredImports); var @namespace = fqt.Namespace; @namespace.Types.Add(declaration); AddNamespace(@namespace); foreach (var item in @interface.Children) { switch (item.DBusItemType) { case DBusItemType.Signal: var eventHandler = ReadSignal((Signal)item, @namespace); declaration.AddEvent(item.Name, eventHandler.Reference); break; case DBusItemType.Method: var method = ReadMethod((Method)item, @namespace); declaration.Members.Add(method); break; case DBusItemType.Property: var property = ReadProperty((Property)item, @namespace); declaration.Members.Add(property); break; case DBusItemType.Annotation: var attribute = ReadAnnotation((Annotation)item, @namespace); declaration.CustomAttributes.Add(attribute); break; default: throw CreateInvalidDataException(@interface.DBusItemType, DBusItemType.Signal, DBusItemType.Method, DBusItemType.Property, DBusItemType.Annotation); } } }
private static Container GetContainerType(CodeTypeContainer container) { if (container == null) { return(Container.None); } if (container.IsArray) { return(Container.Array); } if (container.IsIDictionary) { return(Container.DictEntryArray); } if (container.IsStruct) { return(Container.Struct); } return(Container.None); }
private CodeTypeContainer GetBasicType(int type) { switch (type) { case 'y': return(CodeTypeContainer.Create(typeof(byte))); case 'b': return(CodeTypeContainer.Create(typeof(bool))); case 'n': return(CodeTypeContainer.Create(typeof(short))); case 'q': return(CodeTypeContainer.Create(typeof(ushort))); case 'i': return(CodeTypeContainer.Create(typeof(int))); case 'u': return(CodeTypeContainer.Create(typeof(uint))); case 'x': return(CodeTypeContainer.Create(typeof(long))); case 't': return(CodeTypeContainer.Create(typeof(ulong))); case 'f': Messenger.SendWarning(this, "Encountered single ('f') type. Please check that your version of DBusSharp supports it."); return(CodeTypeContainer.Create(typeof(float))); case 'd': return(CodeTypeContainer.Create(typeof(double))); case 's': return(CodeTypeContainer.Create(typeof(string))); case 'v': return(CodeTypeContainer.Create(typeof(object))); case 'o': return(CodeTypeContainer.Create(typeof(ObjectPath))); case 'g': return(CodeTypeContainer.CreateSignature()); default: return(CodeTypeContainer.Create(typeof(void))); } }
private CodeTypeContainer GetNextDBusType(StringReader sr, Stack <CodeTypeContainer> containers, CodeNamespace @namespace, ICodeDelegate @delegate) { CodeTypeContainer container; int code; switch (code = sr.Read()) { case '(': // STRUCT container = CodeTypeContainer.CreateStruct(); containers.Push(container); var i = 1; do { var type = GetNextDBusType(sr, containers, @namespace, @delegate); // Seeing ourselves signals that we are complete if (type == container) { break; } // Add each member as a property var property = Declare.Property("Item" + i++, type.Reference); container.Members.Add(property.BackingField); container.Members.Add(property); } while (true); // Empty structures are not allowed; there must be at least one type code between // the parentheses. [1] // [1] http://dbus.freedesktop.org/doc/dbus-specification.html#container-types if (container.Members.Count == 0) { throw new InvalidDataException("Empty structures are not allowed; " + "there must be at least one type code between the parentheses."); } var j = 2; var name = @delegate.Name + "Data"; while (@namespace.ContainsTypeName(name)) { name = @delegate.Name + "Data" + j++; } container.Name = name; @namespace.Types.Add(container); return(container); case 'a': // ARRAY container = CodeTypeContainer.CreateArray(); containers.Push(container); // The array type code must be followed by a single complete type. [1] var nextType = GetNextDBusType(sr, containers, @namespace, @delegate); if (GetContainerType(nextType) == Container.DictEntryArray && !nextType.IsComplete) { nextType.IsComplete = true; return(nextType); } containers.Pop(); container.SetArrayElementType(nextType); return(container); case '{': // DICT_ENTRY // Implementations must not accept dict entries outside of arrays... [1] if (GetContainerType(containers.Pop()) != Container.Array) { throw new InvalidDataException("DICT_ENTRY must occur in an array. e.g.: type=\"a{us}\""); } container = CodeTypeContainer.CreateIDictionary(); containers.Push(container); var key = GetNextDBusType(sr, containers, @namespace, @delegate); // ...the first single complete type (the "key") must be a basic type rather than // a container type. [1] if (GetContainerType(key) != Container.None && key.Name != typeof(object).ToString()) { throw new InvalidDataException("The key for DICT_ENTRY must be a basic type."); } var value = GetNextDBusType(sr, containers, @namespace, @delegate); container.TypeArguments.Add(key.Reference); container.TypeArguments.Add(value.Reference); containers.Pop(); ImportNamespaces(container.RequiredImports); return(container); case ')': // STRUCT closing delimiter return(containers.Pop()); case '}': // DICT_ENTRY closing delimiter return(containers.Pop()); case -1: // Stream end Messenger.SendWarning(this, "Reached end of stream."); return(containers.Pop()); default: // Basic types return(GetBasicType(code)); } }
public void SetArrayElementType(CodeTypeContainer container) { ArrayElementType = container; Reference.ArrayElementType = ArrayElementType.Reference; }