Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Fallible.h

Go to the documentation of this file.
00001 //=================================================
00002 //
00003 //
00004 //
00005 // Copier: Peter Sherwood
00006 // See Barton+Nackman p 158
00007 //
00008 //
00009 //===================================================
00010 #ifndef ATLFAST_FALLIBLE_H
00011 #define ATLFAST_FALLIBLE_H
00012 #include<string>
00013 
00014 namespace Atlfast{
00020   class InvalidStateErr{
00021   public:
00022     InvalidStateErr(){}
00023     InvalidStateErr(::std::string s): m_string(s){}
00024     void print(){cout<<m_string<<endl;}
00025   private:
00026     std::string m_string;
00027   };
00028 
00029   template<class T> class Fallible {
00030   public:
00031     Fallible(const T& t):is_valid(true), instance(t){}   //instance exists
00032     Fallible():is_valid(false){}                 //instance does not exist
00033     bool failed() const{ return !is_valid;}      //return true is no instance
00034     bool valid()  const{ return is_valid;}       //return true instance exists
00035     void invalidate()  { is_valid = false;}      //set invalid
00036     //    operator T() const{
00037     //  if(failed()) {::std::string s("Invalid State"); throw s;}             
00038     //  return instance;
00039     //}                                             //conversion to instance type
00040     operator T() const{
00041       if(failed()) {
00042         ::std::string s("Fallible in Invalid State");
00043         //      throw InvalidStateErr(s);
00044         //      throw InvalidStateErr(s);
00045         throw InvalidStateErr();
00046       }             
00047       return instance;
00048     }                                             //conversion to instance type
00049   private:
00050     bool is_valid;
00051     T instance;
00052   };
00053     
00054 }
00055 #endif
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 

Generated on Mon Feb 4 15:54:23 2002 for Atlfast by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001