/// <summary> /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectIdentifier"/> class, reusing given context. /// </summary> /// <param name="context">Context to reuse.</param> public ObjectIdentifier(ObjectIdentifierContext context) { objectToId = new Dictionary<object, int>(); idToObject = context.GetObjects(); for(var i = 0; i < idToObject.Count; i++) { var objectToAdd = idToObject[i]; if(objectToAdd != null) { objectToId.Add(idToObject[i], i); } } }
/// <summary> /// Initializes a new instance of the <see cref="Antmicro.Migrant.ObjectIdentifier"/> class, reusing given context. /// </summary> /// <param name="context">Context to reuse.</param> public ObjectIdentifier(ObjectIdentifierContext context) { objectToId = new Dictionary <object, int>(); idToObject = context.GetObjects(); for (var i = 0; i < idToObject.Count; i++) { var objectToAdd = idToObject[i]; if (objectToAdd != null) { objectToId.Add(idToObject[i], i); } } }
private void PrepareForNextWrite() { objectsWrittenInline.Clear(); parentObjects.Clear(); if(referencePreservation == ReferencePreservation.UseWeakReference) { identifierContext = identifier.GetContext(); } if(referencePreservation != ReferencePreservation.Preserve) { identifier = null; } }
public void WriteObject(object o) { if(o == null || Helpers.IsTransient(o.GetType())) { throw new ArgumentException("Cannot write a null object or a transient object."); } if(referencePreservation != ReferencePreservation.Preserve) { identifier = identifierContext == null ? new ObjectIdentifier() : new ObjectIdentifier(identifierContext); identifierContext = null; } try { var writtenBefore = identifier.Count; writeMethods.writeReferenceMethodsProvider.GetOrCreate(typeof(object))(this, o); if(writtenBefore != identifier.Count) { var refId = identifier.GetId(o); do { if(!objectsWrittenInline.Contains(refId)) { var obj = identifier.GetObject(refId); writer.Write(refId); InvokeCallbacksAndExecute(obj, WriteObjectInner); } refId++; } while(identifier.Count > refId); } } finally { for(var i = identifier.Count - 1; i >= 0; i--) { Completed(identifier.GetObject(i)); } foreach(var postHook in postSerializationHooks) { postHook(); } PrepareForNextWrite(); } }
private void PrepareForNextWrite() { if(referencePreservation != ReferencePreservation.DoNotPreserve) { identifierCountPreviousSession = identifier.Count; } else { inlineWritten.Clear(); } currentlyWrittenTypes.Clear(); if(referencePreservation == ReferencePreservation.UseWeakReference) { identifierContext = identifier.GetContext(); } if(referencePreservation != ReferencePreservation.Preserve) { identifier = null; } }
/// <summary> /// Writes the given object along with the ones referenced by it. /// </summary> /// <param name='o'> /// The object to write. /// </param> public void WriteObject(object o) { if(o == null || Helpers.CheckTransientNoCache(o.GetType())) { throw new ArgumentException("Cannot write a null object or a transient object."); } objectsWrittenThisSession = 0; if(referencePreservation != ReferencePreservation.Preserve) { identifier = identifierContext == null ? new ObjectIdentifier() : new ObjectIdentifier(identifierContext); identifierContext = null; } var identifiersCount = identifier.Count; identifier.GetId(o); var firstObjectIsNew = identifiersCount != identifier.Count; try { // first object is always written InvokeCallbacksAndWriteObject(o); if(firstObjectIsNew) { objectsWrittenThisSession++; } while(identifier.Count - identifierCountPreviousSession > objectsWrittenThisSession) { if(!inlineWritten.Contains(identifierCountPreviousSession + objectsWrittenThisSession)) { InvokeCallbacksAndWriteObject(identifier[identifierCountPreviousSession + objectsWrittenThisSession]); } objectsWrittenThisSession++; } } finally { foreach(var postHook in postSerializationHooks) { postHook(); } PrepareForNextWrite(); } }