00001
00002
00003
00004
00005
00006
00007 #ifndef UTIL_CLONE_HH
00008 #define UTIL_CLONE_HH
00009
00010 #include <typeinfo>
00011
00012 #include "Exceptions/Exception.hh"
00013
00014 namespace fatalmind {
00015
00016 class Clone;
00017
00018 class CloneException: public Exception {
00019 public:
00020 CloneException(const Clone* const t1);
00021 private:
00022 static std::string getCloneErrorMessage(const Clone* const t1);
00023 };
00024
00025
00026 class Clone {
00027 public:
00028 Clone* clone() const {
00029 return clone<Clone>();
00030 }
00031
00032 template<class Derived>
00033 Derived* clone() const {
00034 Clone* c = DoClone();
00035 if (typeid(*c) != typeid(*this)) {
00036 throw CloneException(this);
00037 }
00038 Derived& d = dynamic_cast<Derived&>(*c);
00039 return &d;
00040 }
00041 protected:
00042 virtual Clone* DoClone() const = 0;
00043 virtual ~Clone() {
00044 }
00045 };
00046
00047 }
00048
00049 #endif