PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // TypeTraits.h 00004 // Copyright (C) 2002 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 00035 00036 /* ******************************************************* 00037 * $Id: TypeTraits.h 9042 2008-05-22 15:42:15Z nouiz $ 00038 * AUTHORS: Pascal Vincent 00039 * This file is part of the PLearn library. 00040 ******************************************************* */ 00041 00042 00045 #ifndef TypeTraits_INC 00046 #define TypeTraits_INC 00047 00048 #include <string> 00049 #include <vector> 00050 #include <list> 00051 #include <map> 00052 #include <queue> 00053 #include <set> 00054 #include <nspr/prlong.h> 00055 #include <plearn/base/pl_stdint.h> 00056 00057 namespace PLearn { 00058 using std::string; 00059 00076 template<class T> 00077 class TypeTraits 00078 { 00079 public: 00081 static inline string name() 00082 { return "UNKNOWN_TYPE_NAME"; } 00083 00086 static inline unsigned char little_endian_typecode() 00087 { return 0xFF; } 00088 00091 static inline unsigned char big_endian_typecode() 00092 { return 0xFF; } 00093 00094 }; 00095 00096 00097 //##### Specializations ##################################################### 00098 00099 template<class T> 00100 class TypeTraits<T*> 00101 { 00102 public: 00103 static inline string name() 00104 { return TypeTraits<T>::name()+"*"; } 00105 00106 static inline unsigned char little_endian_typecode() 00107 { return 0xFF; } 00108 00109 static inline unsigned char big_endian_typecode() 00110 { return 0xFF; } 00111 }; 00112 00113 template<class T> 00114 class TypeTraits<T const> 00115 { 00116 public: 00117 static inline string name() 00118 { return TypeTraits<T>::name()+" const"; } 00119 00120 static inline unsigned char little_endian_typecode() 00121 { return 0xFF; } 00122 00123 static inline unsigned char big_endian_typecode() 00124 { return 0xFF; } 00125 }; 00126 00127 #define DECLARE_TYPE_TRAITS_FOR_BASETYPE(T,LITTLE_ENDIAN_TYPECODE,BIG_ENDIAN_TYPECODE) \ 00128 template<> \ 00129 class TypeTraits<T> \ 00130 { \ 00131 public: \ 00132 static inline string name() \ 00133 { return #T; } \ 00134 \ 00135 static inline unsigned char little_endian_typecode() \ 00136 { return LITTLE_ENDIAN_TYPECODE; } \ 00137 \ 00138 static inline unsigned char big_endian_typecode() \ 00139 { return BIG_ENDIAN_TYPECODE; } \ 00140 } 00141 00142 #define DECLARE_TYPE_TRAITS_FOR_INTTYPE(T) \ 00143 template<> \ 00144 class TypeTraits<T> \ 00145 { \ 00146 public: \ 00147 static inline string name() \ 00148 { return #T; } \ 00149 \ 00150 static inline unsigned char little_endian_typecode() \ 00151 { \ 00152 switch(sizeof(T)) \ 00153 { \ 00154 case sizeof(int8_t): \ 00155 return 0x01; \ 00156 case sizeof(int16_t): \ 00157 return 0x03; \ 00158 case sizeof(int32_t): \ 00159 return 0x07; \ 00160 case sizeof(int64_t): \ 00161 return 0x16; \ 00162 default: \ 00163 return 0xFF; \ 00164 } \ 00165 } \ 00166 \ 00167 static inline unsigned char big_endian_typecode() \ 00168 { \ 00169 switch(sizeof(T)) \ 00170 { \ 00171 case sizeof(int8_t): \ 00172 return 0x01; \ 00173 case sizeof(int16_t): \ 00174 return 0x04; \ 00175 case sizeof(int32_t): \ 00176 return 0x08; \ 00177 case sizeof(int64_t): \ 00178 return 0x17; \ 00179 default: \ 00180 return 0xFF; \ 00181 } \ 00182 } \ 00183 } 00184 00185 #define DECLARE_TYPE_TRAITS_FOR_UINTTYPE(T) \ 00186 template<> \ 00187 class TypeTraits<T> \ 00188 { \ 00189 public: \ 00190 static inline string name() \ 00191 { return #T; } \ 00192 \ 00193 static inline unsigned char little_endian_typecode() \ 00194 { \ 00195 switch(sizeof(T)) \ 00196 { \ 00197 case sizeof(uint8_t): \ 00198 return 0x02; \ 00199 case sizeof(uint16_t): \ 00200 return 0x05; \ 00201 case sizeof(uint32_t): \ 00202 return 0x0B; \ 00203 case sizeof(uint64_t): \ 00204 return 0x18; \ 00205 default: \ 00206 return 0xFF; \ 00207 } \ 00208 } \ 00209 \ 00210 static inline unsigned char big_endian_typecode() \ 00211 { \ 00212 switch(sizeof(T)) \ 00213 { \ 00214 case sizeof(uint8_t): \ 00215 return 0x02; \ 00216 case sizeof(uint16_t): \ 00217 return 0x06; \ 00218 case sizeof(uint32_t): \ 00219 return 0x0C; \ 00220 case sizeof(uint64_t): \ 00221 return 0x19; \ 00222 default: \ 00223 return 0xFF; \ 00224 } \ 00225 } \ 00226 } 00227 00228 #define DECLARE_TYPE_TRAITS(T) \ 00229 template<> \ 00230 class TypeTraits<T> \ 00231 { \ 00232 public: \ 00233 static inline string name() \ 00234 { return #T; } \ 00235 \ 00236 static inline unsigned char little_endian_typecode() \ 00237 { return 0xFF; } \ 00238 \ 00239 static inline unsigned char big_endian_typecode() \ 00240 { return 0xFF; } \ 00241 } 00242 00243 // DECLARE_TYPE_TRAITS_FOR_BASETYPE(bool, ??, ??); 00244 DECLARE_TYPE_TRAITS_FOR_BASETYPE(void, 0xFF, 0xFF); 00245 DECLARE_TYPE_TRAITS_FOR_BASETYPE(float, 0x0E, 0x0F); 00246 DECLARE_TYPE_TRAITS_FOR_BASETYPE(double, 0x10, 0x11); 00247 DECLARE_TYPE_TRAITS_FOR_BASETYPE(bool, 0x30, 0x30); 00248 00249 #ifdef __INTEL_COMPILER 00250 #pragma warning(disable:280) 00251 // Yes, I know that "selector expression is constant" 00252 #endif 00253 DECLARE_TYPE_TRAITS_FOR_INTTYPE(char); 00254 DECLARE_TYPE_TRAITS_FOR_INTTYPE(signed char); 00255 DECLARE_TYPE_TRAITS_FOR_INTTYPE(short); 00256 DECLARE_TYPE_TRAITS_FOR_INTTYPE(int); 00257 DECLARE_TYPE_TRAITS_FOR_INTTYPE(long); 00258 DECLARE_TYPE_TRAITS_FOR_INTTYPE(long long); 00259 00260 DECLARE_TYPE_TRAITS_FOR_UINTTYPE(unsigned char); 00261 DECLARE_TYPE_TRAITS_FOR_UINTTYPE(unsigned short); 00262 DECLARE_TYPE_TRAITS_FOR_UINTTYPE(unsigned int); 00263 DECLARE_TYPE_TRAITS_FOR_UINTTYPE(unsigned long); 00264 DECLARE_TYPE_TRAITS_FOR_UINTTYPE(unsigned long long); 00265 #ifdef __INTEL_COMPILER 00266 #pragma warning(default:280) 00267 #endif 00268 00269 DECLARE_TYPE_TRAITS(string); 00270 00271 template<class T> 00272 class TypeTraits< std::vector<T> > 00273 { 00274 public: 00275 static inline string name() 00276 { return string("vector< ") + TypeTraits<T>::name() + " >"; } 00277 00278 static inline unsigned char little_endian_typecode() 00279 { return 0xFF; } 00280 00281 static inline unsigned char big_endian_typecode() 00282 { return 0xFF; } 00283 }; 00284 00285 template<class T> 00286 class TypeTraits< std::list<T> > 00287 { 00288 public: 00289 static inline string name() 00290 { return string("list< ") + TypeTraits<T>::name() + " >"; } 00291 00292 static inline unsigned char little_endian_typecode() 00293 { return 0xFF; } 00294 00295 static inline unsigned char big_endian_typecode() 00296 { return 0xFF; } 00297 }; 00298 00299 template<class T, class U> 00300 class TypeTraits< std::pair<T,U> > 00301 { 00302 public: 00303 static inline string name() 00304 { 00305 return string("pair< ") + TypeTraits<T>::name()+", " 00306 + TypeTraits<U>::name() + " >"; 00307 } 00308 00309 static inline unsigned char little_endian_typecode() 00310 { return 0xFF; } 00311 00312 static inline unsigned char big_endian_typecode() 00313 { return 0xFF; } 00314 }; 00315 00316 template<class T, class U> 00317 class TypeTraits< std::map<T,U> > 00318 { 00319 public: 00320 static inline string name() 00321 { 00322 return string("map< ") + TypeTraits<T>::name()+", " 00323 + TypeTraits<U>::name() + " >"; 00324 } 00325 00326 static inline unsigned char little_endian_typecode() 00327 { return 0xFF; } 00328 00329 static inline unsigned char big_endian_typecode() 00330 { return 0xFF; } 00331 }; 00332 00333 template<class T> 00334 class TypeTraits< std::set<T> > 00335 { 00336 public: 00337 static inline string name() 00338 { return string("set< ") + TypeTraits<T>::name() + " >"; } 00339 00340 static inline unsigned char little_endian_typecode() 00341 { return 0xFF; } 00342 00343 static inline unsigned char big_endian_typecode() 00344 { return 0xFF; } 00345 }; 00346 00347 template<class T> 00348 class TypeTraits< std::priority_queue<T> > 00349 { 00350 public: 00351 static inline string name() 00352 { return string("priority_queue< ") + TypeTraits<T>::name() + " >"; } 00353 00354 static inline unsigned char little_endian_typecode() 00355 { return 0xFF; } 00356 00357 static inline unsigned char big_endian_typecode() 00358 { return 0xFF; } 00359 }; 00360 00361 } // end of namespace PLearn 00362 00363 00364 #endif 00365 00366 00367 /* 00368 Local Variables: 00369 mode:c++ 00370 c-basic-offset:4 00371 c-file-style:"stroustrup" 00372 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00373 indent-tabs-mode:nil 00374 fill-column:79 00375 End: 00376 */ 00377 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :