BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_unspecifiedbool.h
Go to the documentation of this file.
1/// @file bsls_unspecifiedbool.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_unspecifiedbool.h -*-C++-*-
8#ifndef INCLUDED_BSLS_UNSPECIFIEDBOOL
9#define INCLUDED_BSLS_UNSPECIFIEDBOOL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_unspecifiedbool bsls_unspecifiedbool
15/// @brief Provide a class supporting the `unspecified bool` idiom.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_unspecifiedbool
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_unspecifiedbool-purpose"> Purpose</a>
25/// * <a href="#bsls_unspecifiedbool-classes"> Classes </a>
26/// * <a href="#bsls_unspecifiedbool-description"> Description </a>
27/// * <a href="#bsls_unspecifiedbool-relationship-to-c-11"> Relationship to C++11 </a>
28/// * <a href="#bsls_unspecifiedbool-usage"> Usage </a>
29/// * <a href="#bsls_unspecifiedbool-example-1-a-simple-smart-pointer"> Example 1: A Simple Smart Pointer </a>
30///
31/// # Purpose {#bsls_unspecifiedbool-purpose}
32/// Provide a class supporting the `unspecified bool` idiom.
33///
34/// # Classes {#bsls_unspecifiedbool-classes}
35///
36/// - bsls::UnspecifiedBool: class template for the `unspecified bool` idiom.
37///
38/// # Description {#bsls_unspecifiedbool-description}
39/// This component provides a class template that can be used to
40/// manufacture an "unspecified boolean type" that is distinct for each class
41/// that instantiates it. Note that classes supplying an implicit conversion to
42/// an unspecified bool type will be equality comparable (using `operator==` and
43/// `operator!=`) through this conversion. Private equality and inequality
44/// operators should be added to the class definition unless this comparison is
45/// desired. It is important that each class produces a distinct unspecified
46/// bool type, as otherwise objects of different class types would compare equal
47/// through this same conversion.
48///
49/// ## Relationship to C++11 {#bsls_unspecifiedbool-relationship-to-c-11}
50///
51///
52/// This component will become redundant when all Bloomberg production compilers
53/// support "explicit conversion operators", a feature of C++11. An
54/// `explicit operator bool()` conversion operator is superior to this C++98
55/// idiom in all ways.
56///
57/// ## Usage {#bsls_unspecifiedbool-usage}
58///
59///
60/// This section illustrates intended use of this component.
61///
62/// ### Example 1: A Simple Smart Pointer {#bsls_unspecifiedbool-example-1-a-simple-smart-pointer}
63///
64///
65/// A common requirement for "smart pointer" types is to emulate the native
66/// pointer types and, in particular, support testing for "null" or "empty"
67/// pointer values as a simple boolean conversion in `if` and `while` clauses.
68/// We here demonstrate how to create a simple smart pointer type, `SimplePtr`,
69/// using this component to implement a safe the boolean conversion.
70///
71/// An object of type `SimplePtr` holds a pointer value, but does not claim
72/// ownership or any responsibility for the lifetime of the referenced object.
73/// A `SimplePtr` object acts as a "simple" native pointer.
74///
75/// First, we create the `SimplePtr` class, define its data members, creators
76/// and manipulators:
77/// @code
78/// template <class TYPE>
79/// class SimplePtr
80/// {
81/// // This class holds a pointer to a single object, and provides a subset
82/// // of the regular pointer operators. For example, objects of this
83/// // class can be dereferenced with 'operator*' and tested as a boolean
84/// // value to determine if null. Conversely, this class does not support
85/// // pointer arithmetic.
86///
87/// private:
88/// // DATA
89/// TYPE *d_ptr_p; // address of the referenced object
90///
91/// // PRIVATE ACCESSORS
92/// bool operator==(const SimplePtr &); // = delete;
93/// bool operator!=(const SimplePtr &); // = delete;
94/// // Suppress equality-comparison operations on objects of this
95/// // class.
96///
97/// public:
98/// // CREATORS
99/// explicit SimplePtr(TYPE *ptr = 0) : d_ptr_p(ptr) {}
100/// // Create a 'SimplePtr' having the value of the specified 'ptr'.
101///
102/// //! ~SimplePtr() = default;
103/// // Destroy this object.
104///
105/// // ACCESSORS
106/// TYPE& operator*() const { return *d_ptr_p; }
107/// // Return a reference to the object pointed to by this
108/// // 'SimplePtr'.
109///
110/// TYPE *operator->() const { return d_ptr_p; }
111/// // Return the held 'd_ptr_p'.
112/// @endcode
113/// Next, we define, for convenience, an alias for a unique type that is
114/// implicitly convertible to `bool` (note that we pass the current template
115/// instantiation to the `bsls::UnspecifiedBool` template to guarantee a unique
116/// name, even for different instantiations of this same `SimplePtr` template):
117/// @code
118/// // TYPES
119/// typedef typename bsls::UnspecifiedBool<SimplePtr>::BoolType BoolType;
120/// @endcode
121/// Now, we can define a boolean conversion operator that tests whether or not
122/// this `SimplePtr` object is holding a null pointer, or a valid address:
123/// @code
124/// operator BoolType() const {
125/// return bsls::UnspecifiedBool<SimplePtr>::makeValue(d_ptr_p);
126/// }
127/// }; // class SimplePtr
128/// @endcode
129/// Note that we do not need to define `operator!` as this single boolean
130/// conversion operator is invoked with the correct semantics when the user
131/// tries that operator.
132///
133/// Finally, we write a simple test function, creating a couple of `SimplePtr`
134/// objects, one "null", and the other with a well-defined address.
135/// @code
136/// void runTests() {
137/// SimplePtr<int> p1; // default ctor sets to null
138/// assert(!p1);
139///
140/// int i = 3;
141/// SimplePtr<int> p2(&i);
142///
143/// if (p2) {
144/// assert(3 == *p2);
145/// }
146/// }
147/// @endcode
148/// Notice that `SimplePtr` objects behave as native pointers. They should be
149/// tested before dereferencing (as they could be null).
150/// @}
151/** @} */
152/** @} */
153
154/** @addtogroup bsl
155 * @{
156 */
157/** @addtogroup bsls
158 * @{
159 */
160/** @addtogroup bsls_unspecifiedbool
161 * @{
162 */
163
164
165
166namespace bsls {
167
168 // =====================
169 // class UnspecifiedBool
170 // =====================
171
172/// This class provides a member, `d_member`, whose pointer-to-member may be
173/// used as an "unspecified boolean type" for implicit conversion operators.
174///
175/// See @ref bsls_unspecifiedbool
176template<class BSLS_HOST_TYPE>
178
179 private:
180 // DATA
181 int d_member; // This data member is used solely for taking its address
182 // to return a non-null pointer-to-member. Note that the
183 // *value* of 'd_member' is not used.
184
185 public:
186 // TYPES
187
188 /// Alias of a distinct type that is implicitly convertible to `bool`,
189 /// but does not promote to `int`.
190 typedef int UnspecifiedBool::* BoolType;
191
192 // CLASS METHODS
193
194 /// Return a value that converts to the `bool` value `false`.
195 static BoolType falseValue();
196
197 /// Return a value that converts to the `bool` value `true`.
198 static BoolType trueValue();
199
200 /// Return a value that converts to the `bool` value `true` if the
201 /// specified predicate is `true`, and the `bool` value `false`
202 /// otherwise.
203 static BoolType makeValue(bool predicate);
204};
205
206
207// ============================================================================
208// INLINE FUNCTION DEFINITIONS
209// ============================================================================
210
211// CLASS METHODS
212template<class BSLS_HOST_TYPE>
213inline
219
220template<class BSLS_HOST_TYPE>
221inline
224{
225 return &UnspecifiedBool::d_member;
226}
227
228template<class BSLS_HOST_TYPE>
229inline
232{
233 return predicate ? trueValue() : falseValue();
234}
235
236} // close package namespace
237
238#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
239// ============================================================================
240// BACKWARD COMPATIBILITY
241// ============================================================================
242
243#ifdef bsls_UnspecifiedBool
244#undef bsls_UnspecifiedBool
245#endif
246/// This alias is defined for backward compatibility.
247#define bsls_UnspecifiedBool bsls::UnspecifiedBool
248#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
249
250
251
252#endif
253
254// ----------------------------------------------------------------------------
255// Copyright 2013 Bloomberg Finance L.P.
256//
257// Licensed under the Apache License, Version 2.0 (the "License");
258// you may not use this file except in compliance with the License.
259// You may obtain a copy of the License at
260//
261// http://www.apache.org/licenses/LICENSE-2.0
262//
263// Unless required by applicable law or agreed to in writing, software
264// distributed under the License is distributed on an "AS IS" BASIS,
265// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
266// See the License for the specific language governing permissions and
267// limitations under the License.
268// ----------------------------- END-OF-FILE ----------------------------------
269
270/** @} */
271/** @} */
272/** @} */
Definition bsls_unspecifiedbool.h:177
int UnspecifiedBool::* BoolType
Definition bsls_unspecifiedbool.h:190
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691