00001 00027 #ifndef COMMON_SAFE_BOOL_H 00028 #define COMMON_SAFE_BOOL_H 00029 00030 namespace Common { 00031 namespace impl { 00032 template <typename T> 00033 struct no_base {}; 00034 00035 template <typename T> 00036 struct safe_bool_impl { 00037 typedef T *TP; // workaround to make parsing easier 00038 TP stub; 00039 typedef TP safe_bool_impl::*type; 00040 }; 00041 } 00042 00046 template <typename DerivedT, typename BaseT = impl::no_base<DerivedT> > 00047 struct SafeBool : BaseT { 00048 private: 00049 typedef impl::safe_bool_impl<DerivedT> impl_t; 00050 typedef typename impl_t::type bool_type; 00051 00052 public: 00053 operator bool_type() const { 00054 return static_cast<const DerivedT *>(this)->operator_bool() ? 00055 &impl_t::stub : nullptr; 00056 } 00057 00058 operator bool_type() { 00059 return static_cast<DerivedT *>(this)->operator_bool() ? 00060 &impl_t::stub : 0; 00061 } 00062 }; 00063 } // End of namespace Common 00064 00065 #endif