00001
00002
00003
00004
00005
00006
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){}
00032 Fallible():is_valid(false){}
00033 bool failed() const{ return !is_valid;}
00034 bool valid() const{ return is_valid;}
00035 void invalidate() { is_valid = false;}
00036
00037
00038
00039
00040 operator T() const{
00041 if(failed()) {
00042 ::std::string s("Fallible in Invalid State");
00043
00044
00045 throw InvalidStateErr();
00046 }
00047 return instance;
00048 }
00049 private:
00050 bool is_valid;
00051 T instance;
00052 };
00053
00054 }
00055 #endif
00056
00057
00058
00059
00060
00061
00062
00063