BDE 4.14.0 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/// // NOT IMPLEMENTED
86/// Bind2ndInteger(const Bind2ndInteger&);
87/// Bind2ndInteger& operator=(const Bind2ndInteger&);
88///
89/// public:
90/// // CREATORS
91///
92/// /// Create a `Bind2ndInteger` object that will bind the second
93/// /// parameter of the specified `predicate` with the specified
94/// /// integer `value`.
95/// Bind2ndInteger(int value, const BINARY_PREDICATE& predicate);
96///
97/// /// Destroy this object.
98/// //! ~Bind2ndInteger() = default;
99///
100/// // ACCESSORS
101///
102/// /// Return the result of calling the parameterized
103/// /// `BINARY_PREDICATE` passing the specified `value` as the first
104/// /// argument and the integer value passed on construction as the
105/// /// second argument.
106/// bool operator() (const int value) const;
107/// };
108/// @endcode
109/// Then, we implement the methods of the `Bind2ndInteger` class:
110/// @code
111/// template <class BINARY_PREDICATE>
112/// Bind2ndInteger<BINARY_PREDICATE>::Bind2ndInteger(int value,
113/// const BINARY_PREDICATE& predicate)
114/// : FunctorAdapter<BINARY_PREDICATE>::Type(predicate), d_bondValue(value)
115/// {
116/// }
117/// @endcode
118/// Here, we implement the `operator()` member function that simply delegates to
119/// `BINARY_PREDICATE`
120/// @code
121/// template <class BINARY_PREDICATE>
122/// bool Bind2ndInteger<BINARY_PREDICATE>::operator() (const int value) const
123/// {
124/// const BINARY_PREDICATE& predicate = *this;
125/// return predicate(value, d_bondValue);
126/// }
127/// @endcode
128/// Next, we define a function, `intCompareFunction`, that compares two
129/// integers:
130/// @code
131/// bool intCompareFunction(const int lhs, const int rhs)
132/// {
133/// return lhs < rhs;
134/// }
135/// @endcode
136/// Now, we define a `Bind2ndInteger` object `functorLessThan10` using the
137/// `std::less<int>` functor as the parameterized `BINARY_PREDICATE` and invoke
138/// the function call operator:
139/// @code
140/// Bind2ndInteger<std::less<int> > functorLessThan10(10, std::less<int>());
141///
142/// assert(functorLessThan10(1));
143/// assert(!functorLessThan10(12));
144/// @endcode
145/// Finally, we define a `Bind2ndInteger` object `functionLessThan10` passing
146/// the address of `intCompareFunction` on construction and invoke the function
147/// call operator:
148/// @code
149/// Bind2ndInteger<bool (*)(const int, const int)>
150/// functionLessThan10(10, &intCompareFunction);
151///
152/// assert(functionLessThan10(1));
153/// assert(!functionLessThan10(12));
154/// @endcode
155/// @}
156/** @} */
157/** @} */
158
159/** @addtogroup bsl
160 * @{
161 */
162/** @addtogroup bslalg
163 * @{
164 */
165/** @addtogroup bslalg_functoradapter
166 * @{
167 */
168
169#include <bslscm_version.h>
170
171#include <bslmf_assert.h>
173
174#include <bsls_assert.h>
175
176
177namespace bslalg {
178
179 // ====================================
180 // class FunctorAdapter_FunctionPointer
181 // ====================================
182
183/// This class provides a functor that delegates to the function referred to
184/// by a function pointer supplied on construction. Delegation is supported
185/// through the conversion operator, which implicitly returns a reference to
186/// the parameterized `FUNCTION_POINTER`.
187///
188/// See @ref bslalg_functoradapter
189template <class FUNCTION_POINTER>
191
192 private:
193 // DATA
194 FUNCTION_POINTER d_function_p; // the pointer to the function
195
196 public:
197 // CREATORS
198
199 /// Create a `FunctorAdapter_FunctionPointer` object that will delegate
200 /// to the function referred to by the specified `functionPtr`.
201 explicit FunctorAdapter_FunctionPointer(FUNCTION_POINTER functionPtr);
202
203 // MANIPULATORS
204
205 /// Convert this object to the parameterized `FUNCTION_POINTER` by
206 /// returning the function pointer supplied on construction.
207 operator FUNCTION_POINTER& ();
208
209 // ACCESSORS
210
211 /// Convert this object to the parameterized `FUNCTION_POINTER` by
212 /// returning the function pointer supplied on construction.
213 operator const FUNCTION_POINTER& () const;
214};
215
216 // ====================
217 // class FunctorAdapter
218 // ====================
219
220/// This class provides a metafunction that defines an alias `Type` for the
221/// parameterized `CALLABLE_OBJECT`. `Type` is functor type that provides
222/// the same operation as the parameterized `CALLABLE_OBJECT`. Note that
223/// function pointers are supported through a specialization of this
224/// template.
225///
226/// See @ref bslalg_functoradapter
227template <class CALLABLE_OBJECT>
229
230 public:
231 // PUBLIC TYPES
232
233 /// This `typedef` is an alias for the functor.
234 typedef CALLABLE_OBJECT Type;
235};
236
237 // ====================
238 // class FunctorAdapter
239 // ====================
240
241/// This specialization of `FunctorAdapter` defines an alias `Type` for a
242/// functor that delegates to a function pointer matching the parameterized
243/// `FUNCTION` type.
244template <class FUNCTION>
245class FunctorAdapter<FUNCTION*> {
246
248 // This 'BSLMF_ASSERT' statement ensures that the parameter 'FUNCTION'
249 // must be a function pointer.
250
251 public:
252 // PUBLIC TYPES
253
254 /// This `typedef` is an alias for a functor that delegates to the function
255 /// referred to by the function pointer matching the parameterized
256 /// `FUNCTION` type.
258};
259
260
261// ============================================================================
262// TEMPLATE AND INLINE FUNCTION DEFINITIONS
263// ============================================================================
264
265 // ------------------------------------
266 // class FunctorAdapter_FunctionPointer
267 // ------------------------------------
268
269// CREATORS
270template <class FUNCTION_POINTER>
271inline
273::FunctorAdapter_FunctionPointer(FUNCTION_POINTER functionPtr)
274:d_function_p(functionPtr)
275{
276}
277
278// MANIPULATORS
279template <class FUNCTION_POINTER>
280inline
282::operator FUNCTION_POINTER& ()
283{
284 return d_function_p;
285}
286
287// ACCESSORS
288template <class FUNCTION_POINTER>
289inline
291::operator const FUNCTION_POINTER& () const
292{
293 return d_function_p;
294}
295
296} // close package namespace
297
298
299
300#endif
301
302// ----------------------------------------------------------------------------
303// Copyright 2013 Bloomberg Finance L.P.
304//
305// Licensed under the Apache License, Version 2.0 (the "License");
306// you may not use this file except in compliance with the License.
307// You may obtain a copy of the License at
308//
309// http://www.apache.org/licenses/LICENSE-2.0
310//
311// Unless required by applicable law or agreed to in writing, software
312// distributed under the License is distributed on an "AS IS" BASIS,
313// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
314// See the License for the specific language governing permissions and
315// limitations under the License.
316// ----------------------------- END-OF-FILE ----------------------------------
317
318/** @} */
319/** @} */
320/** @} */
FunctorAdapter_FunctionPointer< FUNCTION * > Type
Definition bslalg_functoradapter.h:257
Definition bslalg_functoradapter.h:190
Definition bslalg_functoradapter.h:228
CALLABLE_OBJECT Type
This typedef is an alias for the functor.
Definition bslalg_functoradapter.h:234
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_flathashmap.h:1805
Definition bslmf_functionpointertraits.h:153