PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // tuple_io.h 00004 // Copyright (C) 2006 Pascal Vincent 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 #ifndef pl_tuple_INC 00035 #define pl_tuple_INC 00036 00037 #include <boost/tuple/tuple.hpp> 00038 #include <boost/tuple/tuple_comparison.hpp> 00039 #include <plearn/base/TypeTraits.h> 00040 #include <plearn/io/PStream.h> 00041 00042 namespace PLearn { 00043 00044 // ************************************* 00045 // inject boost tuples facility in PLearn namespace 00046 00047 using boost::tuples::tuple; 00048 using boost::tuples::make_tuple; 00049 using boost::tuples::tie; 00050 using boost::tuples::get; 00051 00052 // *********************************************************** 00053 // define correpsonding TypeTraits for tuples up to 6 elements 00054 00055 template<class T1> 00056 class TypeTraits< tuple<T1> > 00057 { 00058 public: 00059 static inline string name() 00060 { return string("tuple< ") + TypeTraits<T1>::name()+" >"; } 00061 00062 static inline unsigned char little_endian_typecode() 00063 { return 0xFF; } 00064 00065 static inline unsigned char big_endian_typecode() 00066 { return 0xFF; } 00067 }; 00068 00069 template<class T1, class T2> 00070 class TypeTraits< tuple<T1,T2> > 00071 { 00072 public: 00073 static inline string name() 00074 { 00075 return string("tuple< ") 00076 + TypeTraits<T1>::name() + ", " 00077 + TypeTraits<T2>::name() + " >"; 00078 } 00079 00080 static inline unsigned char little_endian_typecode() 00081 { return 0xFF; } 00082 00083 static inline unsigned char big_endian_typecode() 00084 { return 0xFF; } 00085 }; 00086 00087 template<class T1, class T2, class T3> 00088 class TypeTraits< tuple<T1,T2,T3> > 00089 { 00090 public: 00091 static inline string name() 00092 { 00093 return string("tuple< ") 00094 + TypeTraits<T1>::name() + ", " 00095 + TypeTraits<T2>::name() + ", " 00096 + TypeTraits<T3>::name() + " >"; 00097 } 00098 00099 static inline unsigned char little_endian_typecode() 00100 { return 0xFF; } 00101 00102 static inline unsigned char big_endian_typecode() 00103 { return 0xFF; } 00104 }; 00105 00106 template<class T1, class T2, class T3, class T4> 00107 class TypeTraits< tuple<T1,T2,T3,T4> > 00108 { 00109 public: 00110 static inline string name() 00111 { 00112 return string("tuple< ") 00113 + TypeTraits<T1>::name() + ", " 00114 + TypeTraits<T2>::name() + ", " 00115 + TypeTraits<T3>::name() + ", " 00116 + TypeTraits<T4>::name() + " >"; 00117 } 00118 00119 static inline unsigned char little_endian_typecode() 00120 { return 0xFF; } 00121 00122 static inline unsigned char big_endian_typecode() 00123 { return 0xFF; } 00124 }; 00125 00126 template<class T1, class T2, class T3, class T4, class T5> 00127 class TypeTraits< tuple<T1,T2,T3,T4,T5> > 00128 { 00129 public: 00130 static inline string name() 00131 { 00132 return string("tuple< ") 00133 + TypeTraits<T1>::name() + ", " 00134 + TypeTraits<T2>::name() + ", " 00135 + TypeTraits<T3>::name() + ", " 00136 + TypeTraits<T4>::name() + ", " 00137 + TypeTraits<T5>::name() + " >"; 00138 } 00139 00140 static inline unsigned char little_endian_typecode() 00141 { return 0xFF; } 00142 00143 static inline unsigned char big_endian_typecode() 00144 { return 0xFF; } 00145 }; 00146 00147 template<class T1, class T2, class T3, class T4, class T5, class T6> 00148 class TypeTraits< tuple<T1,T2,T3,T4,T5,T6> > 00149 { 00150 public: 00151 static inline string name() 00152 { 00153 return string("tuple< ") 00154 + TypeTraits<T1>::name() + ", " 00155 + TypeTraits<T2>::name() + ", " 00156 + TypeTraits<T3>::name() + ", " 00157 + TypeTraits<T4>::name() + ", " 00158 + TypeTraits<T5>::name() + ", " 00159 + TypeTraits<T6>::name() + " >"; 00160 } 00161 00162 static inline unsigned char little_endian_typecode() 00163 { return 0xFF; } 00164 00165 static inline unsigned char big_endian_typecode() 00166 { return 0xFF; } 00167 }; 00168 00169 00170 // ************************************************************** 00171 // define correpsonding serialization for tuples up to 6 elements 00172 00173 00174 template<class T1> 00175 PStream& operator<<(PStream& out, const tuple<T1>& t) 00176 { 00177 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00178 out.put('('); 00179 out << get<0>(t); 00180 out.put(')'); 00181 out.setOutMode(oldmode); 00182 return out; 00183 } 00184 00185 template<class T1, class T2> 00186 PStream& operator<<(PStream& out, const tuple<T1,T2>& t) 00187 { 00188 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00189 out.put('('); 00190 out << get<0>(t); out.write(", "); 00191 out << get<1>(t); 00192 out.put(')'); 00193 out.setOutMode(oldmode); 00194 return out; 00195 } 00196 00197 template<class T1, class T2, class T3> 00198 PStream& operator<<(PStream& out, const tuple<T1,T2,T3>& t) 00199 { 00200 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00201 out.put('('); 00202 out << get<0>(t); out.write(", "); 00203 out << get<1>(t); out.write(", "); 00204 out << get<2>(t); 00205 out.put(')'); 00206 out.setOutMode(oldmode); 00207 return out; 00208 } 00209 00210 template<class T1, class T2, class T3, class T4> 00211 PStream& operator<<(PStream& out, const tuple<T1,T2,T3,T4>& t) 00212 { 00213 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00214 out.put('('); 00215 out << get<0>(t); out.write(", "); 00216 out << get<1>(t); out.write(", "); 00217 out << get<2>(t); out.write(", "); 00218 out << get<3>(t); 00219 out.put(')'); 00220 out.setOutMode(oldmode); 00221 return out; 00222 } 00223 00224 template<class T1, class T2, class T3, class T4, class T5> 00225 PStream& operator<<(PStream& out, const tuple<T1,T2,T3,T4,T5>& t) 00226 { 00227 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00228 out.put('('); 00229 out << get<0>(t); out.write(", "); 00230 out << get<1>(t); out.write(", "); 00231 out << get<2>(t); out.write(", "); 00232 out << get<3>(t); out.write(", "); 00233 out << get<4>(t); 00234 out.put(')'); 00235 out.setOutMode(oldmode); 00236 return out; 00237 } 00238 00239 template<class T1, class T2, class T3, class T4, class T5, class T6> 00240 PStream& operator<<(PStream& out, const tuple<T1,T2,T3,T4,T5,T6>& t) 00241 { 00242 PStream::mode_t oldmode = out.switchToPLearnOutMode(); 00243 out.put('('); 00244 out << get<0>(t); out.write(", "); 00245 out << get<1>(t); out.write(", "); 00246 out << get<2>(t); out.write(", "); 00247 out << get<3>(t); out.write(", "); 00248 out << get<4>(t); out.write(", "); 00249 out << get<5>(t); 00250 out.put(')'); 00251 out.setOutMode(oldmode); 00252 return out; 00253 } 00254 00255 00256 // ************************************************************** 00257 // define correpsonding de-serialization for tuples up to 6 elements 00258 00259 template<class T1> 00260 PStream& operator>>(PStream& in, tuple<T1>& t) 00261 { 00262 in.skipBlanksAndComments(); in.readExpected('('); 00263 in.skipBlanksAndComments(); in >> get<0>(t); 00264 in.skipBlanksAndComments(); in.readExpected(')'); 00265 return in; 00266 } 00267 00268 template<class T1, class T2> 00269 PStream& operator>>(PStream& in, tuple<T1,T2>& t) 00270 { 00271 in.skipBlanksAndComments(); in.readExpected('('); 00272 in.skipBlanksAndComments(); in >> get<0>(t); 00273 in.skipBlanksAndCommentsAndSeparators(); in >> get<1>(t); 00274 in.skipBlanksAndComments(); in.readExpected(')'); 00275 return in; 00276 } 00277 00278 template<class T1, class T2, class T3> 00279 PStream& operator>>(PStream& in, tuple<T1,T2,T3>& t) 00280 { 00281 in.skipBlanksAndComments(); in.readExpected('('); 00282 in.skipBlanksAndComments(); in >> get<0>(t); 00283 in.skipBlanksAndCommentsAndSeparators(); in >> get<1>(t); 00284 in.skipBlanksAndCommentsAndSeparators(); in >> get<2>(t); 00285 in.skipBlanksAndComments(); in.readExpected(')'); 00286 return in; 00287 } 00288 00289 template<class T1, class T2, class T3, class T4> 00290 PStream& operator>>(PStream& in, tuple<T1,T2,T3,T4>& t) 00291 { 00292 in.skipBlanksAndComments(); in.readExpected('('); 00293 in.skipBlanksAndComments(); in >> get<0>(t); 00294 in.skipBlanksAndCommentsAndSeparators(); in >> get<1>(t); 00295 in.skipBlanksAndCommentsAndSeparators(); in >> get<2>(t); 00296 in.skipBlanksAndCommentsAndSeparators(); in >> get<3>(t); 00297 in.skipBlanksAndComments(); in.readExpected(')'); 00298 return in; 00299 } 00300 00301 template<class T1, class T2, class T3, class T4, class T5> 00302 PStream& operator>>(PStream& in, tuple<T1,T2,T3,T4,T5>& t) 00303 { 00304 in.skipBlanksAndComments(); in.readExpected('('); 00305 in.skipBlanksAndComments(); in >> get<0>(t); 00306 in.skipBlanksAndCommentsAndSeparators(); in >> get<1>(t); 00307 in.skipBlanksAndCommentsAndSeparators(); in >> get<2>(t); 00308 in.skipBlanksAndCommentsAndSeparators(); in >> get<3>(t); 00309 in.skipBlanksAndCommentsAndSeparators(); in >> get<4>(t); 00310 in.skipBlanksAndComments(); in.readExpected(')'); 00311 return in; 00312 } 00313 00314 template<class T1, class T2, class T3, class T4, class T5, class T6> 00315 PStream& operator>>(PStream& in, tuple<T1,T2,T3,T4,T5,T6>& t) 00316 { 00317 in.skipBlanksAndComments(); in.readExpected('('); 00318 in.skipBlanksAndComments(); in >> get<0>(t); 00319 in.skipBlanksAndCommentsAndSeparators(); in >> get<1>(t); 00320 in.skipBlanksAndCommentsAndSeparators(); in >> get<2>(t); 00321 in.skipBlanksAndCommentsAndSeparators(); in >> get<3>(t); 00322 in.skipBlanksAndCommentsAndSeparators(); in >> get<4>(t); 00323 in.skipBlanksAndCommentsAndSeparators(); in >> get<5>(t); 00324 in.skipBlanksAndComments(); in.readExpected(')'); 00325 return in; 00326 } 00327 00328 00330 template<typename T0> 00331 inline void deepCopyField(tuple<T0>& t, CopiesMap& copies) 00332 { 00333 deepCopyField(get<0>(t), copies); 00334 } 00335 template<typename T0, typename T1> 00336 inline void deepCopyField(tuple<T0,T1>& t, CopiesMap& copies) 00337 { 00338 deepCopyField(get<0>(t), copies); 00339 deepCopyField(get<1>(t), copies); 00340 } 00341 template<typename T0, typename T1, typename T2> 00342 inline void deepCopyField(tuple<T0,T1,T2>& t, CopiesMap& copies) 00343 { 00344 deepCopyField(get<0>(t), copies); 00345 deepCopyField(get<1>(t), copies); 00346 deepCopyField(get<2>(t), copies); 00347 } 00348 template<typename T0, typename T1, typename T2, typename T3> 00349 inline void deepCopyField(tuple<T0,T1,T2,T3>& t, CopiesMap& copies) 00350 { 00351 deepCopyField(get<0>(t), copies); 00352 deepCopyField(get<1>(t), copies); 00353 deepCopyField(get<2>(t), copies); 00354 deepCopyField(get<3>(t), copies); 00355 } 00356 template<typename T0, typename T1, typename T2, typename T3, typename T4> 00357 inline void deepCopyField(tuple<T0,T1,T2,T3,T4>& t, CopiesMap& copies) 00358 { 00359 deepCopyField(get<0>(t), copies); 00360 deepCopyField(get<1>(t), copies); 00361 deepCopyField(get<2>(t), copies); 00362 deepCopyField(get<3>(t), copies); 00363 deepCopyField(get<4>(t), copies); 00364 } 00365 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5> 00366 inline void deepCopyField(tuple<T0,T1,T2,T3,T4,T5>& t, CopiesMap& copies) 00367 { 00368 deepCopyField(get<0>(t), copies); 00369 deepCopyField(get<1>(t), copies); 00370 deepCopyField(get<2>(t), copies); 00371 deepCopyField(get<3>(t), copies); 00372 deepCopyField(get<4>(t), copies); 00373 deepCopyField(get<5>(t), copies); 00374 } 00375 00376 00377 } // namespace PLearn 00378 00379 #endif //ndef pl_tuple_INC 00380 00381 00382 /* 00383 Local Variables: 00384 mode:c++ 00385 c-basic-offset:4 00386 c-file-style:"stroustrup" 00387 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00388 indent-tabs-mode:nil 00389 fill-column:79 00390 End: 00391 */ 00392 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :