X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FSafeBool.hh;h=0b944bcf980fc5253aa5f5c747a0f98d41064e46;hb=85ab07d100a382467a42e19d741d403a7a96c951;hp=68b0ffca24538dc63aa98eef69885533b9c85069;hpb=3e42ecb22121f2e6df86b27bea73f890384a4ee4;p=senf.git diff --git a/Utils/SafeBool.hh b/Utils/SafeBool.hh index 68b0ffc..0b944bc 100644 --- a/Utils/SafeBool.hh +++ b/Utils/SafeBool.hh @@ -1,6 +1,6 @@ // $Id$ // -// Copyright (C) 2006 +// Copyright (C) 2006 #ifndef HH_SafeBool_ #define HH_SafeBool_ 1 @@ -13,87 +13,87 @@ namespace senf { /** \brief internal SafeBool base class - \internal + \internal */ - class SafeBoolBase + class SafeBoolBase { protected: - typedef void (SafeBoolBase::*bool_type)() const; - void this_type_does_not_support_comparisons() const; + typedef void (SafeBoolBase::*bool_type)() const; + void this_type_does_not_support_comparisons() const; - // Just here to make them protected ... + // Just here to make them protected ... - SafeBoolBase(); - SafeBoolBase(const SafeBoolBase&); - SafeBoolBase& operator=(const SafeBoolBase&); - ~SafeBoolBase(); + SafeBoolBase(); + SafeBoolBase(const SafeBoolBase&); + SafeBoolBase& operator=(const SafeBoolBase&); + ~SafeBoolBase(); }; /** \brief Mixin class for safe boolean conversion support - - This is a direct yet simplified copy of a safe bool solution - by Bjorn Karlsson from - http://www.artima.com/cppsource/safebool.html - - This mixin provides the client class with safe boolean - testing. It is a safe replacement for operator - bool. operator bool is problematic since \c bool - is an integer type. This conversion operator makes the class - usable in any numeric context, which can be quite - dangerous. The operator void * solution is much - better in this respect but still allows two instances of any - class having such a void * conversion to be compared - for equality. This again will produce absolutely unexpected - results since it will not check wethere the objects are - identical, it will only check, that both return the same - boolean state. - - This solutions solves all these problems by returning a - pointer-to-member which cannot be converted to any other - type. By providing explicit implementations of \c operator== - and \c operator!= which fail in an obvious way at compile - time, this hazard is removed. - - To make a class boolean testable, just inherit from the mixin - and implement \c boolean_test: - - \code - class Testable - : public SafeBool + + This is a direct yet simplified copy of a safe bool solution + by Bjorn Karlsson from + http://www.artima.com/cppsource/safebool.html + + This mixin provides the client class with safe boolean + testing. It is a safe replacement for operator + bool. operator bool is problematic since \c bool + is an integer type. This conversion operator makes the class + usable in any numeric context, which can be quite + dangerous. The operator void * solution is much + better in this respect but still allows two instances of any + class having such a void * conversion to be compared + for equality. This again will produce absolutely unexpected + results since it will not check wethere the objects are + identical, it will only check, that both return the same + boolean state. + + This solutions solves all these problems by returning a + pointer-to-member which cannot be converted to any other + type. By providing explicit implementations of \c operator== + and \c operator!= which fail in an obvious way at compile + time, this hazard is removed. + + To make a class boolean testable, just inherit from the mixin + and implement \c boolean_test: + + \code + class Testable + : public SafeBool { public: - bool boolean_test() const + bool boolean_test() const { // Perform Boolean logic here } }; - Testable t = ...; + Testable t = ...; - if (t) { - ... - } - \endcode + if (t) { + ... + } + \endcode - \todo Either rename intrusive_refcount to IntrusiveRefcount or - SafeBool to safe_bool (I tend to the latter ...) + \todo Either rename intrusive_refcount to IntrusiveRefcount or + SafeBool to safe_bool (I tend to the latter ...) */ - template - class SafeBool - : public SafeBoolBase + template + class SafeBool + : public SafeBoolBase { public: - operator bool_type() const; - bool operator !() const; + operator bool_type() const; + bool operator !() const; protected: - ~SafeBool(); + ~SafeBool(); }; - template + template void operator==(const SafeBool& lhs,const SafeBool& rhs); - template + template void operator!=(const SafeBool& lhs,const SafeBool& rhs); } @@ -108,4 +108,8 @@ namespace senf { // Local Variables: // mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" // End: