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 #include <string>
00034 #include <istream>
00035 #include <iostream>
00036 #include "Serializable.h"
00037 #include "mstring.h"
00038 #include "boost/lexical_cast.hpp"
00039
00040 #ifndef _MACE_TRAITS_H
00041 #define _MACE_TRAITS_H
00042
00048 namespace mace {
00049
00050
00051 class MaceTime;
00052
00054 template<class T>
00055 class KeyTraits {
00056 public:
00058 static bool isString() {
00059 return false;
00060 }
00062 static bool isInt() {
00063 return false;
00064 }
00066 static bool isDeterministic() {
00067 return true;
00068 }
00070 static void append(std::string& str, const T& key) {
00071 }
00073 static T extract(std::istream& in) throw(SerializationException) {
00074 throw SerializationException("Cannot extract unsupported type");
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084 };
00085
00086 template<>
00087 class KeyTraits<std::string> {
00088 public:
00089 static bool isString() {
00090 return true;
00091 }
00092 static bool isInt() {
00093 return false;
00094 }
00095 static bool isDeterministic() {
00096 return true;
00097 }
00098 static void append(std::string& str, const std::string& key) {
00099 str.append(key);
00100 }
00101 static std::string extract(std::istream& in) throw(SerializationException) {
00102 return SerializationUtil::get(in, '<');
00103 }
00104 };
00105
00106
00107
00108
00109
00110 template<>
00111 class KeyTraits<long long> {
00112 public:
00113 static bool isString() {
00114 return false;
00115 }
00116 static bool isInt() {
00117 return true;
00118 }
00119 static bool isDeterministic() {
00120 return true;
00121 }
00122 static void append(std::string& str, const long long& key) {
00123 char buf[30];
00124
00125 sprintf(buf, "%lld", key);
00126 str += buf;
00127 }
00128 static unsigned long long extract(std::istream& in)
00129 throw(SerializationException) {
00130 std::string token = SerializationUtil::get(in, '<');
00131 return boost::lexical_cast<unsigned long long>(token);
00132 }
00133 };
00134
00135 template<>
00136 class KeyTraits<unsigned long long> : public KeyTraits<long long> {
00137 public:
00138 static void append(std::string& str, const unsigned long long& key) {
00139 char buf[30];
00140
00141 sprintf(buf, "%llu", key);
00142 str += buf;
00143 }
00144 };
00145
00146 template<>
00147 class KeyTraits<int> : public KeyTraits<long long> {
00148 };
00149
00150 template<>
00151 class KeyTraits<unsigned> : public KeyTraits<unsigned long long> {
00152 };
00153
00154 template<>
00155 class KeyTraits<short> : public KeyTraits<long long> {
00156 };
00157
00158 template<>
00159 class KeyTraits<unsigned short> : public KeyTraits<unsigned long long> {
00160 };
00161
00162 template<>
00163 class KeyTraits<char> : public KeyTraits<long long> {
00164 };
00165
00166 template<>
00167 class KeyTraits<unsigned char> : public KeyTraits<unsigned long long> {
00168 };
00169
00170
00171 }
00172 #endif // _TRAITS_H