Esempio n. 1
0
            public CodeMemberMethod GenerateGetHashCodeMethod(Type t)
            {
                var m = new MethodHelper("_GetHashCode", typeof(int),
                                         MemberAttributes.Static | MemberAttributes.Final | MemberAttributes.Public);

                m.Param("value", t);
                m.Add(Decl("hash", typeof(int), Default(typeof(int))));

                if (IsPrimitive(t) || t.IsEnum)
                {
                    m.Assign("hash", Invoke(Var("value"), "GetHashCode"));
                    m.Return("hash");
                    return(m.Code);
                }

                if (HasCustomSerialization(t))
                {
                    m.Add(Stmt("throw new System.Exception(\"Not Implemented\");"));
                    //m.Return(Var("hash"));
                    return(m.Code);
                }

                foreach (FieldInfo field in FieldMetadata(t))
                {
                    CodeExpression fieldHash;
                    if (IsNaiadable(field.FieldType))
                    {
                        AddType(field.FieldType);
                        fieldHash = Invoke(CodeGenClass, "_GetHashCode",
                                           new CodeFieldReferenceExpression(Var("value"), field.Name));
                    }
                    else
                    {
                        var fieldRef = new CodeFieldReferenceExpression(Var("value"), field.Name);
                        fieldHash = Invoke("Naiad.HashCode", "SafeGetHashCode", fieldRef);
                    }

                    m.Assign("hash",
                             new CodeBinaryOperatorExpression(Var("hash"), CodeBinaryOperatorType.Add, fieldHash));
                }

                m.Return(Var("hash"));
                return(m.Code);
            }
Esempio n. 2
0
            public CodeMemberMethod GenerateNonStaticSerializeMethod(Type t)
            {
                var m = new MethodHelper("Serialize", typeof(bool), MemberAttributes.Public | MemberAttributes.Final);

                m.Param("destination", typeof(SubArray <byte>), FieldDirection.Ref);
                m.Param("value", t, FieldDirection.In);

                m.Add(LogIt(Expr("destination.Count + \" - " + t.Name + "\"")));
                m.Add(Decl("originalDestination", typeof(SubArray <byte>), Var("destination")));
                m.Add(Decl("success", typeof(bool),
                           Invoke(CodeGenClass, "_Serialize", Ref(Var("destination")), Var("value"))));
                m.Add(IfElse(Var("success"),
                             new CodeStatement[] { },
                             new[] { Assign("destination", Var("originalDestination")) }));


                m.Add(LogIt(Expr("\" After: \" + destination.Count + \" - " + FileNameForType(t) + "\"")));
                m.Return(Var("success"));
                return(m.Code);
            }
Esempio n. 3
0
            public CodeMemberMethod GenerateNonStaticTryDeserializeMethod(Type t)
            {
                var m = new MethodHelper("TryDeserialize", typeof(bool),
                                         MemberAttributes.Final | MemberAttributes.Public);

                m.Param("source", typeof(RecvBuffer), FieldDirection.Ref);
                m.Param("value", t, FieldDirection.Out);
                m.Add(LogIt(Expr("source.CurrentPos +" + "\" : " + t.Name + "\"")));

                m.Add(LogIt(Expr("\" Before: \" + source.CurrentPos + \" - " + FileNameForType(t) + "\"")));
                m.Add(Decl("originalSource", typeof(RecvBuffer), Var("source")));

                m.Add(Decl("success", typeof(bool),
                           Invoke(CodeGenClass, "_TryDeserialize",
                                  Ref(new CodeVariableReferenceExpression("source")),
                                  Out(new CodeVariableReferenceExpression("value")))));

                m.Add(IfElse(
                          Var("success"),
                          new CodeStatement[] { },
                          new[] { Assign("source", Var("originalSource")) }));


                m.Add(LogIt(Expr("\" After: \" + source.CurrentPos + \" - " + FileNameForType(t) + "\"")));
                m.Return(Var("success"));
                return(m.Code);
            }
Esempio n. 4
0
            public CodeMemberMethod GenerateCompareMethod(Type t)
            {
                var m = new MethodHelper("_Equals", typeof(bool),
                                         MemberAttributes.Static | MemberAttributes.Final);

                m.Param("a", t);
                m.Param("b", t);

                if (IsPrimitive(t) || t.IsEnum)
                {
                    m.Return(Invoke(Var("a"), "Equals", Var("b")));
                    return(m.Code);
                }

                if (HasCustomSerialization(t))
                {
                    m.Add(Stmt("throw new System.Exception(\"Not Implemented\");"));
                    m.Return("false");
                    return(m.Code);
                }

                var            toCompare = new List <CodeExpression>();
                CodeExpression trueExpr  = Literal(true);

                foreach (FieldInfo field in FieldMetadata(t))
                {
                    var aField = new CodeFieldReferenceExpression(Var("a"), field.Name);
                    var bField = new CodeFieldReferenceExpression(Var("b"), field.Name);

                    if (IsNaiadable(field.FieldType))
                    {
                        AddType(field.FieldType);
                        toCompare.Add(Invoke(CodeGenClass, "_Equals", aField, bField));
                    }
                    else
                    {
                        toCompare.Add(new CodeMethodInvokeExpression(aField, "Equals", bField));
                    }
                }

                m.Return(
                    toCompare.Aggregate(trueExpr,
                                        (a, b) =>
                                        new CodeBinaryOperatorExpression(a, CodeBinaryOperatorType.BooleanAnd, b)));

                return(m.Code);
            }
Esempio n. 5
0
            public CodeMemberMethod GenerateTryDeserializeMethod(Type t)
            {
                var m = new MethodHelper("_TryDeserialize", typeof(bool),
                                         MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static);

                m.Param("source", typeof(RecvBuffer), FieldDirection.Ref);
                m.Param("value", t, FieldDirection.Out);

                if (IsNullable(t))
                {
                    m.Add(Decl("isNull", typeof(bool), Literal(false)));
                }

                m.AddAll(DeserializeOrReturn(t, Var("source"), Var("value")));
                m.Return("true");
                return(m.Code);
            }