00001
00002 #ifndef __COMMON_TYPES_OBJECT_H__
00003 #define __COMMON_TYPES_OBJECT_H__
00004
00011 #ifdef size_t
00012 typedef size_t UINT;
00013 #else
00014 typedef unsigned long UINT;
00015 #endif
00016
00017 typedef double REAL;
00018
00019 #define INFINITESIMAL (8e-16)
00020 #define EPSILON (1e-9)
00021 #undef INFINITY
00022
00023 #define INFINITY (5e30)
00024
00025 #ifdef __cplusplus
00026
00027 #include <stdio.h>
00028 #include <iostream>
00029 #include <string>
00030 #include <vector>
00031
00053 #define OBJ_FUNC_UNDEFINED(fun) \
00054 std::cerr<<__FILE__":"<<__LINE__<<": "<<id()<<"::" fun " undefined.\n"; \
00055 exit(-99);
00056
00062 class Object {
00063 friend std::istream& operator>> (std::istream&, Object&);
00064 friend std::ostream& operator<< (std::ostream&, const Object&);
00065
00066 public:
00067 virtual ~Object () {}
00068
00069 typedef std::string id_t;
00071 virtual const id_t& id () const = 0;
00072
00078 virtual Object* create () const = 0;
00079
00081 static Object* create (std::istream&);
00082
00092 virtual Object* clone () const = 0;
00093
00094 protected:
00095 static const id_t empty_id;
00096 typedef UINT ver_t;
00097 typedef std::vector<ver_t> ver_list;
00099 virtual bool serialize (std::ostream&, ver_list&) const;
00101 virtual bool unserialize (std::istream&, ver_list&,
00102 const id_t& = empty_id) { return true; }
00103 };
00104
00105 inline std::ostream& operator<< (std::ostream& os, const Object& obj) {
00106 Object::ver_list vl_empty;
00107 obj.serialize(os, vl_empty);
00108 return os;
00109 }
00110
00111 typedef Object* (*_creator_t) ();
00112
00121 struct _register_creator {
00122 _register_creator(const Object::id_t&, _creator_t);
00123 };
00124
00125 #define REGISTER_CREATOR(cls) \
00126 static const Object::id_t _id_ = #cls; \
00127 static Object* _c_ () { return new cls; } \
00128 const Object::id_t& cls::id () const { return ::_id_; } \
00129 static const _register_creator _(_id_, _c_)
00130
00136 #define SERIALIZE_PARENT(pcls,os,vl,def_ver) \
00137 vl.push_back(def_ver); pcls::serialize(os, vl)
00138
00144 #define UNSERIALIZE_PARENT(pcls,is,vl,def_ver,ver) \
00145 if (!pcls::unserialize(is, vl)) return false; \
00146 const ver_t ver = vl.back(); \
00147 if (ver > def_ver) { \
00148 std::cerr << this->id() \
00149 << ": unserialize: format newer than the program\n"; \
00150 return false; \
00151 } \
00152 if (ver > 0) vl.pop_back()
00153
00154 #endif // __cplusplus
00155
00156 #ifdef __OBJECT_H__
00157 #warning "This header file may conflict with another `object.h' file."
00158 #endif
00159 #define __OBJECT_H__
00160 #endif