BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslalg_functoradapter.h
Go to the documentation of this file.
1/// @file bslalg_functoradapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslalg_functoradapter.h -*-C++-*-
8#ifndef INCLUDED_BSLALG_FUNCTORADAPTER
9#define INCLUDED_BSLALG_FUNCTORADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslalg_functoradapter bslalg_functoradapter
15/// @brief Provide an utility that adapts callable objects to functors.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslalg
19/// @{
20/// @addtogroup bslalg_functoradapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslalg_functoradapter-purpose"> Purpose</a>
25/// * <a href="#bslalg_functoradapter-classes"> Classes </a>
26/// * <a href="#bslalg_functoradapter-description"> Description </a>
27/// * <a href="#bslalg_functoradapter-usage"> Usage </a>
28/// * <a href="#bslalg_functoradapter-example-1-using-function-pointer-base-for-an-empty-base-optimized-class"> Example 1: Using function pointer base for an empty-base optimized class </a>
29///
30/// # Purpose {#bslalg_functoradapter-purpose}
31/// Provide an utility that adapts callable objects to functors.
32///
33/// # Classes {#bslalg_functoradapter-classes}
34///
35/// - bslalg::FunctorAdapter: utility for using callable objects as functors
36///
37/// @see bslstl_setcomparator, bslstl_mapcomparator
38///
39/// # Description {#bslalg_functoradapter-description}
40/// This component provides a single utility template,
41/// `FunctorAdapter`, that adapts a parameterized adaptee type, which can be any
42/// callable object type, to a target functor type. This adaptation enables a
43/// client to inherit from the target functor type even if the adaptee callable
44/// object type is a function pointer type. This is particularly useful if the
45/// client of the callable object type wants to take advantage of the empty-base
46/// optimization to avoid paying storage cost when the callable object type is a
47/// functor type with no data members.
48///
49/// `FunctorAdapter` defines an alias to the target functor type. If the
50/// adaptee type is a functor type, the target type is an alias to the adaptee
51/// type. If the adaptee type is a function pointer type, the target type is a
52/// functor type that delegates to a function referred to by a function pointer
53/// of the adaptee type.
54///
55/// ## Usage {#bslalg_functoradapter-usage}
56///
57///
58/// This section illustrates the intended use of this component.
59///
60/// ### Example 1: Using function pointer base for an empty-base optimized class {#bslalg_functoradapter-example-1-using-function-pointer-base-for-an-empty-base-optimized-class}
61///
62///
63/// Suppose that we wanted to define a binder that binds a binary predicate of a
64/// parameterized type to a value passed on construction. Also suppose that we
65/// wanted to use the empty-base optimization to avoid paying storage cost when
66/// the predicate type is a functor type with no data members. Unfortunately,
67/// the binary predicate type may be a function pointer type, which cannot serve
68/// as a base class. The solution is to have the binder inherit from
69/// `FunctorAdapter::Type`, which adapts a function pointer type to a functor
70/// type that is a suitable base class.
71///
72/// First, we define the class `Bind2ndInteger`, which inherits from
73/// `FunctorAdapter::Type` to take advantage of the empty-base optimization:
74/// @code
75/// /// This class provides a functor that delegate its function-call
76/// /// operator to the parameterized `BINARY_PREDICATE`, passing the user
77/// /// supplied parameter as the first argument and the integer value
78/// /// passed on construction as the second argument.
79/// template <class BINARY_PREDICATE>
80/// class Bind2ndInteger : private FunctorAdapter<BINARY_PREDICATE>::Type {
81///
82/// // DATA
83/// int d_bondValue; // the bound value
84///
85/// private:
86/// // NOT IMPLEMENTED
87/// Bind2ndInteger(const Bind2ndInteger&);
88/// Bind2ndInteger& operator=(const Bind2ndInteger&);
89///
90/// public:
91/// // CREATORS
92///
93/// /// Create a `Bind2ndInteger` object that will bind the second
94/// /// parameter of the specified `predicate` with the specified
95/// /// integer `value`.
96/// Bind2ndInteger(int value, const BINARY_PREDICATE& predicate);
97///
98/// /// Destroy this object.
99/// //! ~Bind2ndInteger() = default;
100///
101/// // ACCESSORS
102///
103/// /// Return the result of calling the parameterized
104/// /// `BINARY_PREDICATE` passing the specified `value` as the first
105/// /// argument and the integer value passed on construction as the
106/// /// second argument.
107/// bool operator() (const int value) const;
108/// };
109/// @endcode
110/// Then, we implement the methods of the `Bind2ndInteger` class:
111/// @code
112/// template <class BINARY_PREDICATE>
113/// Bind2ndInteger<BINARY_PREDICATE>::Bind2ndInteger(int value,
114/// const BINARY_PREDICATE& predicate)
115/// : FunctorAdapter<BINARY_PREDICATE>::Type(predicate), d_bondValue(value)
116/// {
117/// }
118/// @endcode
119/// Here, we implement the `operator()` member function that simply delegates to
120/// `BINARY_PREDICATE`
121/// @code
122/// template <class BINARY_PREDICATE>
123/// bool Bind2ndInteger<BINARY_PREDICATE>::operator() (const int value) const
124/// {
125/// const BINARY_PREDICATE& predicate = *this;
126/// return predicate(value, d_bondValue);
127/// }
128/// @endcode
129/// Next, we define a function, `intCompareFunction`, that compares two
130/// integers:
131/// @code
132/// bool intCompareFunction(const int lhs, const int rhs)
133/// {
134/// return lhs < rhs;
135/// }
136/// @endcode
137/// Now, we define a `Bind2ndInteger` object `functorLessThan10` using the
138/// `std::less<int>` functor as the parameterized `BINARY_PREDICATE` and invoke
139/// the function call operator:
140/// @code
141/// Bind2ndInteger<std::less<int> > functorLessThan10(10, std::less<int>());
142///
143/// assert(functorLessThan10(1));
144/// assert(!functorLessThan10(12));
145/// @endcode
146/// Finally, we define a `Bind2ndInteger` object `functionLessThan10` passing
147/// the address of `intCompareFunction` on construction and invoke the function
148/// call operator:
149/// @code
150/// Bind2ndInteger<bool (*)(const int, const int)>
151/// functionLessThan10(10, &intCompareFunction);
152///
153/// assert(functionLessThan10(1));
154/// assert(!functionLessThan10(12));
155/// @endcode
156/// @}
157/** @} */
158/** @} */
159
160/** @addtogroup bsl
161 * @{
162 */
163/** @addtogroup bslalg
164 * @{
165 */
166/** @addtogroup bslalg_functoradapter
167 * @{
168 */
169
170#include <bslscm_version.h>
171
172#include <bslmf_assert.h>
174
175#include <bsls_assert.h>
176
177
178namespace bslalg {
179
180 // ====================================
181 // class FunctorAdapter_FunctionPointer
182 // ====================================
183
184/// This class provides a functor that delegates to the function referred to
185/// by a function pointer supplied on construction. Delegation is supported
186/// through the conversion operator, which implicitly returns a reference to
187/// the parameterized `FUNCTION_POINTER`.
188///
189/// See @ref bslalg_functoradapter
190template <class FUNCTION_POINTER>
192
193 private:
194 // DATA
195 FUNCTION_POINTER d_function_p; // the pointer to the function
196
197 public:
198 // CREATORS
199
200 /// Create a `FunctorAdapter_FunctionPointer` object that will delegate
201 /// to the function referred to by the specified `functionPtr`.
202 explicit FunctorAdapter_FunctionPointer(FUNCTION_POINTER functionPtr);
203
204 // MANIPULATORS
205
206 /// Convert this object to the parameterized `FUNCTION_POINTER` by
207 /// returning the function pointer supplied on construction.
208 operator FUNCTION_POINTER& ();
209
210 // ACCESSORS
211
212 /// Convert this object to the parameterized `FUNCTION_POINTER` by
213 /// returning the function pointer supplied on construction.
214 operator const FUNCTION_POINTER& () const;
215};
216
217 // ====================
218 // class FunctorAdapter
219 // ====================
220
221/// This class provides a metafunction that defines an alias `Type` for the
222/// parameterized `CALLABLE_OBJECT`. `Type` is functor type that provides
223/// the same operation as the parameterized `CALLABLE_OBJECT`.
224///
225/// \note Note that function pointers are supported through a specialization of this
226/// template.
227///
228/// See @ref bslalg_functoradapter
229template <class CALLABLE_OBJECT>
231
232 public:
233 // PUBLIC TYPES
234
235 /// This `typedef` is an alias for the functor.
236 typedef CALLABLE_OBJECT Type;
237};
238
239 // ====================
240 // class FunctorAdapter
241 // ====================
242
243/// This specialization of `FunctorAdapter` defines an alias `Type` for a
244/// functor that delegates to a function pointer matching the parameterized
245/// `FUNCTION` type.
246template <class FUNCTION>
247class FunctorAdapter<FUNCTION*> {
248
250 // This 'BSLMF_ASSERT' statement ensures that the parameter 'FUNCTION'
251 // must be a function pointer.
252
253 public:
254 // PUBLIC TYPES
255
256 /// This `typedef` is an alias for a functor that delegates to the function
257 /// referred to by the function pointer matching the parameterized
258 /// `FUNCTION` type.
260};
261
262
263// ============================================================================
264// TEMPLATE AND INLINE FUNCTION DEFINITIONS
265// ============================================================================
266
267 // ------------------------------------
268 // class FunctorAdapter_FunctionPointer
269 // ------------------------------------
270
271// CREATORS
272template <class FUNCTION_POINTER>
273inline
275::FunctorAdapter_FunctionPointer(FUNCTION_POINTER functionPtr)
276:d_function_p(functionPtr)
277{
278}
279
280// MANIPULATORS
281template <class FUNCTION_POINTER>
282inline
284::operator FUNCTION_POINTER& ()
285{
286 return d_function_p;
287}
288
289// ACCESSORS
290template <class FUNCTION_POINTER>
291inline
293::operator const FUNCTION_POINTER& () const
294{
295 return d_function_p;
296}
297
298} // close package namespace
299
300
301
302#endif
303
304// ----------------------------------------------------------------------------
305// Copyright 2013 Bloomberg Finance L.P.
306//
307// Licensed under the Apache License, Version 2.0 (the "License");
308// you may not use this file except in compliance with the License.
309// You may obtain a copy of the License at
310//
311// http://www.apache.org/licenses/LICENSE-2.0
312//
313// Unless required by applicable law or agreed to in writing, software
314// distributed under the License is distributed on an "AS IS" BASIS,
315// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
316// See the License for the specific language governing permissions and
317// limitations under the License.
318// ----------------------------- END-OF-FILE ----------------------------------
319
320/** @} */
321/** @} */
322/** @} */
FunctorAdapter_FunctionPointer< FUNCTION * > Type
Definition bslalg_functoradapter.h:259
Definition bslalg_functoradapter.h:191
Definition bslalg_functoradapter.h:230
CALLABLE_OBJECT Type
This typedef is an alias for the functor.
Definition bslalg_functoradapter.h:236
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_flathashmap.h:2218
Definition bslmf_functionpointertraits.h:163