X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2Fsafe_bool.hh;fp=Utils%2FSafeBool.hh;h=50a113a81187bb1558d7c65191d48e00e1ecf7f5;hb=f00a102138bcbabdaab1caab1db6a8876463dedc;hp=eb116da1f2676db6edecc36d845c6f12a3031c9f;hpb=ff0f2fabb3dbb76ee9383a98291a1420d2a59a7f;p=senf.git diff --git a/Utils/SafeBool.hh b/Utils/safe_bool.hh similarity index 50% rename from Utils/SafeBool.hh rename to Utils/safe_bool.hh index eb116da..50a113a 100644 --- a/Utils/SafeBool.hh +++ b/Utils/safe_bool.hh @@ -21,66 +21,76 @@ // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** \file - \brief SafeBool public header */ + \brief safe_bool public header */ -#ifndef HH_SafeBool_ -#define HH_SafeBool_ 1 +#ifndef HH_safe_bool_ +#define HH_safe_bool_ 1 // Custom includes -//#include "SafeBool.mpp" +//#include "safe_bool.mpp" ///////////////////////////////hh.p//////////////////////////////////////// namespace senf { - /** \brief internal SafeBool base class + /** \brief internal safe_bool base class \internal + + \see safe_bool */ - class SafeBoolBase + class safe_bool_base { protected: - typedef void (SafeBoolBase::*bool_type)() const; + typedef void (safe_bool_base::*bool_type)() const; void this_type_does_not_support_comparisons() const; // Just here to make them protected ... - SafeBoolBase(); - SafeBoolBase(const SafeBoolBase&); - SafeBoolBase& operator=(const SafeBoolBase&); - ~SafeBoolBase(); + safe_bool_base(); + safe_bool_base(const safe_bool_base&); + safe_bool_base& operator=(const safe_bool_base&); + ~safe_bool_base(); + }; + + /** \brief Mixin class for safe bool conversion support (comparable classes) + + \see safe_bool + */ + template + class comparable_safe_bool + : public safe_bool_base + { + public: + operator bool_type() const; + bool operator !() const; + + protected: + ~comparable_safe_bool(); }; /** \brief Mixin class for safe boolean conversion support - This is a direct yet simplified copy of a safe bool solution - by Bjorn Karlsson from + 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 whether the objects are - identical, it will only check, that both return the same + 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 whether 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. + 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: + To make a class boolean testable, just inherit from the mixin and implement \c boolean_test: \code class Testable - : public SafeBool + : public safe_bool { public: bool boolean_test() const @@ -95,38 +105,29 @@ namespace senf { ... } \endcode + + If the class to be made using senf::safe_bool is itself comparable via it's own \c + operator==, you must use comparable_safe_bool instead of safe_bool to not loose this + capability. - \todo Either rename intrusive_refcount to IntrusiveRefcount or - SafeBool to safe_bool (I tend to the latter ...) + \see comparable_safe_bool */ template - class ComparableSafeBool - : public SafeBoolBase - { - public: - operator bool_type() const; - bool operator !() const; - - protected: - ~ComparableSafeBool(); - }; - - template - class SafeBool : public ComparableSafeBool {}; + class safe_bool : public comparable_safe_bool {}; template - void operator==(const SafeBool& lhs,const SafeBool& rhs); + void operator==(const safe_bool& lhs,const safe_bool& rhs); template - void operator!=(const SafeBool& lhs,const SafeBool& rhs); + void operator!=(const safe_bool& lhs,const safe_bool& rhs); } ///////////////////////////////hh.e//////////////////////////////////////// -#include "SafeBool.cci" -//#include "SafeBool.ct" -#include "SafeBool.cti" -//#include "SafeBool.mpp" +#include "safe_bool.cci" +//#include "safe_bool.ct" +#include "safe_bool.cti" +//#include "safe_bool.mpp" #endif