Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

TypeTraits.h

Go to the documentation of this file.
00001 // -*- C++ -*-4 1999/10/29 20:41:34 dugas 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,v 1.7 2004/05/14 17:49:10 chrish42 Exp $ 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 00053 namespace PLearn { 00054 using namespace std; 00055 00065 // The following template defines the TypeTraits for a generic unknown type 00066 00067 template<class T> 00068 class TypeTraits 00069 { 00070 public: 00071 static inline string name() 00072 { return "UNKNOWN_TYPE_NAME"; } 00073 00074 static inline unsigned char little_endian_typecode() 00075 { return 0xFF; } 00076 00077 static inline unsigned char big_endian_typecode() 00078 { return 0xFF; } 00079 00080 }; 00081 00082 // Pointer type 00083 00084 template<class T> 00085 class TypeTraits<T*> 00086 { 00087 public: 00088 static inline string name() 00089 { return TypeTraits<T>::name()+"*"; } 00090 00091 static inline unsigned char little_endian_typecode() 00092 { return 0xFF; } 00093 00094 static inline unsigned char big_endian_typecode() 00095 { return 0xFF; } 00096 }; 00097 00098 #define DECLARE_TYPE_TRAITS_FOR_BASETYPE(T,LITTLE_ENDIAN_TYPECODE,BIG_ENDIAN_TYPECODE) \ 00099 template<> \ 00100 class TypeTraits<T> \ 00101 { \ 00102 public: \ 00103 static inline string name() \ 00104 { return #T; } \ 00105 \ 00106 static inline unsigned char little_endian_typecode() \ 00107 { return LITTLE_ENDIAN_TYPECODE; } \ 00108 \ 00109 static inline unsigned char big_endian_typecode() \ 00110 { return BIG_ENDIAN_TYPECODE; } \ 00111 } 00112 00113 #define DECLARE_TYPE_TRAITS(T) \ 00114 template<> \ 00115 class TypeTraits<T> \ 00116 { \ 00117 public: \ 00118 static inline string name() \ 00119 { return #T; } \ 00120 \ 00121 static inline unsigned char little_endian_typecode() \ 00122 { return 0xFF; } \ 00123 \ 00124 static inline unsigned char big_endian_typecode() \ 00125 { return 0xFF; } \ 00126 } 00127 00128 // DECLARE_TYPE_TRAITS_FOR_BASETYPE(bool, ??, ??); 00129 DECLARE_TYPE_TRAITS_FOR_BASETYPE(char, 0x01, 0x01); 00130 DECLARE_TYPE_TRAITS_FOR_BASETYPE(signed char, 0x01, 0x01); 00131 DECLARE_TYPE_TRAITS_FOR_BASETYPE(unsigned char, 0x02, 0x02); 00132 DECLARE_TYPE_TRAITS_FOR_BASETYPE(short, 0x03, 0x04); 00133 DECLARE_TYPE_TRAITS_FOR_BASETYPE(unsigned short, 0x05, 0x06); 00134 DECLARE_TYPE_TRAITS_FOR_BASETYPE(int, 0x07, 0x08); 00135 DECLARE_TYPE_TRAITS_FOR_BASETYPE(unsigned int, 0x0B, 0x0C); 00136 DECLARE_TYPE_TRAITS_FOR_BASETYPE(long, 0x07, 0x08); 00137 DECLARE_TYPE_TRAITS_FOR_BASETYPE(unsigned long, 0x0B, 0x0C); 00138 DECLARE_TYPE_TRAITS_FOR_BASETYPE(float, 0x0E, 0x0F); 00139 DECLARE_TYPE_TRAITS_FOR_BASETYPE(double, 0x10, 0x11); 00140 DECLARE_TYPE_TRAITS_FOR_BASETYPE(bool, 0x12, 0x12); 00141 00142 DECLARE_TYPE_TRAITS(string); 00143 00144 template<class T> 00145 class TypeTraits< vector<T> > 00146 { 00147 public: 00148 static inline string name() 00149 { return string("vector< ") + TypeTraits<T>::name()+" >"; } 00150 00151 static inline unsigned char little_endian_typecode() 00152 { return 0xFF; } 00153 00154 static inline unsigned char big_endian_typecode() 00155 { return 0xFF; } 00156 }; 00157 00158 template<class T> 00159 class TypeTraits< list<T> > 00160 { 00161 public: 00162 static inline string name() 00163 { return string("list< ") + TypeTraits<T>::name()+" >"; } 00164 00165 static inline unsigned char little_endian_typecode() 00166 { return 0xFF; } 00167 00168 static inline unsigned char big_endian_typecode() 00169 { return 0xFF; } 00170 }; 00171 00172 template<class T, class U> 00173 class TypeTraits< pair<T,U> > 00174 { 00175 public: 00176 static inline string name() 00177 { return string("pair< ") + TypeTraits<T>::name()+", " + TypeTraits<U>::name()+" >"; } 00178 00179 static inline unsigned char little_endian_typecode() 00180 { return 0xFF; } 00181 00182 static inline unsigned char big_endian_typecode() 00183 { return 0xFF; } 00184 }; 00185 00186 template<class T, class U> 00187 class TypeTraits< map<T,U> > 00188 { 00189 public: 00190 static inline string name() 00191 { return string("map< ") + TypeTraits<T>::name()+", " + TypeTraits<U>::name()+" >"; } 00192 00193 static inline unsigned char little_endian_typecode() 00194 { return 0xFF; } 00195 00196 static inline unsigned char big_endian_typecode() 00197 { return 0xFF; } 00198 }; 00199 00200 } // end of namespace PLearn 00201 00202 00203 #endif

Generated on Tue Aug 17 16:09:14 2004 for PLearn by doxygen 1.3.7