00001 #ifndef SAFE_BOOL_HPP 00002 #define SAFE_BOOL_HPP SAFE_BOOL_HPP 00003 00004 /** 00005 Taken from http://www.artima.com/cppsource/safebool3.html 00006 See there for more information. 00007 */ 00008 class safe_bool_base { 00009 protected: 00010 typedef void (safe_bool_base::*bool_type)() const; 00011 void this_type_does_not_support_comparisons() const {} 00012 00013 safe_bool_base() {} 00014 safe_bool_base(const safe_bool_base&) {} 00015 safe_bool_base& operator=(const safe_bool_base&) {return *this;} 00016 ~safe_bool_base() {} 00017 }; 00018 00019 template <typename T=void> class safe_bool : public safe_bool_base { 00020 public: 00021 operator bool_type() const { 00022 return (static_cast<const T*>(this))->boolean_test() 00023 ? &safe_bool<T>::this_type_does_not_support_comparisons 00024 : 0; 00025 } 00026 protected: 00027 ~safe_bool() {} 00028 }; 00029 00030 template<> class safe_bool<void> : public safe_bool_base { 00031 public: 00032 operator bool_type() const { 00033 return boolean_test()==true ? 00034 &safe_bool<void>::this_type_does_not_support_comparisons 00035 : 0; 00036 } 00037 protected: 00038 virtual bool boolean_test() const=0; 00039 virtual ~safe_bool() {} 00040 }; 00041 00042 template <typename T, typename U> 00043 void operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs) { 00044 lhs.this_type_does_not_support_comparisons(); 00045 return false; 00046 } 00047 00048 template <typename T,typename U> 00049 void operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs) { 00050 lhs.this_type_does_not_support_comparisons(); 00051 return false; 00052 } 00053 00054 #endif // SAFE_BOOL_HPP