00001 // Copyright (C) 1999 Jean-Marc Valin & Dominic Letourneau 00002 00003 #ifndef _NETTYPES_H_ 00004 #define _NETTYPES_H_ 00005 00006 00007 #include "Object.h" 00008 #include <iostream> 00009 #include <string> 00010 #include <fstream> 00011 #include <stdio.h> 00012 #include "ObjectPool.h" 00013 #include "ObjectParser.h" 00014 #include "typetraits.h" 00015 //temporarily inserting include of Stream.h 00016 #include "Stream.h" 00017 00018 namespace FD { 00019 00022 template <class T> 00023 class GenericType : public Object { 00024 00025 protected: 00026 00028 T value; 00029 00030 public: 00031 00033 typedef T basicType; 00034 00036 T &val() {return value;} 00037 00039 GenericType() { 00040 //value = (T) 0; 00041 } 00042 00044 virtual ~GenericType() {;} 00045 00047 GenericType (T val) { 00048 value = val; 00049 } 00050 00052 GenericType (GenericType<T> ©) { 00053 value = copy.value; 00054 } 00055 00056 //static GenericType<T> *alloc() {return ObjectPool<GenericType<T> >::alloc();} 00057 00063 virtual void printOn(std::ostream &out=std::cout) const { 00064 out << "<Generic Type: " << typeid(T).name() << ">" << std::endl; 00065 } 00066 00067 }; 00068 00073 template <class T> 00074 class PrintableGenericType : public GenericType<T> { 00075 00076 public: 00077 00079 PrintableGenericType () {} 00080 00085 PrintableGenericType (T val) { 00086 GenericType<T>::value = val; 00087 } 00088 00090 PrintableGenericType (PrintableGenericType<T> ©) { 00091 GenericType<T>::value = copy.value; 00092 } 00093 00098 T &val() {return GenericType<T>::value;} 00099 00105 void printOn(std::ostream &out) const 00106 { 00107 out << "<" << this->className() << " " << this->value << " >"; 00108 } 00109 00114 virtual void prettyPrint(std::ostream &out=std::cout) const { 00115 out << this->value << " "; 00116 } 00117 00123 void readFrom(std::istream &in) 00124 { 00125 in >> this->value; 00126 char ch; 00127 in >> ch; 00128 if (ch != '>') 00129 throw new GeneralException("Error reading String: '>' expected", __FILE__, __LINE__); 00130 } 00131 00137 void serialize(std::ostream &out) const 00138 { 00139 out << "{" << this->className() << " |" << this->value << " }"; 00140 } 00141 00147 void unserialize(std::istream &in) 00148 { 00149 in >> this->value; 00150 char ch; 00151 in >> ch; 00152 if (ch != '}') 00153 throw new GeneralException("Error reading String: '}' expected", __FILE__, __LINE__); 00154 } 00155 00156 //static PrintableGenericType<T> *alloc() {return ObjectPool<PrintableGenericType<T> >::alloc();} 00157 }; 00158 00164 template <class T> 00165 class NetCType : public PrintableGenericType<T> { 00166 00167 public: 00168 00170 NetCType() { 00171 //casting into the type 00172 GenericType<T>::value = (T) 0; 00173 } 00174 00176 NetCType(T val) { 00177 GenericType<T>::value = val; 00178 } 00179 00181 virtual ~NetCType() {;} 00182 00184 operator T() { 00185 return GenericType<T>::value; 00186 } 00187 00189 NetCType<T>& operator= (NetCType<T> &type) { 00190 GenericType<T>::value = type.value; 00191 return *this; 00192 } 00193 00195 NetCType<T>& operator= (T val) { 00196 GenericType<T>::value = val; 00197 return *this; 00198 } 00199 00201 int operator== (NetCType<T> &type) { 00202 return (type.value == GenericType<T>::value); 00203 } 00204 00206 int operator== (T val) { 00207 return (GenericType<T>::value == val); 00208 } 00209 00211 int operator!= (NetCType<T> &type) { 00212 return (type.value != GenericType<T>::value); 00213 } 00214 00216 int operator!= (T val) { 00217 return (GenericType<T>::value != val); 00218 } 00219 00224 static NetCType<T> *alloc() {return ObjectPool<NetCType<T> >::alloc();} 00225 00230 static NetCType<T> *alloc(const T &obj) 00231 { 00232 NetCType<T> *ret = ObjectPool<NetCType<T> >::alloc(); 00233 ret->value = obj; 00234 return ret; 00235 } 00236 00238 void destroy() {ObjectPool<NetCType<T> >::release(this);} 00239 00240 }; 00241 00243 typedef NetCType<char> Char; 00244 00246 typedef NetCType<int> Int; 00247 00249 typedef NetCType<float> Float; 00250 00252 typedef NetCType<double> Double; 00253 00255 typedef NetCType<bool> Bool; 00256 00257 //typedef NetCType<FILE *> FILEPTR; 00259 class FILEPTR : public GenericType<FILE *> { 00260 public: 00261 FILEPTR(FILE *file); 00262 FILEPTR(const std::string &filename, const std::string &mode); 00263 ~FILEPTR(); 00264 }; 00265 00266 #ifndef WIN32 00267 00268 //typedef NetCType<FILE *> FILEPTR; 00270 class FILEDES : public GenericType<int> { 00271 public: 00272 FILEDES(int fd); 00273 FILEDES(const std::string &filename, int mode); 00274 ~FILEDES(); 00275 }; 00276 00277 #endif /*ifdef WIN32*/ 00278 00280 extern ObjectRef TrueObject; 00281 00283 extern ObjectRef FalseObject; 00284 00285 00289 class String : public std::string, public Object 00290 { 00291 public: 00292 00294 typedef std::string basicType; 00295 00297 String() : std::string() {} 00298 00304 void printOn(std::ostream &out) const; 00305 00311 void readFrom(std::istream &in); 00312 00318 void serialize(std::ostream &out) const; 00319 00325 void unserialize(std::istream &in); 00326 00331 void prettyPrint(std::ostream &out) const; 00332 00334 String(const char *str) : std::string(str) 00335 {} 00336 00338 String(const std::string &str) : std::string(str) 00339 {} 00340 00342 const std::string& val() { return *this;} 00343 }; 00344 00346 std::istream &operator >> (std::istream &in, String &str); 00347 00348 _DEF_OBJECT_TYPE(String) 00349 00350 }//namespace FD 00351 #endif