4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief safe_bool public header */
27 #define HH_safe_bool_ 1
31 //#include "safe_bool.mpp"
32 ///////////////////////////////hh.p////////////////////////////////////////
36 /** \brief internal safe_bool base class
44 typedef void (safe_bool_base::*bool_type)() const;
45 void this_type_does_not_support_comparisons() const;
47 // Just here to make them protected ...
50 safe_bool_base(const safe_bool_base&);
51 safe_bool_base& operator=(const safe_bool_base&);
55 /** \brief Mixin class for safe bool conversion support (comparable classes)
60 class comparable_safe_bool
61 : public safe_bool_base
64 operator bool_type() const;
65 bool operator !() const;
68 ~comparable_safe_bool();
71 /** \brief Mixin class for safe boolean conversion support
73 This is a direct yet simplified copy of a safe bool solution by Bjorn Karlsson from
74 http://www.artima.com/cppsource/safebool.html
76 This mixin provides the client class with safe boolean testing. It is a safe replacement for
77 <tt>operator bool</tt>. <tt>operator bool</tt> is problematic since \c bool is an integer
78 type. This conversion operator makes the class usable in any numeric context, which can be
79 quite dangerous. The <tt>operator void *</tt> solution is much better in this respect but
80 still allows two instances of any class having such a <tt>void *</tt> conversion to be
81 compared for equality. This again will produce absolutely unexpected results since it will
82 not check whether the objects are identical, it will only check, that both return the same
85 This solutions solves all these problems by returning a pointer-to-member which cannot be
86 converted to any other type. By providing explicit implementations of \c operator== and \c
87 operator!= which fail in an obvious way at compile time, this hazard is removed.
89 To make a class boolean testable, just inherit from the mixin and implement \c boolean_test:
93 : public safe_bool<Testable>
96 bool boolean_test() const
98 // Perform Boolean logic here
109 If the class to be made using senf::safe_bool is itself comparable via it's own \c
110 operator==, you must use comparable_safe_bool instead of safe_bool to not loose this
113 \see comparable_safe_bool
115 template <typename T>
116 class safe_bool : public comparable_safe_bool<T> {};
118 template <typename T, typename U>
119 void operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs);
121 template <typename T,typename U>
122 void operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs);
126 ///////////////////////////////hh.e////////////////////////////////////////
127 #include "safe_bool.cci"
128 //#include "safe_bool.ct"
129 #include "safe_bool.cti"
130 //#include "safe_bool.mpp"
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // comment-column: 40