Context for reading and writing streams: SetContext, GetContext

The destination stream is passed as an argument along with the instance being flattened. Every MCollectible instance must have a member function like this one. If you want to flatten multiple instances to a stream, set the context of the stream before making this call.

The TContext argument is used in the case where multiple references to the same object are flattened and packed together on the same stream. The TContext is basically a dynamic dictionary built during the flattening process to assign references to repeated object instances from a set of objects to be saved. If you only want to save a single instance, then the TContext can be generated automatically by the system.

If you want to save multiple instances (which might point to overlapping instances), you need to provide a TContext. The code sample below shows both single and multiple instance flattening techniques:

      // Flatten a single instance.
      TPersistentClassA* a = new TPersistentClassA('a', 5);
      TContiguousMemoryStream aStream(new char[100], 100);
      Flatten(a,aStream);
      
      // FlattenPointer two instances with shared parts
      TPersistentClassD* d = new TPersistentClassD("me", "cguy", "bguy", a);
      TMemory aStream(new char[100], 100);
      TContext aContext;
      if (!aStream>GetContext())
          aStream>SetContext(&aContext);
      aStream.FlattenPointer(d);
      aStream.FlattenPointer(a);
      if (aStream>GetContext() == &aContext)
          aStream>SetContext(NIL);

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker