object.h

Go to the documentation of this file.
00001 // -*- C++ -*-
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 NIL_ID;
00096     typedef UINT ver_t;
00097     typedef std::vector<ver_t> ver_list;
00108     virtual bool serialize (std::ostream&, ver_list&) const;
00121     virtual bool unserialize (std::istream&, ver_list&, const id_t& = NIL_ID)
00122     { return true; }
00123 };
00124 
00125 inline std::ostream& operator<< (std::ostream& os, const Object& obj) {
00126     Object::ver_list vl_empty;
00127     obj.serialize(os, vl_empty);
00128     return os;
00129 }
00130 
00131 typedef Object* (*_creator_t) ();
00132 
00141 struct _register_creator {
00142     _register_creator(const Object::id_t&, _creator_t);
00143 };
00144 
00146 #define REGISTER_CREATOR2(cls,p)                             \
00147     static const Object::id_t p##_id_ = #cls;                \
00148     static Object* p##_c_ () { return new cls; }             \
00149     const Object::id_t& cls::id () const { return p##_id_; } \
00150     static const _register_creator p##_(p##_id_, p##_c_)
00151 #define REGISTER_CREATOR(cls) REGISTER_CREATOR2(cls,)
00152 
00158 #define SERIALIZE_PARENT(pcls,os,vl,def_ver) \
00159     vl.push_back(def_ver); pcls::serialize(os, vl)
00160 
00166 #define UNSERIALIZE_PARENT(pcls,is,vl,def_ver,ver) \
00167     if (!pcls::unserialize(is, vl)) return false; \
00168     const ver_t ver = vl.back(); \
00169     if (ver > def_ver) { \
00170         std::cerr << this->id() \
00171                   << ": unserialize: format newer than the program\n"; \
00172         return false; \
00173     } \
00174     if (ver > 0) vl.pop_back()
00175 
00176 #endif // __cplusplus
00177 
00178 #ifdef  __OBJECT_H__
00179 #warning "This header file may conflict with another `object.h' file."
00180 #endif
00181 #define __OBJECT_H__
00182 #endif

Generated on Wed Nov 8 08:15:22 2006 for LEMGA by  doxygen 1.4.6