2 Pup routines for STL classes.
4 After including this header, you can parameter-marshall
5 an variable consisting of STL vectors, lists, maps,
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.
28 #include <unordered_map>
30 #include <tr1/unordered_map>
34 #include <utility> /*for std::pair*/
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
);
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
);
59 inline void operator|(er
&p
,typename
std::vector
<T
> &v
);
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
);
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
);
74 p
.syncComment(sync_item
);
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
);
87 inline void operator|(er
&p
,std::complex<T
> &v
)
89 T re
=v
.real(), im
=v
.imag();
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();
98 if (p
.isUnpacking()) { //Unpack to temporary buffer
99 charType
*buf
=new charType
[nChar
];
101 v
=std::basic_string
<charType
>(buf
,nChar
);
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();
114 if (p
.isUnpacking()) { //Unpack to temporary buffer
115 char *buf
=new char[nChar
];
117 v
=std::basic_string
<char>(buf
,nChar
);
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();
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();
143 p
.syncComment(sync_item
);
144 // Cast away the constness (needed for std::set)
149 // Specialized to work with vector<bool>
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
);
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
);
167 { //Unpacking: Extract each element and push_back:
169 for (size_t i
=0;i
<nElem
;i
++) {
170 p
.syncComment(sync_item
);
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
);
186 { //Unpacking: Extract each element and insert:
187 for (size_t i
=0;i
<nElem
;i
++) {
193 else PUP_stl_container_items
<container
, dtype
>(p
,c
);
194 p
.syncComment(sync_end_list
);
198 inline void operator|(er
&p
,typename
std::vector
<T
> &v
)
199 { PUP_stl_container
<std::vector
<T
>,T
>(p
,v
); }
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
); }
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
); }
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
); }
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
); }
223 inline void operator|(er
&p
,typename
std::set
<T
> &m
)
224 { PUP_stl_map
<std::set
<T
>,T
>(p
,m
); }