home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWStream / Sources / FWStrmRW.cpp < prev   
Encoding:
Text File  |  1995-11-08  |  4.9 KB  |  180 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWStrmRW.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWSTRMRW_H
  13. #include "FWStrmRW.h"
  14. #endif
  15.  
  16. #ifndef FWEXCLIB_H
  17. #include "FWExcLib.h"
  18. #endif
  19.  
  20. #ifndef FWOBJREG_H
  21. #include "FWObjReg.h"
  22. #endif
  23.  
  24. #if FW_LIB_EXPORT_PRAGMAS
  25. #pragma lib_export on
  26. #endif
  27.  
  28. #pragma segment FWStream
  29.  
  30.  
  31. //========================================================================================
  32. //    Constants having file scope
  33. //========================================================================================
  34.  
  35. enum
  36. {
  37.     kIDOnly, kIDAndValue
  38. };
  39.  
  40. //========================================================================================
  41. //    CLASS FW_CReadableStream
  42. //========================================================================================
  43.  
  44. //----------------------------------------------------------------------------------------
  45. // FW_CReadableStream::FW_CReadableStream
  46. //----------------------------------------------------------------------------------------
  47.  
  48. FW_CReadableStream::FW_CReadableStream(FW_CSink* sink,
  49.                                        FW_CReadableStreamFormatter* formatter,
  50.                                        FW_CObjectRegistry* objectRegistry) :
  51.     fSink(sink),
  52.     fStreamCreatedFormatter(formatter ? FALSE : TRUE),
  53.     fFormatter(formatter),
  54.     fArchiveCreatedObjectRegistry(objectRegistry ? FALSE : TRUE),
  55.     fObjectRegistry(objectRegistry)
  56. {
  57.     if (fStreamCreatedFormatter)
  58.         fFormatter = FW_NEW(FW_CReadableStreamFormatter, ());
  59.  
  60.     if (fArchiveCreatedObjectRegistry)
  61.         fObjectRegistry = new FW_CBasicObjectRegistry();
  62.  
  63.     FW_END_CONSTRUCTOR
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // FW_CReadableStream::~FW_CReadableStream
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_CReadableStream::~FW_CReadableStream()
  71. {
  72.     FW_START_DESTRUCTOR
  73.     
  74.     if (fArchiveCreatedObjectRegistry)
  75.         delete fObjectRegistry;
  76.  
  77.     if (fStreamCreatedFormatter)
  78.         delete fFormatter;
  79. }
  80.  
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // FW_CReadableStream::InputObject
  84. //----------------------------------------------------------------------------------------
  85.  
  86. void* FW_CReadableStream::InputObject(InputFunction inputFunction)
  87. {
  88.     FW_CObjectRegistry::ID id;
  89.     short byValue;
  90.     void* objectPtr;
  91.  
  92.     *this >> byValue;
  93.     *this >> id;
  94.  
  95.     switch (byValue)
  96.     {
  97.         case kIDAndValue:
  98.             objectPtr = inputFunction(*this);
  99.             fObjectRegistry->Register(objectPtr, id);
  100.             break;
  101.         case kIDOnly:
  102.             objectPtr = (void*)fObjectRegistry->Lookup(id);
  103.             break;
  104.  
  105.         default:
  106.             FW_ASSERT(FALSE);
  107.             break;
  108.     }
  109.  
  110.     return objectPtr;
  111. }
  112.  
  113.  
  114. //========================================================================================
  115. //    CLASS FW_CWritableStream
  116. //========================================================================================
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // FW_CWritableStream::FW_CWritableStream
  120. //----------------------------------------------------------------------------------------
  121.  
  122. FW_CWritableStream::FW_CWritableStream(FW_CSink* sink,
  123.                                        FW_CWritableStreamFormatter* formatter,
  124.                                        FW_CObjectRegistry* objectRegistry) :
  125.     fSink(sink),
  126.     fStreamCreatedFormatter(formatter ? FALSE : TRUE),
  127.     fFormatter(formatter),
  128.     fArchiveCreatedObjectRegistry(objectRegistry ? FALSE : TRUE),
  129.     fObjectRegistry(objectRegistry)
  130. {
  131.     if (fStreamCreatedFormatter)
  132.         fFormatter = FW_NEW(FW_CWritableStreamFormatter, ());
  133.  
  134.     if (fArchiveCreatedObjectRegistry)
  135.         fObjectRegistry = new FW_CBasicObjectRegistry();
  136.  
  137.     FW_END_CONSTRUCTOR
  138. }
  139.  
  140. //----------------------------------------------------------------------------------------
  141. // FW_CWritableStream::~FW_CWritableStream
  142. //----------------------------------------------------------------------------------------
  143.  
  144. FW_CWritableStream::~FW_CWritableStream()
  145. {
  146.     FW_START_DESTRUCTOR
  147.     
  148.     if (fArchiveCreatedObjectRegistry)
  149.         delete fObjectRegistry;
  150.     
  151.     if (fStreamCreatedFormatter)
  152.         delete fFormatter;
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // FW_CWritableStream::OutputObject
  157. //----------------------------------------------------------------------------------------
  158.  
  159. void FW_CWritableStream::OutputObject(OutputFunction outputFunction,
  160.                                       const void* object)
  161. {
  162.     FW_CObjectRegistry::ID id = fObjectRegistry->Lookup(object);
  163.  
  164.     if (id == FW_CObjectRegistry::kNotInRegistry)
  165.     {
  166.         id = fObjectRegistry->Register(object);
  167.         *this << ((short)kIDAndValue) << id;
  168.         // Explicitly cast the kIDOnly into a short because on the mac
  169.         // an enum can be a char.
  170.  
  171.         outputFunction(*this, object);
  172.     }
  173.     else
  174.     {
  175.         *this << ((short)kIDOnly) << id;
  176.         // Explicitly cast the kIDOnly into a short because on the mac
  177.         // an enum can be a char.
  178.     }
  179. }
  180.