InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PersistSTLTypes.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Abhishek Gulati
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 
24 
25 #ifndef __PersistSTL__
26 #define __PersistSTL__
27 
28 #include "IPMStream.h"
29 #include <map>
30 #include <string>
31 #include <vector>
32 
41 namespace Persist
42 {
46  template <typename _T1, typename _T2>
47  struct Serializer < std::pair<_T1, _T2> >
48  {
49  inline static void Read (IPMStream* stream, std::pair<_T1, _T2>& p)
50  {
51  ASSERT (stream && stream->IsReading());
52 
53  Persist::Read (stream, p.first);
54  Persist::Read (stream, p.second);
55  }
56 
57  inline static void Write (IPMStream* stream, const std::pair<_T1, _T2>& p)
58  {
59  ASSERT (stream && stream->IsWriting());
60 
61  Persist::Write (stream, p.first);
62  Persist::Write (stream, p.second);
63  }
64  };
65 
72  template <>
73  inline void Read (IPMStream* stream, std::string& str)
74  {
75  ASSERT (stream && stream->IsReading());
76 
77  int32 size;
78  Persist::Read(stream, size);
79 
80  str.resize (size);
81 
82  // Get the pointer to the start of the string buffer.
83  char* buf = &str[0];
84  stream->XferByte ((uchar*)buf, size);
85  }
86 
93  template <>
94  inline void Write (IPMStream* stream, const std::string& str)
95  {
96  ASSERT (stream && stream->IsWriting());
97 
98  int32 size = (int32)str.size();
99  Persist::Write(stream, size);
100 
101  // Get the pointer to the start of the string buffer.
102  const char* buf = &str[0];
103  stream->XferByte ((uchar*)buf, size);
104  }
105 
109  template <typename T>
110  struct Serializer < std::vector<T> >
111  {
112  inline static void Read (IPMStream* stream, std::vector<T>& v)
113  {
114  ASSERT (stream && stream->IsReading());
115 
116  v.clear();
117 
118  int32 size;
119  Persist::Read (stream, size);
120 
121  v.resize(size);
122  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i)
123  {
124  Persist::Read(stream, *i);
125  }
126  }
127 
128  inline static void Write (IPMStream* stream, const std::vector<T>& v)
129  {
130  ASSERT (stream && stream->IsWriting());
131 
132  const int32 size = static_cast<int32>(v.size());
133  Persist::Write (stream, size);
134 
135  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
136  {
137  Persist::Write(stream, *i);
138  }
139  }
140  };
141 
145  template <typename _KeyType, typename _DataType>
146  struct Serializer < std::map<_KeyType, _DataType> >
147  {
148  inline static void Read (IPMStream* stream, std::map<_KeyType, _DataType>& m)
149  {
150  ASSERT (stream && stream->IsReading());
151 
152  m.clear();
153 
154  int32 size;
155  Persist::Read (stream, size);
156 
157  while(size--)
158  {
159  _KeyType key;
160  Persist::Read (stream, key);
161 
162  _DataType data;
163  Persist::Read (stream, data);
164 
165  const bool unique = m.insert (std::pair<const _KeyType, _DataType> (key, data)).second;
166  ASSERT_MSG (unique, "Duplicate key found while deserializing a map.");
167  }
168  }
169 
170  inline static void Write (IPMStream* stream, const std::map<_KeyType, _DataType>& m)
171  {
172  ASSERT (stream && stream->IsWriting());
173 
174  const int32 size = (int32)m.size();
175  Persist::Write (stream, size);
176 
177  for (typename std::map<_KeyType, _DataType>::const_iterator i = m.begin(); i != m.end(); ++i)
178  {
179  const _KeyType& key = i->first;
180  Persist::Write(stream, key);
181 
182  const _DataType& data = i->second;
183  Persist::Write (stream, data);
184  }
185  }
186  };
187 }
188 
189 #endif