BG XLC: Use tr1/unordered_map
[charm.git] / src / util / pup_stl.h
blob691e9c6bcb55b44ad771f1fc58ae0ef536cfaa42
1 /*
2 Pup routines for STL classes.
4 After including this header, you can parameter-marshall
5 an variable consisting of STL vectors, lists, maps,
6 strings, or pairs.
8 This includes variables of type "std::list<int>", or even
9 "std::map<double, std::vector<std::string> >".
11 NOT included are the rarer types like valarray or slice,
12 vector<bool>, set or multiset, or deque.
14 Orion Sky Lawlor, olawlor@acm.org, 7/22/2002
16 #ifndef _UIUC_CHARM_PUP_STL_H
17 #define _UIUC_CHARM_PUP_STL_H
19 /*It's kind of annoying that we have to drag all these headers in
20 just so the std:: parameter declarations will compile.
22 #include <set>
23 #include <vector>
24 #include <deque>
25 #include <list>
26 #include <map>
27 #if !CMK_USING_XLC
28 #include <unordered_map>
29 #else
30 #include <tr1/unordered_map>
31 #endif
32 #include <string>
33 #include <complex>
34 #include <utility> /*for std::pair*/
35 #include "pup.h"
37 namespace PUP {
38 /*************** Simple classes ***************/
39 // Non-const version is required for puping std::pair
40 template <class A,class B>
41 inline void operator|(er &p,typename std::pair<A,B> &v);
42 template <class A,class B>
43 inline void operator|(er &p,typename std::pair<const A,B> &v);
44 template <class T>
45 inline void operator|(er &p,std::complex<T> &v);
46 template <class charType>
47 inline void operator|(er &p,typename std::basic_string<charType> &v);
48 inline void operator|(er &p,std::string &v);
49 template <class container>
50 inline size_t PUP_stl_container_size(er &p,container &c);
51 template <class container, class dtype>
52 inline void PUP_stl_container_items(er &p,container &c);
53 template <> inline void PUP_stl_container_items<std::vector<bool>,bool>(er &p, std::vector<bool> &c);
54 template <class container,class dtype>
55 inline void PUP_stl_container(er &p,container &c);
56 template <class container,class dtype>
57 inline void PUP_stl_map(er &p,container &c);
58 template <class T>
59 inline void operator|(er &p,typename std::vector<T> &v);
60 template <class T>
61 inline void operator|(er &p,typename std::list<T> &v);
62 template <class V,class T,class Cmp>
63 inline void operator|(er &p,typename std::map<V,T,Cmp> &m);
64 template <class V,class T,class Cmp>
65 inline void operator|(er &p,typename std::multimap<V,T,Cmp> &m);
66 template <class T>
67 inline void operator|(er &p,typename std::set<T> &m);
69 template <class A,class B>
70 inline void operator|(er &p,typename std::pair<A,B> &v)
72 p.syncComment(sync_index);
73 p|v.first;
74 p.syncComment(sync_item);
75 p|v.second;
77 // Const version is required for puping std::map
78 template <class A,class B>
79 inline void operator|(er &p,typename std::pair<const A,B> &v)
81 p.syncComment(sync_index);
82 p|*(A *)&v.first; /* cast away constness on A */
83 p.syncComment(sync_item);
84 p|v.second;
86 template <class T>
87 inline void operator|(er &p,std::complex<T> &v)
89 T re=v.real(), im=v.imag();
90 p|re; p|im;
91 v=std::complex<T>(re,im);
93 template <class charType>
94 inline void operator|(er &p,typename std::basic_string<charType> &v)
96 size_t nChar=v.length();
97 p|nChar;
98 if (p.isUnpacking()) { //Unpack to temporary buffer
99 charType *buf=new charType[nChar];
100 p(buf,nChar);
101 v=std::basic_string<charType>(buf,nChar);
102 delete[] buf;
104 else /*packing*/ { //Do packing in-place from data
105 //Have to cast away constness here
106 p((charType *)v.data(),nChar);
109 inline void operator|(er &p,std::string &v)
111 p.syncComment(sync_begin_object,"std::string");
112 size_t nChar=v.length();
113 p|nChar;
114 if (p.isUnpacking()) { //Unpack to temporary buffer
115 char *buf=new char[nChar];
116 p(buf,nChar);
117 v=std::basic_string<char>(buf,nChar);
118 delete[] buf;
120 else /*packing*/ { //Do packing in-place from data
121 //Have to cast away constness here
122 p((char *)v.data(),nChar);
124 p.syncComment(sync_end_object);
127 /**************** Containers *****************/
129 //Impl. util: pup the length of a container
130 template <class container>
131 inline size_t PUP_stl_container_size(er &p,container &c) {
132 size_t nElem=c.size();
133 p|nElem;
134 return nElem;
137 //Impl. util: pup each current item of a container (no allocation)
138 template <class container, class dtype>
139 inline void PUP_stl_container_items(er &p,container &c) {
140 for (typename container::iterator it=c.begin();
141 it!=c.end();
142 ++it) {
143 p.syncComment(sync_item);
144 // Cast away the constness (needed for std::set)
145 p|*(dtype *)&(*it);
149 // Specialized to work with vector<bool>
150 template<>
151 inline void PUP_stl_container_items<std::vector<bool>, bool>(er &p, std::vector<bool> &c)
153 std::deque<bool> q(c.begin(), c.end());
155 for (std::deque<bool>::iterator it = q.begin(); it != q.end(); it++)
157 p.syncComment(sync_item);
158 p|*it;
162 template <class container,class dtype>
163 inline void PUP_stl_container(er &p,container &c) {
164 p.syncComment(sync_begin_array);
165 size_t nElem=PUP_stl_container_size(p,c);
166 if (p.isUnpacking())
167 { //Unpacking: Extract each element and push_back:
168 c.resize(0);
169 for (size_t i=0;i<nElem;i++) {
170 p.syncComment(sync_item);
171 dtype n;
172 p|n;
173 c.push_back(n);
176 else PUP_stl_container_items<container, dtype>(p,c);
177 p.syncComment(sync_end_array);
179 //Map objects don't have a "push_back", while vector and list
180 // don't have an "insert", so PUP_stl_map isn't PUP_stl_container
181 template <class container,class dtype>
182 inline void PUP_stl_map(er &p,container &c) {
183 p.syncComment(sync_begin_list);
184 size_t nElem=PUP_stl_container_size(p,c);
185 if (p.isUnpacking())
186 { //Unpacking: Extract each element and insert:
187 for (size_t i=0;i<nElem;i++) {
188 dtype n;
189 p|n;
190 c.insert(n);
193 else PUP_stl_container_items<container, dtype>(p,c);
194 p.syncComment(sync_end_list);
197 template <class T>
198 inline void operator|(er &p,typename std::vector<T> &v)
199 { PUP_stl_container<std::vector<T>,T>(p,v); }
200 template <class T>
201 inline void operator|(er &p,typename std::list<T> &v)
202 { PUP_stl_container<std::list<T>,T>(p,v); }
204 template <class V,class T,class Cmp>
205 inline void operator|(er &p,typename std::map<V,T,Cmp> &m)
206 //{ PUP_stl_map<std::map<V,T,Cmp>,std::pair<const V,T> >(p,m); } // 'const' confuses old version of a SUN CC compiler
207 { PUP_stl_map<std::map<V,T,Cmp>,std::pair<V,T> >(p,m); }
208 #if !CMK_USING_XLC
209 template <class V,class T,class Cmp>
210 inline void operator|(er &p,typename std::unordered_map<V,T,Cmp> &m)
211 //{ PUP_stl_map<std::unordered_map<V,T,Cmp>,std::pair<const V,T> >(p,m); } // 'const' confuses old version of a SUN CC compiler
212 { PUP_stl_map<std::unordered_map<V,T,Cmp>,std::pair<V,T> >(p,m); }
213 #else
214 template <class V,class T,class Cmp>
215 inline void operator|(er &p,typename std::tr1::unordered_map<V,T,Cmp> &m)
216 //{ PUP_stl_map<std::unordered_map<V,T,Cmp>,std::pair<const V,T> >(p,m); } // 'const' confuses old version of a SUN CC compiler
217 { PUP_stl_map<std::tr1::unordered_map<V,T,Cmp>,std::pair<V,T> >(p,m); }
218 #endif
219 template <class V,class T,class Cmp>
220 inline void operator|(er &p,typename std::multimap<V,T,Cmp> &m)
221 { PUP_stl_map<std::multimap<V,T,Cmp>,std::pair<const V,T> >(p,m); }
222 template <class T>
223 inline void operator|(er &p,typename std::set<T> &m)
224 { PUP_stl_map<std::set<T>,T >(p,m); }
228 #endif