BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formaterror.h
Go to the documentation of this file.
1/// @file bslfmt_formaterror.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formaterror.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATERROR
10#define INCLUDED_BSLFMT_FORMATERROR
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formaterror bslfmt_formaterror
16/// @brief Provide an exception type for format library errors.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formaterror
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formaterror-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formaterror-classes"> Classes </a>
27/// * <a href="#bslfmt_formaterror-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_formaterror-description"> Description </a>
29/// * <a href="#bslfmt_formaterror-usage"> Usage </a>
30/// * <a href="#bslfmt_formaterror-example-indicate-a-formatting-error"> Example: Indicate a Formatting Error </a>
31///
32/// # Purpose {#bslfmt_formaterror-purpose}
33/// Provide an exception type for format library errors.
34///
35/// # Classes {#bslfmt_formaterror-classes}
36///
37/// - bsl::format_error: standard-compliant format library exception type
38///
39/// # Canonical Header {#bslfmt_formaterror-canonical-header}
40/// bsl_format.h
41///
42/// # Description {#bslfmt_formaterror-description}
43/// This component provides an implementation of the C++20 Standard
44/// Library's @ref format_error , providing an exception type thrown in the event of
45/// an error in the formatting library.
46///
47/// Where the standard library `<format>` header is available, this is an alias
48/// to the `std::format_error` type.
49///
50/// This header is not intended to be included directly. Please include
51/// `<bsl_format.h>` to be able to use `bsl::format_error`.
52///
53/// ## Usage {#bslfmt_formaterror-usage}
54///
55///
56/// In this section we show the intended use of this component.
57///
58/// ### Example: Indicate a Formatting Error {#bslfmt_formaterror-example-indicate-a-formatting-error}
59///
60///
61///
62/// Typically a @ref format_error exception would be thrown from the `format` or
63/// `vformat` functions. However, as this is at the very bottom of the
64/// dependency hierarchy the usage example cannot accurately reflect that case.
65///
66/// @code
67/// bool formatErrorCaught = false;
68/// try {
69/// throw bsl::format_error("Error message");
70/// }
71/// catch (const bsl::format_error &exc) {
72/// formatErrorCaught = true;
73/// assert(0 == strcmp(exc.what(), "Error message"));
74/// }
75/// assert(formatErrorCaught);
76/// @endcode
77/// @}
78/** @} */
79/** @} */
80
81/** @addtogroup bsl
82 * @{
83 */
84/** @addtogroup bslfmt
85 * @{
86 */
87/** @addtogroup bslfmt_formaterror
88 * @{
89 */
90
91#include <bslscm_version.h>
92
94#include <bsls_keyword.h>
95
96#include <bslstl_string.h>
97
98#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_FORMAT)
99 #include <format>
100#endif
101
102#include <stdexcept>
103
104
105namespace bslfmt {
106
107 // ==================
108 // class format_error
109 // ==================
110
111#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_FORMAT)
112using std::format_error;
113#else
114/// This class represents exceptions thrown by the formatting library, its main
115/// method of reporting errors during formatting.
116///
117/// See @ref bslfmt_formaterror
118class format_error : public std::runtime_error {
119 public:
120 // CREATORS
121
122 /// Create an object of this type holding the error message given by the
123 /// specified `whatArg`.
124 BSLS_KEYWORD_EXPLICIT format_error(const std::string& whatArg);
125 BSLS_KEYWORD_EXPLICIT format_error(const char *whatArg);
127
128 /// Create an object of this type which is a copy of the specified `other`.
130
131 // MANIPULATORS
132
133 /// Assign to this object the value of the specified `rhs` object, and
134 /// return a non-`const` reference to this object.
136};
137#endif
138
139} // close package namespace
140
141
142namespace bsl {
143
144// Unlike other format-related types and free functions, @ref format_error
145// specifically needs to be promoted into the `bsl` namespace here rather than
146// in `bslfmt_format.h` so that `formatter` partial specializations can be
147// written to reference `bsl::format_error` and thus remain compatible with
148// both the BSL implementation and the standard library implementation.
149
150#if defined(BSLS_LIBRARYFEATURES_HAS_CPP20_FORMAT)
151 using std::format_error;
152#else
153 using BloombergLP::bslfmt::format_error;
154#endif
155
156} // close namespace bsl
157
158
159// ============================================================================
160// INLINE DEFINITIONS
161// ============================================================================
162
163#if !defined(BSLS_LIBRARYFEATURES_HAS_CPP20_FORMAT)
164
165
166namespace bslfmt {
167
168 // ------------------
169 // class format_error
170 // ------------------
171
172// CREATORS
173inline
174format_error::format_error(const std::string& whatArg)
175: std::runtime_error(whatArg)
176{
177}
178
179inline
180format_error::format_error(const char *whatArg)
181: std::runtime_error(whatArg)
182{
183}
184
185inline
187: std::runtime_error(whatArg.c_str())
188{
189}
190
191inline
193: runtime_error(other)
194{
195}
196
197// MANIPULATORS
198inline
201{
202 std::runtime_error& me = *this;
203
204 me = rhs;
205
206 return *this;
207}
208
209} // close package namespace
210
211
212#endif // !defined(BSLS_LIBRARYFEATURES_HAS_CPP20_FORMAT)
213
214#endif // INCLUDED_BSLFMT_FORMATERROR
215
216// ----------------------------------------------------------------------------
217// Copyright 2023 Bloomberg Finance L.P.
218//
219// Licensed under the Apache License, Version 2.0 (the "License");
220// you may not use this file except in compliance with the License.
221// You may obtain a copy of the License at
222//
223// http://www.apache.org/licenses/LICENSE-2.0
224//
225// Unless required by applicable law or agreed to in writing, software
226// distributed under the License is distributed on an "AS IS" BASIS,
227// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
228// See the License for the specific language governing permissions and
229// limitations under the License.
230// ----------------------------- END-OF-FILE ----------------------------------
231
232/** @} */
233/** @} */
234/** @} */
Definition bslstl_string.h:1252
Definition bslfmt_formaterror.h:118
format_error & operator=(const format_error &rhs) BSLS_KEYWORD_NOEXCEPT
Definition bslfmt_formaterror.h:199
BSLS_KEYWORD_EXPLICIT format_error(const std::string &whatArg)
Definition bslfmt_formaterror.h:174
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_EXPLICIT
Definition bsls_keyword.h:683
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
Definition bdlat_valuetypefunctions.h:939
Definition bslfmt_enablestreamedformatter.h:130
Definition bdldfp_decimal.h:5549