Ejemplo n.º 1
0
 /**
  * Converts a field type into a C# field type name string.
  */
 public static string ToCSharpType(IFieldType fieldType)
 {
     if (fieldType is PrimitiveFieldType)
     {
         PrimitiveFieldType primitiveFieldType = (PrimitiveFieldType)fieldType;
         return(ToCSharpPrimitiveType(primitiveFieldType.PrimitiveType));
     }
     else if (fieldType is ReferenceFieldType)
     {
         ReferenceFieldType referenceFieldType = (ReferenceFieldType)fieldType;
         if (referenceFieldType.Referrable is PatternBufferEnum)
         {
             return(ToCSharpName(((ReferenceFieldType)fieldType).ReferrableName));
         }
         else
         {
             return(ToCSharpName(((ReferenceFieldType)fieldType).ReferrableName));
         }
     }
     else if (fieldType is ListFieldType)
     {
         return("List<" + ToCSharpType(((ListFieldType)fieldType).ElementType) + ">");
     }
     else if (fieldType is SetFieldType)
     {
         return("HashSet<" + ToCSharpType(((SetFieldType)fieldType).ElementType) + ">");
     }
     else if (fieldType is MapFieldType)
     {
         return("Dictionary<" + ToCSharpType(((MapFieldType)fieldType).KeyType) + "," + ToCSharpType(((MapFieldType)fieldType).ValueType) + ">");
     }
     return("WHATEVS");
 }
Ejemplo n.º 2
0
        /**
         * Appends C# code to serialize a C# list into bytes.
         */
        public static void AppendSerializerWriteCollection(ref string code, string fieldName, IFieldType elementType, string subfieldName, string schemaObjectTypeName)
        {
            string subfield = "";

            if (subfieldName != null)
            {
                subfield = "." + subfieldName;
            }

            // Deal with list lengths
            string targetList = "o." + fieldName + subfield;

            code += "            if (o." + fieldName + " == null || " + targetList + " == null || " + targetList + ".Count == 0) {\r\n";
            PBP.AppendWriteByte(ref code, "0");
            code += "            }\r\n";
            code += "            else { \r\n";
            PBP.AppendWriteVUInt16(ref code, targetList + ".Count");

            if (elementType is PrimitiveFieldType)
            {
                PrimitiveFieldType primitiveFieldType = (PrimitiveFieldType)elementType;
                code += "                foreach (" + PB.ToCSharpType(primitiveFieldType) + " primitive in " + targetList + ") {\r\n";
                PBP.AppendWritePrimitive(ref code, "primitive", primitiveFieldType.PrimitiveType);
                code += "                }\r\n";
            }
            else if (elementType is ReferenceFieldType)
            {
                ReferenceFieldType referenceFieldType = (ReferenceFieldType)elementType;
                // list<enum>
                if (referenceFieldType.Referrable is PatternBufferEnum)
                {
                    PatternBufferEnum patternBufferEnum = (PatternBufferEnum)referenceFieldType.Referrable;
                    code += "                foreach (" + PB.ToCSharpName(patternBufferEnum.Name) + " enumValue in " + targetList + ") {\r\n";
                    PBP.AppendWriteByte(ref code, "enumValueIndexMap[\"" + referenceFieldType.ReferrableName + "\"][enumValue.ToString()];\r\n");
                    code += "                }\r\n";
                }
                // list<something>
                else if (referenceFieldType.Referrable is PatternBufferType)
                {
                    PatternBufferType patternBufferType = (PatternBufferType)referenceFieldType.Referrable;
                    // All objects in the list are the same type; do not write type IDs for each item
                    if (patternBufferType.IsFinal)
                    {
                        code += "                foreach (" + PB.ToCSharpName(patternBufferType.Name) + " finalObject in " + targetList + ") {\r\n";
                        code += "                    Energize(finalObject, bytes, ref index, false);\r\n";
                        code += "                }\r\n";
                    }
                    // The objects in the list may be of different types; write the type ID for each item
                    else
                    {
                        code += "                foreach (" + schemaObjectTypeName + " abstractObject in " + targetList + ") {\r\n";
                        code += "                    Energize((" + schemaObjectTypeName + ")abstractObject, bytes, ref index, true);\r\n";
                        code += "                }\r\n";
                    }
                }
                else
                {
                    // what
                }
            }
            code += "            }\r\n";
        }
Ejemplo n.º 3
0
        /**
         * Appends C# code to read a collection of serialized objects into the appropriate collection type.
         */
        public static void AppendSerializerReadCollection(ref string code, string fieldName, IFieldType elementType, string collectionTypeName, string subfieldName)
        {
            // Read the list item count
            code += "            // Read list item count\r\n";
            string countFieldName = PB.CreateRandomFieldName("count");

            code += "            ushort " + countFieldName + ";\r\n";
            PBP.AppendReadVUInt16(ref code, countFieldName);

            // Continue if the bytes indicate more than zero items.
            code += "            // Read list items\r\n";
            code += "            if (" + countFieldName + " > 0) {\r\n";

            // The element size is constant if:
            // 1) It's a primitive field
            // 2) It's not a variant primitive
            // 3) It's not a string
            // Serialize constant-sized primitives
            if ((elementType is PrimitiveFieldType))
            {
                PrimitiveFieldType primitiveFieldType = (PrimitiveFieldType)elementType;
                string             cappedElementName  = PB.ToCapitalizedCSharpType(primitiveFieldType);
                code += "                " + fieldName + " = this.instantiator.Acquire" + collectionTypeName + "Of" + cappedElementName + "();\r\n";
                string tempFieldName = PB.CreateRandomFieldName("listValue");
                code += "                " + PB.ToCSharpType(primitiveFieldType) + " " + tempFieldName + ";\r\n";
                string i = PB.CreateRandomFieldName("i");
                code += "                for (int " + i + " = 0; " + i + " < " + countFieldName + "; " + i + "++) {\r\n";
                code += "                    // Read " + primitiveFieldType.PrimitiveType + " list item\r\n";
                PBP.AppendReadPrimitive(ref code, tempFieldName, primitiveFieldType.PrimitiveType);
                code += "                    " + fieldName + ".Add(" + tempFieldName + ");\r\n";
                code += "                }\r\n";
            }

            // Serialize variable size list elements (incl. references, enums, strings)
            else
            {
                ReferenceFieldType referenceFieldType = (ReferenceFieldType)elementType;
                // list<enum>
                if (referenceFieldType.Referrable is PatternBufferEnum)
                {
                    PatternBufferEnum e                 = (PatternBufferEnum)referenceFieldType.Referrable;
                    string            enumName          = PB.ToCSharpName(e.Name);
                    string            cappedElementName = PB.ToCapitalizedCSharpType(referenceFieldType);
                    code += "                " + fieldName + " = this.instantiator.Acquire" + collectionTypeName + "Of" + cappedElementName + "();\r\n";
                    string i = PB.CreateRandomFieldName("i");
                    code += "                for (int " + i + " = 0; " + i + " < " + countFieldName + "; " + i + "++) {\r\n";
                    code += "                    " + fieldName + ".Add((" + enumName + ")System.Enum.Parse(typeof(" + enumName + "), enumIndexValueMap[\"" + referenceFieldType.ReferrableName + "\"][bytes[index++]]));\r\n";
                    code += "                }\r\n";
                }
                // list<something>
                else if (referenceFieldType.Referrable is PatternBufferType)
                {
                    PatternBufferType patternBufferType = (PatternBufferType)referenceFieldType.Referrable;
                    string            elementTypeName   = PB.ToCSharpType(referenceFieldType);
                    string            cappedElementName = PB.ToCapitalizedCSharpType(referenceFieldType);
                    code += "                " + fieldName + " = this.instantiator.Acquire" + collectionTypeName + "Of" + cappedElementName + "();\r\n";
                    string i = PB.CreateRandomFieldName("li");
                    // All objects in the list are the same type
                    if (patternBufferType.IsFinal)
                    {
                        code += "                for (int " + i + " = 0; " + i + " < " + countFieldName + "; " + i + "++) {\r\n";
                        code += "                    " + fieldName + ".Add((" + elementTypeName + ")Energize(bytes, ref index, " + patternBufferType.TypeId + "));\r\n";
                        code += "                }\r\n";
                    }
                    // The objects in the list may be of different types
                    else
                    {
                        code += "                for (int " + i + " = 0; " + i + " < " + countFieldName + "; " + i + "++) {\r\n";
                        code += "                    " + fieldName + ".Add((" + elementTypeName + ")Energize(bytes, ref index));\r\n";
                        code += "                }\r\n";
                    }
                }
                else
                {
                    // what
                }
            }
            code += "            }\r\n";
        }
        private void CheckPrimitiveFieldsSerialization(PortableMarshaller marsh, PrimitiveFieldType obj)
        {
            byte[] bytes = marsh.Marshal(obj);

            IPortableObject portObj = marsh.Unmarshal<IPortableObject>(bytes, PortableMode.ForcePortable);

            Assert.AreEqual(obj.GetHashCode(), portObj.GetHashCode());

            PrimitiveFieldType newObj = portObj.Deserialize<PrimitiveFieldType>();

            Assert.AreEqual(obj, newObj);
        }
        private void CheckPrimitiveFields(PortableMarshaller marsh, PrimitiveFieldType obj)
        {
            obj.PBool = true;
            obj.PByte = 2;
            obj.PSbyte = 3;
            obj.PShort = 4;
            obj.PUshort = 5;
            obj.PInt = 6;
            obj.PUint = 7;
            obj.PLong = 8;
            obj.PUlong = 9;
            obj.PChar = 'a';
            obj.PFloat = 10;
            obj.PDouble = 11;
            obj.PString = "abc";
            obj.PGuid = Guid.NewGuid();
            obj.PnGuid = Guid.NewGuid();
            
            //CheckPrimitiveFieldsSerialization(marsh, obj);

            //obj.PString = "";

            //CheckPrimitiveFieldsSerialization(marsh, obj);

            //obj.PString = null;

            //CheckPrimitiveFieldsSerialization(marsh, obj);

            //obj.PString = null;
            //obj.PNGuid = null;

            CheckPrimitiveFieldsSerialization(marsh, obj);
        }
        public void TestPrimitiveFieldsRawSerializer()
        {
            ICollection<PortableTypeConfiguration> typeCfgs = 
                new List<PortableTypeConfiguration>();

            PortableTypeConfiguration typeCfg =
                new PortableTypeConfiguration(typeof(PrimitiveFieldType));

            typeCfg.Serializer = new PrimitiveFieldsRawSerializer();

            typeCfgs.Add(typeCfg);

            PortableConfiguration cfg = new PortableConfiguration();

            cfg.TypeConfigurations = typeCfgs;

            PortableMarshaller marsh = new PortableMarshaller(cfg);

            PrimitiveFieldType obj = new PrimitiveFieldType();

            CheckPrimitiveFields(marsh, obj);
        }
        public void TestPrimitiveFieldsSerializer()
        {
            var typeCfgs = new List<PortableTypeConfiguration>
            {
                new PortableTypeConfiguration(typeof (PrimitiveFieldType))
                {
                    Serializer = new PrimitiveFieldsSerializer()
                }
            };

            PortableConfiguration cfg = new PortableConfiguration {TypeConfigurations = typeCfgs};

            PortableMarshaller marsh = new PortableMarshaller(cfg);

            PrimitiveFieldType obj = new PrimitiveFieldType();

            CheckPrimitiveFields(marsh, obj);
        }
        public void TestPrimitiveFieldsReflective()
        {
            ICollection<PortableTypeConfiguration> typeCfgs = 
                new List<PortableTypeConfiguration>();

            typeCfgs.Add(new PortableTypeConfiguration(typeof(PrimitiveFieldType)));

            PortableConfiguration cfg = new PortableConfiguration {TypeConfigurations = typeCfgs};

            PortableMarshaller marsh = new PortableMarshaller(cfg);

            PrimitiveFieldType obj = new PrimitiveFieldType();

            CheckPrimitiveFields(marsh, obj);
        }