InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
k2VectorStreaming.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Jesse Jones
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 // Summary: Template functions used to stream K2Vector's in and out.
24 //
25 //========================================================================================
26 
27 #ifndef __k2VectorStreaming__
28 #define __k2VectorStreaming__
29 
30 #include "IPMStream.h"
31 #include "KeyValuePair.h"
32 #include "PMRect.h"
33 #include "Swap.h"
34 #include "WideString.h"
35 
36 #ifdef PUBLIC_BUILD
37 #endif
38 
39 
40 // ===================================================================================
41 // Internal Functions
42 // ===================================================================================
43 
44 // ----- Primitive Streaming -----
45 inline void K2Write(IPMStream* stream, int8 n)
46 {
47  unsigned char temp = n;
48  stream->XferByte(temp);
49 }
50 
51 inline void K2Read(IPMStream* stream, int8& n)
52 {
53  unsigned char temp(0);
54  stream->XferByte(temp);
55  n = temp;
56 }
57 
58 
59 inline void K2Write(IPMStream* stream, int16 n)
60 {
61  stream->XferInt16(n);
62 }
63 
64 inline void K2Read(IPMStream* stream, int16& n)
65 {
66  stream->XferInt16(n);
67 }
68 
69 
70 inline void K2Write(IPMStream* stream, int32 n)
71 {
72  stream->XferInt32(n);
73 }
74 
75 inline void K2Read(IPMStream* stream, int32& n)
76 {
77  stream->XferInt32(n);
78 }
79 
80 
81 inline void K2Write(IPMStream* stream, uint8 n)
82 {
83  stream->XferByte(n);
84 }
85 
86 inline void K2Read(IPMStream* stream, uint8& n)
87 {
88  stream->XferByte(n);
89 }
90 
91 
92 inline void K2Write(IPMStream* stream, uint16 n)
93 {
94  int16 temp = n;
95  stream->XferInt16(temp);
96 }
97 
98 inline void K2Read(IPMStream* stream, uint16& n)
99 {
100  int16 temp(0);
101  stream->XferInt16(temp);
102  n = temp;
103 }
104 
105 
106 inline void K2Write(IPMStream* stream, uint32 n)
107 {
108  int32 temp = n;
109  stream->XferInt32(temp);
110 }
111 
112 inline void K2Read(IPMStream* stream, uint32& n)
113 {
114  int32 temp(0);
115  stream->XferInt32(temp);
116  n = temp;
117 }
118 
119 inline void K2Write(IPMStream* stream, const PMReal& n)
120 {
121  PMReal temp = n;
122  stream->XferRealNumber(temp);
123 }
124 
125 inline void K2Read(IPMStream* stream, PMReal& n)
126 {
127  stream->XferRealNumber(n);
128 }
129 
130 
131 inline void K2Write(IPMStream* stream, const PMPoint& n)
132 {
133  K2Write(stream, n.X());
134  K2Write(stream, n.Y());
135 }
136 
137 inline void K2Read(IPMStream* stream, PMPoint& n)
138 {
139  K2Read(stream, n.X());
140  K2Read(stream, n.Y());
141 }
142 
143 
144 inline void K2Write(IPMStream* stream, const PMRect& n)
145 {
146  K2Write(stream, n.Top());
147  K2Write(stream, n.Left());
148  K2Write(stream, n.Bottom());
149  K2Write(stream, n.Right());
150 }
151 
152 inline void K2Read(IPMStream* stream, PMRect& n)
153 {
154  K2Read(stream, n.Top());
155  K2Read(stream, n.Left());
156  K2Read(stream, n.Bottom());
157  K2Read(stream, n.Right());
158 }
159 
160 // ----- ReadWrite Object Streaming -----
161 
162 #define DEFINEK2READWRITE( K2TYPE ) \
163  inline void K2Write(IPMStream* stream, const K2TYPE& n) { K2TYPE& temp = const_cast<K2TYPE&>(n); temp.ReadWrite(stream); } \
164  inline void K2Read(IPMStream* stream, K2TYPE& n) { n.ReadWrite(stream); }
165 
166 DEFINEK2READWRITE( PMString )
167 DEFINEK2READWRITE( WideString )
168 
169 // ----- IDType Streaming -----
170 
171 template<class T>
172 inline void K2Write( IPMStream* s, const IDType<T>& id )
173 {
174  IDType<T>& temp = const_cast< IDType<T>& >(id) ;
175  s->XferID( temp ) ;
176 }
177 
178 template<class T>
179 inline void K2Read( IPMStream* s, IDType<T>& id )
180 {
181  s->XferID( id ) ;
182 }
183 
184 // ----- KeyValuePair Streaming -----
185 
186 template <class _T1, class _T2>
187 inline void K2Write( IPMStream* s, const KeyValuePair<_T1,_T2>& kvp )
188 {
189  KeyValuePair<_T1,_T2>& temp = const_cast< KeyValuePair<_T1,_T2>& >(kvp) ;
190  K2Write( s, temp.fKey ) ;
191  K2Write( s, temp.fValue ) ;
192 }
193 
194 template <class _T1, class _T2>
195 inline void K2Read( IPMStream* s, KeyValuePair<_T1,_T2>& kvp )
196 {
197  K2Read( s, kvp.fKey ) ;
198  K2Read( s, kvp.fValue ) ;
199 }
200 
201 // ----- Optimized Block Streaming -----
202 template <class T>
203 void K2WriteBlock(IPMStream* stream, uint32 count, const T* block)
204 {
205  for (uint32 i = 0; i < count; ++i)
206  K2Write(stream, block[i]); // you'll have to provide a K2Write function for custom types
207 }
208 
209 template <class T>
210 void K2ReadBlock(IPMStream* stream, uint32 count, T* block)
211 {
212  for (uint32 i = 0; i < count; ++i)
213  K2Read(stream, block[i]); // you'll have to provide a K2Read function for custom types
214 }
215 
216 
217 template <>
218 inline void K2WriteBlock(IPMStream* stream, uint32 count, const char* block)
219 {
220  stream->XferByte((uint8*) block, count*sizeof(char));
221 }
222 
223 template <>
224 inline void K2ReadBlock(IPMStream* stream, uint32 count, char* block)
225 {
226  stream->XferByte((uint8*) block, count*sizeof(char));
227 }
228 
229 
230 template <>
231  void K2WriteBlock(IPMStream* stream, uint32 count, const int16* block);
232 
233 template <>
234  void K2ReadBlock(IPMStream* stream, uint32 count, int16* block);
235 
236 
237 template <>
238  void K2WriteBlock(IPMStream* stream, uint32 count, const uint16* block);
239 
240 template <>
241  void K2ReadBlock(IPMStream* stream, uint32 count, uint16* block);
242 
243 
244 template <>
245  void K2WriteBlock(IPMStream* stream, uint32 count, const int32* block);
246 
247 template <>
248  void K2ReadBlock(IPMStream* stream, uint32 count, int32* block);
249 
250 
251 #if 1 // $$$ not safe while UIDs are uint32's
252 template <>
253  void K2WriteBlock(IPMStream* stream, uint32 count, const uint32* block);
254 
255 template <>
256  void K2ReadBlock(IPMStream* stream, uint32 count, uint32* block);
257 #endif
258 
259 
260 // ----- Streaming -----
261 
262 class Uint32s_Cannot_Be_Streamed { // not instantiable (private default ctor)
264 };
265 
266 template <class T, class A>
267 void K2Write(IPMStream* stream, const K2Vector<T, A>& v)
268 {
269  ASSERT(stream != nil);
270  ASSERT(stream->IsWriting());
271 
272  using namespace K2Meta;
273  META_ASSERT<!IS_SAME_TYPE<T, uint32>::RET, Uint32s_Cannot_Be_Streamed>();
274 
275  int32 count = v.size();
276  stream->XferInt32(count);
277 
278  K2WriteBlock(stream, count, v.begin());
279 }
280 
281 template <class T, class A>
282 void K2Read(IPMStream* stream, K2Vector<T, A>& v)
283 {
284  ASSERT(stream != nil);
285  ASSERT(stream->IsReading());
286 
287  using namespace K2Meta;
288  META_ASSERT<!IS_SAME_TYPE<T, uint32>::RET, Uint32s_Cannot_Be_Streamed>();
289 
290  int32 count(0);
291  stream->XferInt32(count);
292  v.resize(count, T());
293 
294  K2ReadBlock(stream, count, v.begin());
295 }
296 
297 // ===================================================================================
298 // Global Functions
299 // ===================================================================================
300 
301 // ----- Vectors -----
302 template <class T, class A>
303 void K2ReadWrite(IPMStream* stream, K2Vector<T, A>& v)
304 {
305  if (stream->IsReading())
306  K2Read(stream, v);
307  else
308  K2Write(stream, v);
309 }
310 
311 template <class A>
312 void K2ReadWriteObject(IPMStream* stream, K2Vector<UID, A>& v)
313 {
314  stream->XferObject(v.begin(), v.size());
315 }
316 
317 template <class A>
318 void K2ReadWriteReference(IPMStream* stream, K2Vector<UID, A>& v)
319 {
320  stream->XferReference(v.begin(), v.size());
321 }
322 
323 
324 #endif // __k2VectorStreaming__