private static CodeMemberMethod CreateCreateMessageCodeMemberMethod(IEnumerable <Message> messages, string messagesNamespace, TypeInfo messageTypeEnumTypeInfo, TypeInfo messageInterfaceTypeInfo)
        {
            string mavTypeParamName = "mavType";

            CodeMemberMethod codeMemberMethod = new CodeMemberMethod();

            codeMemberMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static;
            codeMemberMethod.Name       = "CreateMessage";
            codeMemberMethod.ReturnType = new CodeTypeReference(messageInterfaceTypeInfo.Name);
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(messageTypeEnumTypeInfo.FullName, mavTypeParamName));

            foreach (Message message in messages)
            {
                string messageClassName     = NameHelper.GetMessageClassName(message);
                string messageClassFullname = NamespaceHelper.GetFullname(messagesNamespace, messageClassName);

                CodeBinaryOperatorExpression equalsExpression = new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression(mavTypeParamName),
                    CodeBinaryOperatorType.IdentityEquality,
                    new CodeTypeReferenceExpression(NamespaceHelper.GetFullname(messageTypeEnumTypeInfo.FullName, message.Name)));

                CodeConditionStatement conditionalStatement = new CodeConditionStatement(
                    equalsExpression,
                    new CodeMethodReturnStatement(new CodeObjectCreateExpression(messageClassFullname)));

                codeMemberMethod.Statements.Add(conditionalStatement);
            }

            CodeStatement returnNullCodeStatement = new CodeMethodReturnStatement(new CodeSnippetExpression("null"));

            codeMemberMethod.Statements.Add(returnNullCodeStatement);

            return(codeMemberMethod);
        }
        private static void AddReadPropertyStatements(CodeMemberMethod codeMemberMethod, Message message, string readerParamName, string messageVariableName, string ns)
        {
            IEnumerable <MessageField> orderedMessageFields = message.Fields.OrderForSerialization().ToList();

            foreach (MessageField messageField in orderedMessageFields)
            {
                CodePropertyReferenceExpression propertyExpression =
                    new CodePropertyReferenceExpression(
                        new CodeVariableReferenceExpression(messageVariableName),
                        messageField.Name);

                switch (messageField.Type.FieldType)
                {
                case FieldType.Array:
                    AddArrayReadCodeStatements(codeMemberMethod.Statements, readerParamName, messageField, propertyExpression);
                    break;

                case FieldType.Enum:
                    string enumFullname = NamespaceHelper.GetFullname(ns, messageField.Type.Enum.Name);
                    AddEnumReadCodeStatements(codeMemberMethod.Statements, readerParamName, messageField.Type, propertyExpression, enumFullname);
                    break;

                default:
                    AddPrimitiveReadCodeStatements(codeMemberMethod.Statements, readerParamName, messageField, propertyExpression);
                    break;
                }
            }
        }
예제 #3
0
        private static CodeMemberMethod CreateGetCrcExtraCodeMemberMethod(IEnumerable <Message> messages, TypeInfo messageTypeEnumTypeInfo)
        {
            string mavTypeParamName = "mavType";

            CodeMemberMethod codeMemberMethod = new CodeMemberMethod();

            codeMemberMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static;
            codeMemberMethod.Name       = "GetCrcExtra";
            codeMemberMethod.ReturnType = new CodeTypeReference(typeof(byte));
            codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression(messageTypeEnumTypeInfo.FullName, mavTypeParamName));

            foreach (Message message in messages)
            {
                CodeBinaryOperatorExpression equalsExpression = new CodeBinaryOperatorExpression(
                    new CodeVariableReferenceExpression(mavTypeParamName),
                    CodeBinaryOperatorType.IdentityEquality,
                    new CodeTypeReferenceExpression(NamespaceHelper.GetFullname(messageTypeEnumTypeInfo.FullName, message.Name)));

                CodeConditionStatement conditionalStatement = new CodeConditionStatement(
                    equalsExpression,
                    new CodeMethodReturnStatement(new CodePrimitiveExpression(message.CrcExtra)));

                codeMemberMethod.Statements.Add(conditionalStatement);
            }

            CodeStatement returnNullCodeStatement = new CodeMethodReturnStatement(new CodeSnippetExpression("Byte.MinValue"));

            codeMemberMethod.Statements.Add(returnNullCodeStatement);

            return(codeMemberMethod);
        }
예제 #4
0
        public static CodeCompileUnit CreateCodeCompileUnit(TypeInfo typeInfo, Message message, TypeInfo messageBaseClassTypeInfo, TypeInfo messageTypeEnumTypeInfo, IDictionary <MessageDefinitions.Data.Enum, TypeInfo> typeInfoByEnum)
        {
            // Generate the container unit
            CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

            CodeNamespace globalNamespace = new CodeNamespace(String.Empty);

            codeCompileUnit.Namespaces.Add(globalNamespace);

            // using
            globalNamespace.Imports.Add(new CodeNamespaceImport("System"));
            globalNamespace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
            globalNamespace.Imports.Add(new CodeNamespaceImport("MavLink4Net.Messages.Metadata"));

            // Generate the namespace
            CodeNamespace codeNamespace = new CodeNamespace(typeInfo.Namespace);

            codeCompileUnit.Namespaces.Add(codeNamespace);

            string messageTypeEnumValue = NamespaceHelper.GetFullname(messageTypeEnumTypeInfo.FullName, message.Name);

            // Declare the class
            CodeTypeDeclaration classDeclaration = ToCodeTypeDeclaration(typeInfo, message, messageBaseClassTypeInfo, messageTypeEnumValue, typeInfoByEnum);

            // Add class to the namespace
            codeNamespace.Types.Add(classDeclaration);

            return(codeCompileUnit);
        }