BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstp_exfunctional.h
Go to the documentation of this file.
1/// @file bslstp_exfunctional.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstp_exfunctional.h -*-C++-*-
8#ifndef INCLUDED_BSLSTP_EXFUNCTIONAL
9#define INCLUDED_BSLSTP_EXFUNCTIONAL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstp_exfunctional bslstp_exfunctional
15/// @brief Provide a namespace for functional extensions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstp
19/// @{
20/// @addtogroup bslstp_exfunctional
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstp_exfunctional-purpose"> Purpose</a>
25/// * <a href="#bslstp_exfunctional-classes"> Classes </a>
26/// * <a href="#bslstp_exfunctional-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstp_exfunctional-description"> Description </a>
28/// * <a href="#bslstp_exfunctional-usage"> Usage </a>
29///
30/// # Purpose {#bslstp_exfunctional-purpose}
31/// Provide a namespace for functional extensions.
32///
33/// @deprecated Do not use directly.
34///
35/// # Classes {#bslstp_exfunctional-classes}
36///
37/// - bsl::compose1: create an unary function object adaptor
38/// - bsl::compose2: create a binary function object adaptor
39/// - bsl::select1st: select `first` value of a pair
40/// - bsl::select2nd: select `second` value of a pair
41///
42/// # Canonical Header {#bslstp_exfunctional-canonical-header}
43/// bsl_functional.h
44///
45/// @see bsl_functional
46///
47/// # Description {#bslstp_exfunctional-description}
48/// This component is for internal use only. Please include
49/// `<bsl_functional.h>` directly. This component provides a namespace for
50/// functional extensions that are not specified in the C++ standard. This
51/// provides backward compatibility for code using extensions that STLPort
52/// provides.
53///
54/// Note that the classes in this component are based on STLPort's
55/// implementation, with copyright notice as follows:
56/// @code
57/// -----------------------------------------------------------------------------
58///
59/// Copyright (c) 1994
60/// Hewlett-Packard Company
61///
62/// Copyright (c) 1996-1998
63/// Silicon Graphics Computer Systems, Inc.
64///
65/// Copyright (c) 1997
66/// Moscow Center for SPARC Technology
67///
68/// Copyright (c) 1999
69/// Boris Fomitchev
70///
71/// This material is provided "as is", with absolutely no warranty expressed
72/// or implied. Any use is at your own risk.
73///
74/// Permission to use or copy this software for any purpose is hereby granted
75/// without fee, provided the above notices are retained on all copies.
76/// Permission to modify the code and to distribute modified code is granted,
77/// provided the above notices are retained, and a notice that the code was
78/// modified is included with the above copyright notice.
79/// -----------------------------------------------------------------------------
80/// @endcode
81///
82/// ## Usage {#bslstp_exfunctional-usage}
83///
84///
85/// This component is for internal use only.
86/// @}
87/** @} */
88/** @} */
89
90/** @addtogroup bsl
91 * @{
92 */
93/** @addtogroup bslstp
94 * @{
95 */
96/** @addtogroup bslstp_exfunctional
97 * @{
98 */
99
100#ifdef BDE_OPENSOURCE_PUBLICATION // STP
101#error "bslstp_exfunctional is not for publication"
102#endif
103
104#include <bslscm_version.h>
105
108
109#include <bslstl_equalto.h>
110
113
114#include <cstring> // for 'std::strcmp'
115#include <functional> // for 'std::unary_function'
116
117#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
118# include <bslalg_typetraits.h>
119# include <bsls_nativestd.h>
120# include <functional>
121#endif
122
123namespace bsl {
124
125 // ========================
126 // class bsl::unary_compose
127 // ========================
128
129/// This class provides a function object adaptor for two other functors.
130/// When given two adaptable unary functions `f` and `g`, and when `g`s
131/// return type is convertible to `f`s argument type, this class can be used
132/// to create a function object `h` that is equivalent to `f(g(x))`.
133template <class OPERATION1, class OPERATION2>
135{
136 public:
137 // public type names
138 typedef typename OPERATION2::argument_type argument_type;
139 typedef typename OPERATION1::result_type result_type;
140
141 protected:
142 // DATA
143 OPERATION1 d_fn1; // function object 'f', in 'f(g(x))'
144 OPERATION2 d_fn2; // function object 'g', in 'f(g(x))'
145
146 public:
147 // ACCESSORS
148 unary_compose(const OPERATION1& x, const OPERATION2& y)
149 : d_fn1(x)
150 , d_fn2(y) {}
151
153 return d_fn1(d_fn2(x));
154 }
155
157 return d_fn1(d_fn2(x));
158 }
159};
160
161 // =========================
162 // class bsl::binary_compose
163 // =========================
164
165/// This class provides a function object adaptor for three other functors.
166/// When given two adaptable unary functions `g1` and `g2`, and a third
167/// adaptable binary function `f`, and if `g1` and `g2`s argument type is
168/// convertible to `f`s argument type, this class can be used to create a
169/// function object `h` that is equivalent to `f(g1(x), g2(x))`.
170template <class OPERATION1, class OPERATION2, class OPERATION3>
172{
173 public:
174 // public type names
175 typedef typename OPERATION2::argument_type argument_type;
176 typedef typename OPERATION1::result_type result_type;
177
178 protected:
179 // DATA
180 OPERATION1 d_fn1; // function object 'f', in 'f(g1(x), g2(x))'
181 OPERATION2 d_fn2; // function object 'g1', in 'f(g1(x), g2(x))'
182 OPERATION3 d_fn3; // function object 'g2', in 'f(g1(x), g2(x))'
183
184 public:
185 // CREATORS
186 binary_compose(const OPERATION1& fn1,
187 const OPERATION2& fn2,
188 const OPERATION3& fn3)
189 : d_fn1(fn1)
190 , d_fn2(fn2)
191 , d_fn3(fn3) {}
192
193 // ACCESSORS
195 {
196 return d_fn1(d_fn2(x), d_fn3(x));
197 }
198
200 {
201 return d_fn1(d_fn2(x), d_fn3(x));
202 }
203};
204
205
206 // ====================
207 // class bsl::select1st
208 // ====================
209
210/// public type names
211template <class PAIR>
213{
214 typedef PAIR argument_type;
215 typedef typename PAIR::first_type result_type;
216
217 // TRAITS
219
220 // ACCESSORS
221 const typename PAIR::first_type& operator()(const PAIR& x) const
222 {
223 return x.first;
224 }
225};
226
227 // ====================
228 // class bsl::select2nd
229 // ====================
230
231/// public type names
232template <class PAIR>
234{
235 typedef PAIR argument_type;
236 typedef typename PAIR::second_type result_type;
237
238 // TRAITS
240
241 // ACCESSORS
242 const typename PAIR::second_type& operator()(const PAIR& x) const
243 {
244 return x.second;
245 }
246};
247
248// FREE FUNCTIONS
249
250/// Return an @ref unary_compose function object constructed using the
251/// specified `fn1` and `fn2` unary functions. The returned function object
252/// is equivalent to `fn1(fn2(x))`.
253template <class OPERATION1, class OPERATION2>
254inline
255unary_compose<OPERATION1, OPERATION2>
256compose1(const OPERATION1& fn1, const OPERATION2& fn2)
257{
259}
260
261/// Return a @ref binary_compose function object constructed using the
262/// specified `fn1` binary function, and the specified `fn2` and `fn3` unary
263/// functions. The returned function object is equivalent to
264/// `fn1(fn2(x), fn3(x))`.
265template <class OPERATION1, class OPERATION2, class OPERATION3>
266inline
267binary_compose<OPERATION1, OPERATION2, OPERATION3>
268compose2(const OPERATION1& fn1,
269 const OPERATION2& fn2,
270 const OPERATION3& fn3)
271{
273}
274
275 // ===========================
276 // class bsl::StringComparator
277 // ===========================
278
279/// This class is a functor that provides comparison between two
280/// `const char *` strings.
282{
283 // TRAITS
286
287 // ACCESSORS
288
289 /// Return `true` if the specified strings `a` and `b` have the same
290 /// value, and return `false` otherwise. Two strings have the same
291 /// value if the result of `std::strcmp` returns 0.
292 bool operator()(const char *a, const char *b) const
293 {
294
295 return 0 == std::strcmp(a, b);
296 }
297};
298
299 // =============================
300 // class bsl::ComparatorSelector
301 // =============================
302
303/// This meta-function selects the appropriate implementation for comparing
304/// the parameterized `TYPE`. This generic template uses the
305/// `std::equal_to` functor.
306template <class HASH_KEY>
308{
309 // TYPES
311};
312
313/// This meta-function is specialized for `const char *`, and uses the
314/// `StringComparator` functor to compare the string values.
315template <>
316struct ComparatorSelector<const char *>
317{
318 // TYPES
320};
321
322} // close namespace bsl
323
324
325namespace bslstp {
326
327 // ======================
328 // class bslstp::Identity
329 // ======================
330
331/// An identity function.
332///
333/// See @ref bslstp_exfunctional
334template <class TYPE>
335struct Identity {
336
337 // TYPES
338 typedef TYPE argument_type;
339 typedef TYPE result_type;
340
341 // TRAITS
343
344 // ACCESSORS
345
346 /// Return a const reference to the specified `x`.
347 const TYPE& operator()(const TYPE& x) const;
348};
349
350//=============================================================================
351// TEMPLATE AND INLINE FUNCTION DEFINITIONS
352//=============================================================================
353
354template <class TYPE>
355inline
356const TYPE& Identity<TYPE>::operator()(const TYPE& x) const
357{
358 return x;
359}
360
361} // close package namespace
362
363
364#endif
365
366// ----------------------------------------------------------------------------
367// Copyright 2013 Bloomberg Finance L.P.
368//
369// Licensed under the Apache License, Version 2.0 (the "License");
370// you may not use this file except in compliance with the License.
371// You may obtain a copy of the License at
372//
373// http://www.apache.org/licenses/LICENSE-2.0
374//
375// Unless required by applicable law or agreed to in writing, software
376// distributed under the License is distributed on an "AS IS" BASIS,
377// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
378// See the License for the specific language governing permissions and
379// limitations under the License.
380// ----------------------------- END-OF-FILE ----------------------------------
381
382/** @} */
383/** @} */
384/** @} */
Definition bslstp_exfunctional.h:172
OPERATION2 d_fn2
Definition bslstp_exfunctional.h:181
OPERATION3 d_fn3
Definition bslstp_exfunctional.h:182
result_type operator()(argument_type &x) const
Definition bslstp_exfunctional.h:199
result_type operator()(const argument_type &x) const
Definition bslstp_exfunctional.h:194
OPERATION1::result_type result_type
Definition bslstp_exfunctional.h:176
OPERATION1 d_fn1
Definition bslstp_exfunctional.h:180
binary_compose(const OPERATION1 &fn1, const OPERATION2 &fn2, const OPERATION3 &fn3)
Definition bslstp_exfunctional.h:186
OPERATION2::argument_type argument_type
Definition bslstp_exfunctional.h:175
Definition bslstp_exfunctional.h:135
OPERATION2 d_fn2
Definition bslstp_exfunctional.h:144
OPERATION1 d_fn1
Definition bslstp_exfunctional.h:143
result_type operator()(const argument_type &x) const
Definition bslstp_exfunctional.h:152
OPERATION2::argument_type argument_type
Definition bslstp_exfunctional.h:138
result_type operator()(argument_type &x) const
Definition bslstp_exfunctional.h:156
OPERATION1::result_type result_type
Definition bslstp_exfunctional.h:139
unary_compose(const OPERATION1 &x, const OPERATION2 &y)
Definition bslstp_exfunctional.h:148
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
unary_compose< OPERATION1, OPERATION2 > compose1(const OPERATION1 &fn1, const OPERATION2 &fn2)
Definition bslstp_exfunctional.h:256
binary_compose< OPERATION1, OPERATION2, OPERATION3 > compose2(const OPERATION1 &fn1, const OPERATION2 &fn2, const OPERATION3 &fn3)
Definition bslstp_exfunctional.h:268
Definition bslstp_exfunctional.h:325
StringComparator Type
Definition bslstp_exfunctional.h:319
Definition bslstp_exfunctional.h:308
bsl::equal_to< HASH_KEY > Type
Definition bslstp_exfunctional.h:310
Definition bslstp_exfunctional.h:282
bool operator()(const char *a, const char *b) const
Definition bslstp_exfunctional.h:292
BSLMF_NESTED_TRAIT_DECLARATION(StringComparator, bsl::is_trivially_copyable)
Definition bslstl_equalto.h:316
Definition bslmf_istriviallycopyable.h:324
public type names
Definition bslstp_exfunctional.h:213
const PAIR::first_type & operator()(const PAIR &x) const
Definition bslstp_exfunctional.h:221
BSLMF_NESTED_TRAIT_DECLARATION(select1st, bsl::is_trivially_copyable)
PAIR argument_type
Definition bslstp_exfunctional.h:214
PAIR::first_type result_type
Definition bslstp_exfunctional.h:215
public type names
Definition bslstp_exfunctional.h:234
const PAIR::second_type & operator()(const PAIR &x) const
Definition bslstp_exfunctional.h:242
PAIR argument_type
Definition bslstp_exfunctional.h:235
PAIR::second_type result_type
Definition bslstp_exfunctional.h:236
BSLMF_NESTED_TRAIT_DECLARATION(select2nd, bsl::is_trivially_copyable)
Definition bslstp_exfunctional.h:335
TYPE argument_type
Definition bslstp_exfunctional.h:338
TYPE result_type
Definition bslstp_exfunctional.h:339
BSLMF_NESTED_TRAIT_DECLARATION(Identity, bsl::is_trivially_copyable)
const TYPE & operator()(const TYPE &x) const
Return a const reference to the specified x.
Definition bslstp_exfunctional.h:356