00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
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
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
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 }
00201
00202
00203
#endif