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