BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_badfunctioncall.h
Go to the documentation of this file.
1/// @file bslstl_badfunctioncall.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_badfunctioncall.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_BADFUNCTIONCALL
9#define INCLUDED_BSLSTL_BADFUNCTIONCALL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_badfunctioncall bslstl_badfunctioncall
15/// @brief Provide an exception class thrown by `bsl::function`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_badfunctioncall
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_badfunctioncall-purpose"> Purpose</a>
25/// * <a href="#bslstl_badfunctioncall-classes"> Classes </a>
26/// * <a href="#bslstl_badfunctioncall-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_badfunctioncall-description"> Description </a>
28/// * <a href="#bslstl_badfunctioncall-usage"> Usage </a>
29/// * <a href="#bslstl_badfunctioncall-example-1-a-degenerate-function-wrapper"> Example 1: A Degenerate Function Wrapper </a>
30///
31/// # Purpose {#bslstl_badfunctioncall-purpose}
32/// Provide an exception class thrown by `bsl::function`.
33///
34/// # Classes {#bslstl_badfunctioncall-classes}
35///
36/// - bsl::bad_function_call: exception type thrown by `bsl::function`
37///
38/// # Canonical Header {#bslstl_badfunctioncall-canonical-header}
39/// bsl_functional.h
40///
41/// @see bslstl_function, bslstl_stdexceptionutil
42///
43/// # Description {#bslstl_badfunctioncall-description}
44/// This component provides a `bsl::bad_function_call` exception
45/// class when exceptions are enabled, and nothing otherwise. This exception is
46/// thrown by `bsl::function::operator()` when the function wrapper object has
47/// no target. If compiling as C++11 or later, `bsl::bad_function_call` is an
48/// alias for `std::bad_function_call`.
49///
50/// ## Usage {#bslstl_badfunctioncall-usage}
51///
52///
53/// This section illustrates intended use of this component.
54///
55/// ### Example 1: A Degenerate Function Wrapper {#bslstl_badfunctioncall-example-1-a-degenerate-function-wrapper}
56///
57///
58/// Suppose we want to write a null functor, the functional equivalent of a
59/// null pointer, which throws an exception when invoked. We can use the
60/// `bsl::bad_function_call` exception type as our common vocabulary for
61/// reporting the invocation of functors when they are not in a callable state.
62///
63/// First, we define the class and give it a call operator that unconditionally
64/// throws:
65/// @code
66/// struct EmptyFunction {
67/// void operator()() const { BSLS_THROW(bsl::bad_function_call()); }
68/// };
69/// @endcode
70/// Then, we invoke the call operator inside a `try` block and confirm that the
71/// expected exception type is caught:
72/// @code
73/// bool caught = false;
74/// try {
75/// EmptyFunction()();
76/// }
77/// catch (const bsl::bad_function_call&) {
78/// caught = true;
79/// }
80/// assert(caught);
81/// @endcode
82/// @}
83/** @} */
84/** @} */
85
86/** @addtogroup bsl
87 * @{
88 */
89/** @addtogroup bslstl
90 * @{
91 */
92/** @addtogroup bslstl_badfunctioncall
93 * @{
94 */
95
96#include <bslscm_version.h>
97
98#include <bsls_buildtarget.h>
100#include <bsls_exceptionutil.h>
101
102#ifdef BDE_BUILD_TARGET_EXC
103
104#include <exception>
105#include <functional> // for `std::bad_function_call` in C++11
106
107#if BSLS_COMPILERFEATURES_FULL_CPP11
108namespace bsl {
109 using std::bad_function_call;
110} // close namespace bsl
111#else
112namespace bsl {
113 // =======================
114 // class bad_function_call
115 // =======================
116
117class bad_function_call : public std::exception {
118 public:
119 // CREATORS
120
121 /// Create a @ref bad_function_call object.
122 /// \note Note that this function is
123 /// explicitly user-declared, to make it simple to declare `const`
124 /// objects of this type.
125 bad_function_call();
126
127 // ACCESSORS
128
129 /// Return a pointer to the string literal "bad_function_call", with a storage duration of the lifetime of the program.
130 ///
131 /// \note Note that the
132 /// caller should *not* attempt to free this memory.
133 const char *what() const BSLS_EXCEPTION_VIRTUAL_NOTHROW;
134};
135} // close namespace bsl
136#endif // BSLS_COMPILERFEATURES_FULL_CPP11
137#endif // BDE_BUILD_TARGET_EXC
138#endif
139
140// ----------------------------------------------------------------------------
141// Copyright 2020 Bloomberg Finance L.P.
142//
143// Licensed under the Apache License, Version 2.0 (the "License");
144// you may not use this file except in compliance with the License.
145// You may obtain a copy of the License at
146//
147// http://www.apache.org/licenses/LICENSE-2.0
148//
149// Unless required by applicable law or agreed to in writing, software
150// distributed under the License is distributed on an "AS IS" BASIS,
151// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152// See the License for the specific language governing permissions and
153// limitations under the License.
154// ----------------------------- END-OF-FILE ----------------------------------
155
156/** @} */
157/** @} */
158/** @} */
#define BSLS_EXCEPTION_VIRTUAL_NOTHROW
Definition bsls_exceptionutil.h:402
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939