packing.h

00001 //==========================================================================
00002 //  PACKING.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //==========================================================================
00007 
00008 /*--------------------------------------------------------------*
00009   Copyright (C) 1992-2008 Andras Varga
00010   Copyright (C) 2006-2008 OpenSim Ltd.
00011 
00012   This file is distributed WITHOUT ANY WARRANTY. See the file
00013   `license' for details on this and other legal matters.
00014 *--------------------------------------------------------------*/
00015 
00016 #ifndef __PACKING_H
00017 #define __PACKING_H
00018 
00019 #include <vector>
00020 #include <list>
00021 #include <set>
00022 #include <map>
00023 #include "ccommbuffer.h"
00024 
00025 NAMESPACE_BEGIN
00026 
00027 //
00028 // This file contains methods for generated parsimPack/parsimUnpack methods
00029 //
00030 
00031 //
00032 // pack/unpack functions for primitive types
00033 //
00034 // Note: pack doesn't use "const", as the template rule somehow doesn't fire
00035 // then (VC++ 7.1)
00036 //
00037 #define DOPACKING(T,R) \
00038           inline void doPacking(cCommBuffer *b, /*const*/ T R a) {b->pack(a);}  \
00039           inline void doPacking(cCommBuffer *b, /*const*/ T *a, int n) {b->pack(a,n);}  \
00040           inline void doUnpacking(cCommBuffer *b, T& a) {b->unpack(a);}  \
00041           inline void doUnpacking(cCommBuffer *b, T *a, int n) {b->unpack(a,n);}
00042 #define _
00043 DOPACKING(char,_)
00044 DOPACKING(unsigned char,_)
00045 DOPACKING(bool,_)
00046 DOPACKING(short,_)
00047 DOPACKING(unsigned short,_)
00048 DOPACKING(int,_)
00049 DOPACKING(unsigned int,_)
00050 DOPACKING(long,_)
00051 DOPACKING(unsigned long,_)
00052 DOPACKING(float,_)
00053 DOPACKING(double,_)
00054 DOPACKING(long double,_)
00055 DOPACKING(char *,_)
00056 DOPACKING(SimTime,_)
00057 DOPACKING(opp_string,&)
00058 #undef _
00059 #undef DOPACKING
00060 
00061 //
00062 // Default pack/unpack function for arrays
00063 //
00064 template<typename T>
00065 void doPacking(cCommBuffer *b, /*const*/ T *t, int n)
00066 {
00067     for (int i=0; i<n; i++)
00068         doPacking(b, t[i]);
00069 }
00070 
00071 template<typename T>
00072 void doUnpacking(cCommBuffer *b, T *t, int n)
00073 {
00074     for (int i=0; i<n; i++)
00075         doUnpacking(b, t[i]);
00076 }
00077 
00078 
00079 //
00080 // Packing/unpacking an std::vector
00081 //
00082 template<typename T>
00083 void doPacking(cCommBuffer *buffer, /*const*/ std::vector<T>& v)
00084 {
00085     int n = v.size();
00086     doPacking(buffer, n);
00087     for (int i=0; i<n; i++)
00088         doPacking(buffer, v[i]);
00089 }
00090 
00091 template<typename T>
00092 void doUnpacking(cCommBuffer *buffer, std::vector<T>& v)
00093 {
00094     int n;
00095     doUnpacking(buffer, n);
00096     v.resize(n);
00097     for (int i=0; i<n; i++)
00098         doUnpacking(buffer, v[i]);
00099 }
00100 
00101 template<typename T, typename A>
00102 void doPacking(cCommBuffer *buffer, /*const*/ std::vector<T,A>& v)
00103 {
00104     int n = v.size();
00105     doPacking(buffer, n);
00106     for (int i=0; i<n; i++)
00107         doPacking(buffer, v[i]);
00108 }
00109 
00110 template<typename T, typename A>
00111 void doUnpacking(cCommBuffer *buffer, std::vector<T,A>& v)
00112 {
00113     int n;
00114     doUnpacking(buffer, n);
00115     v.resize(n);
00116     for (int i=0; i<n; i++)
00117         doUnpacking(buffer, v[i]);
00118 }
00119 
00120 
00121 //
00122 // Packing/unpacking an std::list
00123 //
00124 template<typename T>
00125 void doPacking(cCommBuffer *buffer, /*const*/ std::list<T>& l)
00126 {
00127     doPacking(buffer, (int)l.size());
00128     for (typename std::list<T>::const_iterator it = l.begin(); it != l.end(); it++)
00129         doPacking(buffer, (T&)*it);
00130 }
00131 
00132 template<typename T>
00133 void doUnpacking(cCommBuffer *buffer, std::list<T>& l)
00134 {
00135     int n;
00136     doUnpacking(buffer, n);
00137     for (int i=0; i<n; i++) {
00138         l.push_back(T());
00139         doUnpacking(buffer, l.back());
00140     }
00141 }
00142 
00143 template<typename T, typename A>
00144 void doPacking(cCommBuffer *buffer, /*const*/ std::list<T,A>& l)
00145 {
00146     doPacking(buffer, (int)l.size());
00147     for (typename std::list<T,A>::const_iterator it = l.begin(); it != l.end(); it++)
00148         doPacking(buffer, (T&)*it);
00149 }
00150 
00151 template<typename T, typename A>
00152 void doUnpacking(cCommBuffer *buffer, std::list<T,A>& l)
00153 {
00154     int n;
00155     doUnpacking(buffer, n);
00156     for (int i=0; i<n; i++) {
00157         l.push_back(T());
00158         doUnpacking(buffer, l.back());
00159     }
00160 }
00161 
00162 //
00163 // Packing/unpacking an std::set
00164 //
00165 template<typename T>
00166 void doPacking(cCommBuffer *buffer, /*const*/ std::set<T>& s)
00167 {
00168     doPacking(buffer, (int)s.size());
00169     for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); it++)
00170         doPacking(buffer, (T&)*it);
00171 }
00172 
00173 template<typename T>
00174 void doUnpacking(cCommBuffer *buffer, std::set<T>& s)
00175 {
00176     int n;
00177     doUnpacking(buffer, n);
00178     for (int i=0; i<n; i++) {
00179         T x;
00180         doUnpacking(buffer, x);
00181         s.insert(x);
00182     }
00183 }
00184 
00185 template<typename T, typename Tr>
00186 void doPacking(cCommBuffer *buffer, /*const*/ std::set<T,Tr>& s)
00187 {
00188     doPacking(buffer, (int)s.size());
00189     for (typename std::set<T,Tr>::const_iterator it = s.begin(); it != s.end(); it++)
00190         doPacking(buffer, (T&)*it);
00191 }
00192 
00193 template<typename T, typename Tr>
00194 void doUnpacking(cCommBuffer *buffer, std::set<T,Tr>& s)
00195 {
00196     int n;
00197     doUnpacking(buffer, n);
00198     for (int i=0; i<n; i++) {
00199         T x;
00200         doUnpacking(buffer, x);
00201         s.insert(x);
00202     }
00203 }
00204 
00205 template<typename T, typename Tr, typename A>
00206 void doPacking(cCommBuffer *buffer, /*const*/ std::set<T,Tr,A>& s)
00207 {
00208     doPacking(buffer, (int)s.size());
00209     for (typename std::set<T,Tr,A>::const_iterator it = s.begin(); it != s.end(); it++)
00210         doPacking(buffer, *it);
00211 }
00212 
00213 template<typename T, typename Tr, typename A>
00214 void doUnpacking(cCommBuffer *buffer, std::set<T,Tr,A>& s)
00215 {
00216     int n;
00217     doUnpacking(buffer, n);
00218     for (int i=0; i<n; i++) {
00219         T x;
00220         doUnpacking(buffer, x);
00221         s.insert(x);
00222     }
00223 }
00224 
00225 //
00226 // Packing/unpacking an std::map
00227 //
00228 template<typename K, typename V>
00229 void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V>& m)
00230 {
00231     doPacking(buffer, (int)m.size());
00232     for (typename std::map<K,V>::const_iterator it = m.begin(); it != m.end(); it++) {
00233         doPacking(buffer, (K&)it->first);
00234         doPacking(buffer, (V&)it->second);
00235     }
00236 }
00237 
00238 template<typename K, typename V>
00239 void doUnpacking(cCommBuffer *buffer, std::map<K,V>& m)
00240 {
00241     int n;
00242     doUnpacking(buffer, n);
00243     for (int i=0; i<n; i++) {
00244         K k; V v;
00245         doUnpacking(buffer, k);
00246         doUnpacking(buffer, v);
00247         m[k] = v;
00248     }
00249 }
00250 
00251 template<typename K, typename V, typename Tr>
00252 void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V,Tr>& m)
00253 {
00254     doPacking(buffer, (int)m.size());
00255     for (typename std::map<K,V,Tr>::const_iterator it = m.begin(); it != m.end(); it++) {
00256         doPacking(buffer, it->first);
00257         doPacking(buffer, it->second);
00258     }
00259 }
00260 
00261 template<typename K, typename V, typename Tr>
00262 void doUnpacking(cCommBuffer *buffer, std::map<K,V,Tr>& m)
00263 {
00264     int n;
00265     doUnpacking(buffer, n);
00266     for (int i=0; i<n; i++) {
00267         K k; V v;
00268         doUnpacking(buffer, k);
00269         doUnpacking(buffer, v);
00270         m[k] = v;
00271     }
00272 }
00273 
00274 template<typename K, typename V, typename Tr, typename A>
00275 void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V,Tr,A>& m)
00276 {
00277     doPacking(buffer, (int)m.size());
00278     for (typename std::map<K,V,Tr,A>::const_iterator it = m.begin(); it != m.end(); it++) {
00279         doPacking(buffer, it->first);
00280         doPacking(buffer, it->second);
00281     }
00282 }
00283 
00284 template<typename K, typename V, typename Tr, typename A>
00285 void doUnpacking(cCommBuffer *buffer, std::map<K,V,Tr,A>& m)
00286 {
00287     int n;
00288     doUnpacking(buffer, n);
00289     for (int i=0; i<n; i++) {
00290         K k; V v;
00291         doUnpacking(buffer, k);
00292         doUnpacking(buffer, v);
00293         m[k] = v;
00294     }
00295 }
00296 
00297 NAMESPACE_END
00298 
00299 #endif
00300 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3