public static SerialBinaryCompound getSBCompound(this IBinarySerializable bs) { if (compounds.ContainsKey(bs.GetType())) { return(compounds[bs.GetType()].setReferenced(bs)); } return(SerialBinaryCompound.Empty); }
public static SerialBinaryCompound createSBCompound(this IBinarySerializable bs) { if (!compounds.ContainsKey(bs.GetType())) { SerialBinaryCompound c = new SerialBinaryCompound(bs); compounds.Add(bs.GetType(), c); return(c); } throw new Exception("Compound already created!"); }
public void Write(IBinarySerializable value) { if (value == null) { Write((ulong)0); } else { ulong token; if (writeCache.TryGetValue(value, out token)) { Write(token); } else { token = (ulong)writeCache.Count + 1; writeCache.Add(value, token); Write(token); Write(value.GetType()); using (var writer = new Serializer(this)) { value.Serialize(writer); } } } }
/// <summary> /// Deserialize into an existing IBinarySerializable object. /// </summary> /// <param name="buffer">The buffer containing the serialized data.</param> /// <param name="start">The start index in the buffer of the serialized object.</param> /// <param name="value">The deserialized object.</param> /// <returns>The number of deserialized bytes.</returns> public uint FromBytesOverwrite(Buffer buffer, uint start, IBinarySerializable value) { Type type; CheckDeserializationParameters(buffer, start); uint read = FromBytes(buffer, start, out type); if (type != null) { if (!type.IsAssignableFrom(value.GetType())) { throw new FormatException("The type of IBynarySerializable object does not match the provided object."); } read += value.FromBytes(this, buffer, start + read); } else { // Nothing to do: we got nothing to deserialize and the value already exists, so returning null is not an option. // It would be great to notify the user, but we do have a mecanism for that, and raising an exception would stop // the deserialization, but this should just be a warning. } return(read); }
/// <summary> /// Serialize a IBinarySerializable object. /// </summary> /// <param name="buffer">The buffer where to serialize the data.</param> /// <param name="start">The start index in the buffer where to serialize the data.</param> /// <param name="value">The object to serialize.</param> /// <returns>The number of serialized bytes.</returns> public uint ToBytes(ref Buffer buffer, uint start, IBinarySerializable value) { uint written; CheckSerializationParameters(buffer, start); if (value != null) { Type type = value.GetType(); CheckDefaultConstructor(type); written = ToBytes(ref buffer, start, type); written += value.ToBytes(this, ref buffer, start + written); } else { written = ToBytes(ref buffer, start, (Type)null); } return(written); }
public static bool hasSBCompound(this IBinarySerializable bs) { return(compounds.ContainsKey(bs.GetType())); }