4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief safe_bool public header */
31 #ifndef HH_SENF_Utils_safe_bool_
32 #define HH_SENF_Utils_safe_bool_ 1
36 //#include "safe_bool.mpp"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 /** \brief internal safe_bool base class
49 typedef void (safe_bool_base::*bool_type)() const;
50 void this_type_does_not_support_comparisons() const;
52 // Just here to make them protected ...
55 safe_bool_base(const safe_bool_base&);
56 safe_bool_base& operator=(const safe_bool_base&);
60 /** \brief Mixin class for safe bool conversion support (comparable classes)
65 class comparable_safe_bool
66 : public safe_bool_base
69 operator bool_type() const;
70 bool operator !() const;
73 ~comparable_safe_bool();
76 /** \brief Mixin class for safe boolean conversion support
78 This is a direct yet simplified copy of a safe bool solution by Bjorn Karlsson from
79 http://www.artima.com/cppsource/safebool.html
81 This mixin provides the client class with safe boolean testing. It is a safe replacement for
82 <tt>operator bool</tt>. <tt>operator bool</tt> is problematic since \c bool is an integer
83 type. This conversion operator makes the class usable in any numeric context, which can be
84 quite dangerous. The <tt>operator void *</tt> solution is much better in this respect but
85 still allows two instances of any class having such a <tt>void *</tt> conversion to be
86 compared for equality. This again will produce absolutely unexpected results since it will
87 not check whether the objects are identical, it will only check, that both return the same
90 This solutions solves all these problems by returning a pointer-to-member which cannot be
91 converted to any other type. By providing explicit implementations of \c operator== and \c
92 operator!= which fail in an obvious way at compile time, this hazard is removed.
94 To make a class boolean testable, just inherit from the mixin and implement \c boolean_test:
98 : public safe_bool<Testable>
101 bool boolean_test() const
103 // Perform Boolean logic here
114 If the class to be made using senf::safe_bool is itself comparable via it's own \c
115 operator==, you must use comparable_safe_bool instead of safe_bool to not loose this
118 \see comparable_safe_bool
120 template <typename T>
121 class safe_bool : public comparable_safe_bool<T> {};
123 template <typename T, typename U>
124 void operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs);
126 template <typename T,typename U>
127 void operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs);
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 #include "safe_bool.cci"
133 //#include "safe_bool.ct"
134 #include "safe_bool.cti"
135 //#include "safe_bool.mpp"
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40