10 //#include "SafeBool.mpp"
11 ///////////////////////////////hh.p////////////////////////////////////////
15 /** \brief internal SafeBool base class
21 typedef void (SafeBoolBase::*bool_type)() const;
22 void this_type_does_not_support_comparisons() const;
24 // Just here to make them protected ...
27 SafeBoolBase(const SafeBoolBase&);
28 SafeBoolBase& operator=(const SafeBoolBase&);
32 /** \brief Mixin class for safe boolean conversion support
34 This is a direct yet simplified copy of a safe bool solution
35 by Bjorn Karlsson from
36 http://www.artima.com/cppsource/safebool.html
38 This mixin provides the client class with safe boolean
39 testing. It is a safe replacement for <tt>operator
40 bool</tt>. <tt>operator bool</tt> is problematic since \c bool
41 is an integer type. This conversion operator makes the class
42 usable in any numeric context, which can be quite
43 dangerous. The <tt>operator void *</tt> solution is much
44 better in this respect but still allows two instances of any
45 class having such a <tt>void *</tt> conversion to be compared
46 for equality. This again will produce absolutely unexpected
47 results since it will not check whether the objects are
48 identical, it will only check, that both return the same
51 This solutions solves all these problems by returning a
52 pointer-to-member which cannot be converted to any other
53 type. By providing explicit implementations of \c operator==
54 and \c operator!= which fail in an obvious way at compile
55 time, this hazard is removed.
57 To make a class boolean testable, just inherit from the mixin
58 and implement \c boolean_test:
62 : public SafeBool<Testable>
65 bool boolean_test() const
67 // Perform Boolean logic here
78 \todo Either rename intrusive_refcount to IntrusiveRefcount or
79 SafeBool to safe_bool (I tend to the latter ...)
82 class ComparableSafeBool
86 operator bool_type() const;
87 bool operator !() const;
90 ~ComparableSafeBool();
94 class SafeBool : public ComparableSafeBool<T> {};
96 template <typename T, typename U>
97 void operator==(const SafeBool<T>& lhs,const SafeBool<U>& rhs);
99 template <typename T,typename U>
100 void operator!=(const SafeBool<T>& lhs,const SafeBool<U>& rhs);
104 ///////////////////////////////hh.e////////////////////////////////////////
105 #include "SafeBool.cci"
106 //#include "SafeBool.ct"
107 #include "SafeBool.cti"
108 //#include "SafeBool.mpp"
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // comment-column: 40