Clone.hh

00001 //********************************************************************
00002 //*** util/Clone.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: Clone.hh,v 1.10 2009-03-02 14:16:47 mws Exp $
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 // according to C++ Coding Standards, Herb Sutter, Andrei Alexandrescu 2005
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

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3