InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IPMStream.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Robin Briggs
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //
23 // Purpose:
24 // This is the interface used for serializing objects.
25 //
26 // Streams are normally created for you by methods in the StreamUtil class.
27 //
28 //========================================================================================
29 
30 #include "IPMUnknown.h"
31 
32 #ifndef __IPMSTREAM__
33 #define __IPMSTREAM__
34 
35 
37 typedef enum
38 {
40  kSeekFromStart = 0x01,
42  kSeekFromCurrent = 0x02,
44  kSeekFromEnd = 0x04
45 } SeekFromWhere;
46 
47 /* Modes used for opening files */
48 typedef enum
49 {
51  kOpenIn = 0x01,
53  kOpenOut = 0x02,
55  kOpenApp = 0x04,
57  kOpenTrunc = 0x08
58 } OpenMode;
59 
61 typedef enum
62 {
64  kStreamStateGood = 0,
66  kStreamStateEOF,
68  kStreamStateFailure,
70  kStreamStateClosed
71 } StreamState;
72 
73 #ifdef DEBUG
74 #include "IDBStreamData.h"
75 #include "DocumentID.h"
76 #include "WorkspaceID.h"
77 
78 #define ASSERT_IF_WRITING_TO_DOCUMENT(db_Stream) if(db_Stream->IsWriting()) {\
79  InterfacePtr<IDBStreamData> db_StreamData(db_Stream,IID_IDBSTREAMDATA);\
80  if (db_StreamData != nil)\
81  {\
82  IDataBase *stream_DB = db_StreamData->GetDataBase();\
83  UID db_rootUID = stream_DB->GetRootUID();\
84  ASSERT_MSG(stream_DB->GetClass(db_rootUID) != kDocBoss,"This shouldn't be written to a pub database!");\
85  }}
86 
87 #define ASSERT_IF_WRITING_TO_DEFAULTS(db_Stream) if(db_Stream->IsWriting()) {\
88  InterfacePtr<IDBStreamData> db_StreamData(db_Stream,IID_IDBSTREAMDATA);\
89  if (db_StreamData != nil)\
90  {\
91  IDataBase *stream_DB = db_StreamData->GetDataBase();\
92  UID db_rootUID = stream_DB->GetRootUID();\
93  ASSERT_MSG(stream_DB->GetClass(db_rootUID) != kWorkspaceBoss,"This shouldn't be written to a defaults database!");\
94  }}
95 
96 #else
97 #define ASSERT_IF_WRITING_TO_DOCUMENT(stream)
98 #define ASSERT_IF_WRITING_TO_DEFAULTS(stream)
99 #endif
100 
101 // Use IPMStream::XferEnum instead of the XFER_ENUM and XFER_ENUM16 macros
102 #define XFER_ENUM(stream, enumVal) { stream->XferInt32((int32&)enumVal); }
103 #define XFER_ENUM16(stream, enumVal) { int16 _dummy = (int16) enumVal; (int32&) enumVal = (int32) stream->XferInt16(_dummy); }
104 
111 class IPMStream : public IPMUnknown
112 {
113  public:
115  typedef enum _StreamType {
116  kUnknown,
121  } StreamType;
122 
125  template<typename T> inline void XferEnum(T& e)
126  {
127  int32 tmp(0);
128  if (IsReading()) {
129  XferInt32(tmp);
130  e = static_cast<T>(tmp);
131  }
132  else {
133  tmp = static_cast<int32>(e);
134  XferInt32(tmp);
135  }
136  }
137 
138  virtual ~IPMStream() {}
141  virtual uchar XferByte(uchar& chr) = 0;
144  virtual int32 XferByte(uchar *buf, int32 num) = 0;
145 
148  virtual bool16 XferBool(bool16& chr) = 0;
151  virtual int32 XferBool(bool16 *buf, int32 num) = 0;
152 
155  virtual int16 XferInt16(int16& i) = 0;
158  virtual int32 XferInt16(int16 *buf, int32 num) = 0;
159 
162  virtual int32 XferInt32(int32& i) = 0;
165  virtual int32 XferInt32(int32 *buf, int32 num) = 0;
166 
169  virtual int64 XferInt64(int64& i) = 0;
170 
176  virtual uintptr_t XferPointer(uintptr_t& i) = 0;
177 
182  template<class T>
184  {
185  XferInt32((int32&)id.Get());
186  return id;
187  }
188 
193  template<class T>
194  inline int32 XferID(IDType<T>* buf, int32 count)
195  {
196  for (int32 i = 0; i < count; ++i)
197  XferID(buf[i]);
198  return count;
199  }
200 
203  virtual ClassID XferID(ClassID& i) = 0;
206  virtual int32 XferID(ClassID *buf, int32 num) = 0;
207 
211  virtual ImplementationID XferID(ImplementationID& i) = 0;
214  virtual int32 XferID(ImplementationID *buf, int32 num) = 0;
215 
219  virtual WidgetID XferID(WidgetID& i) = 0;
222  virtual int32 XferID(WidgetID *buf, int32 num) = 0;
223 
227  virtual PluginID XferID(PluginID& i) = 0;
230  virtual int32 XferID(PluginID *buf, int32 num) = 0;
231 
235  virtual PMIID XferID(PMIID& i) = 0;
238  virtual int32 XferID(PMIID *buf, int32 num) = 0;
239 
243  virtual ActionID XferID(ActionID& i) = 0;
246  virtual int32 XferID(ActionID *buf, int32 num) = 0;
247 
251  virtual ScriptElementID XferID(ScriptElementID& i) = 0;
254  virtual int32 XferID(ScriptElementID *buf, int32 num) = 0;
255 
258  virtual ScriptID XferID(ScriptID& i) = 0;
261  virtual int32 XferID(ScriptID *buf, int32 num) = 0;
262 
266  virtual LinkClientID XferID(LinkClientID& i) = 0;
269  virtual int32 XferID(LinkClientID *buf, int32 num) = 0;
270 
271 
278  virtual UID XferObject(UID& u) = 0;
281  virtual int32 XferObject(UID *buf, int32 num) = 0;
282 
291  virtual UID XferReference(UID& u) = 0;
294  virtual int32 XferReference(UID *buf, int32 num) = 0;
295 
298  virtual PMReal& XferRealNumber(PMReal& r) = 0;
301  virtual int32 XferRealNumber(PMReal *buf, int32 num) = 0;
302 
307  virtual void Comment(const char* comment) = 0;
308 
312  virtual int64 Seek(int64 numberOfBytes, SeekFromWhere fromHere) = 0;
316  virtual void SetEndOfStream() = 0;
317 
320  virtual void Flush() = 0;
321 
325  virtual StreamState GetStreamState() = 0;
326 
329  virtual bool16 IsReading() const = 0;
332  virtual bool16 IsWriting() const = 0;
333 
346  virtual bool16 IsWritingForDuplication(IDataBase** optDestDB = nil) const = 0;
347 
353  virtual void SetSwapping(bool16 swapOn) = 0;
356  virtual bool16 GetSwapping() const = 0;
357 
364  virtual bool16 Open() = 0;
367  virtual void Close() = 0;
368 
369 };
370 
371 // specialize adobe::type_info<> to avoid typeid problems across DLL boundaries
372 ADOBE_NAME_TYPE_0("ipmstream:indesign:adobe",IPMStream);
373 
374 namespace Persist
375 {
396  template <typename T>
397  struct Serializer
398  {
399  inline static void Read (IPMStream* stream, T& t)
400  {
401  ASSERT (stream && stream->IsReading());
402 
403  t.ReadWrite(stream);
404  }
405 
406  inline static void Write (IPMStream* stream, const T& t)
407  {
408  ASSERT (stream && stream->IsWriting());
409 
410  // Cast constness away to to match ReadWrite signature. This should be safe since ReadWrite
411  // implementations do not modify the object when the stream is being written to.
412  T& tNonConst = const_cast<T&> (t);
413  tNonConst.ReadWrite(stream);
414  }
415  };
416 
436  template<typename T>
437  inline void Read (IPMStream* stream, T& t)
438  {
439  Serializer<T>::Read(stream, t);
440  }
441 
460  template<typename T>
461  inline void Write (IPMStream* stream, const T& t)
462  {
463  Serializer<T>::Write(stream, t);
464  }
465 }
466 
467 
468 #endif