BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlat_enumutil.h
Go to the documentation of this file.
1/// @file bdlat_enumutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlat_enumutil.h -*-C++-*-
8#ifndef INCLUDED_BDLAT_ENUMUTIL
9#define INCLUDED_BDLAT_ENUMUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlat_enumutil bdlat_enumutil
15/// @brief Provide functions for decoding enumerations with fallback values.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlat
19/// @{
20/// @addtogroup bdlat_enumutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlat_enumutil-purpose"> Purpose</a>
25/// * <a href="#bdlat_enumutil-classes"> Classes </a>
26/// * <a href="#bdlat_enumutil-description"> Description </a>
27/// * <a href="#bdlat_enumutil-usage"> Usage </a>
28///
29/// # Purpose {#bdlat_enumutil-purpose}
30/// Provide functions for decoding enumerations with fallback values.
31///
32/// # Classes {#bdlat_enumutil-classes}
33///
34/// - bdlat::EnumUtil: namespace for utility functions for decoding enumerations
35///
36/// @see bdlat_enumfunctions
37///
38/// # Description {#bdlat_enumutil-description}
39/// This component provides a `struct`, `bdlat::EnumUtil`, with two
40/// utility function templates, `fromIntOrFallbackIfEnabled` and
41/// `fromStringOrFallbackIfEnabled`, which respectively attempt to decode a
42/// `bdlat` "enumeration" type from the integral or string representation.
43/// However, unlike the decoding functions in the `bdlat_EnumFunctions`
44/// namespace, when the functions in this component are given values that do not
45/// correspond to any enumerator, they attempt to set the result to the
46/// "fallback" enumerator value, if possible, instead of failing.
47///
48/// ## Usage {#bdlat_enumutil-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// Suppose you have a C++ `enum` type called `ImageType` whose enumerators
54/// represent supported formats for image files, and it exposes "enumeration"
55/// behavior as described in @ref bdlat_enumfunctions :
56/// @code
57/// #include <bdlat_enumfunctions.h>
58/// #include <bdlb_string.h>
59/// #include <bsl_string.h>
60///
61/// namespace BloombergLP {
62/// namespace mine {
63///
64/// enum ImageType {
65/// JPG = 0,
66/// PNG = 1,
67/// GIF = 2,
68/// UNKNOWN = 100
69/// };
70///
71/// // MANIPULATORS
72///
73/// int bdlat_enumFromInt(ImageType* result, int number);
74///
75/// int bdlat_enumFromString(ImageType *result,
76/// const char *string,
77/// int stringLength);
78///
79/// int bdlat_enumMakeFallback(ImageType *result);
80///
81/// // ACCESSORS
82///
83/// void bdlat_enumToInt(int *result, const ImageType& value);
84///
85/// void bdlat_enumToString(bsl::string *result, const ImageType& value);
86///
87/// bool bdlat_enumHasFallback(const ImageType&);
88///
89/// bool bdlat_enumIsFallback(const ImageType& value);
90///
91/// } // close namespace mine
92/// @endcode
93/// Next, we provide definitions for the `bdlat_enum*` customization point
94/// function overloads:
95/// @code
96/// // MANIPULATORS
97///
98/// inline
99/// int mine::bdlat_enumFromInt(ImageType *result, int number)
100/// {
101/// enum { SUCCESS = 0, NOT_FOUND = -1 };
102///
103/// switch (number) {
104/// case JPG: {
105/// *result = JPG;
106/// return SUCCESS;
107/// }
108/// case PNG: {
109/// *result = PNG;
110/// return SUCCESS;
111/// }
112/// case GIF: {
113/// *result = GIF;
114/// return SUCCESS;
115/// }
116/// case UNKNOWN: {
117/// *result = UNKNOWN;
118/// return SUCCESS;
119/// }
120/// default: {
121/// return NOT_FOUND;
122/// }
123/// }
124/// }
125///
126/// inline
127/// int mine::bdlat_enumFromString(ImageType *result,
128/// const char *string,
129/// int stringLength)
130/// {
131/// enum { SUCCESS = 0, NOT_FOUND = -1 };
132///
133/// if (bdlb::String::areEqualCaseless("jpg",
134/// string,
135/// stringLength)) {
136/// *result = JPG;
137/// return SUCCESS;
138/// }
139///
140/// if (bdlb::String::areEqualCaseless("png",
141/// string,
142/// stringLength)) {
143/// *result = PNG;
144/// return SUCCESS;
145/// }
146///
147/// if (bdlb::String::areEqualCaseless("gif",
148/// string,
149/// stringLength)) {
150/// *result = GIF;
151/// return SUCCESS;
152/// }
153/// if (bdlb::String::areEqualCaseless("unknown",
154/// string,
155/// stringLength)) {
156/// *result = UNKNOWN;
157/// return SUCCESS;
158/// }
159///
160/// return NOT_FOUND;
161/// }
162///
163/// inline
164/// int mine::bdlat_enumMakeFallback(ImageType *result)
165/// {
166/// *result = UNKNOWN;
167/// return 0;
168/// }
169///
170/// // ACCESSORS
171///
172/// inline
173/// void mine::bdlat_enumToInt(int *result, const ImageType& value)
174/// {
175/// *result = static_cast<int>(value);
176/// }
177///
178/// inline
179/// void mine::bdlat_enumToString(bsl::string *result, const ImageType& value)
180/// {
181/// switch (value) {
182/// case JPG: {
183/// *result = "JPG";
184/// } break;
185/// case PNG: {
186/// *result = "PNG";
187/// } break;
188/// case GIF: {
189/// *result = "GIF";
190/// } break;
191/// case UNKNOWN: {
192/// *result = "UNKNOWN";
193/// } break;
194/// default: {
195/// *result = "INVALID";
196/// } break;
197/// }
198/// }
199///
200/// inline
201/// bool mine::bdlat_enumHasFallback(const ImageType&)
202/// {
203/// return true;
204/// }
205///
206/// inline
207/// bool mine::bdlat_enumIsFallback(const ImageType& value)
208/// {
209/// return value == UNKNOWN;
210/// }
211/// @endcode
212/// To complete the implementation of `mine::ImageType` as an "enumeration" type
213/// with fallback enumerator recognized by the `bdlat` framework, we specialize
214/// the necessary traits:
215/// @code
216/// namespace bdlat_EnumFunctions {
217/// template <>
218/// struct IsEnumeration<mine::ImageType> : bsl::true_type {
219/// };
220/// template <>
221/// struct HasFallbackEnumerator<mine::ImageType> : bsl::true_type {
222/// };
223/// } // close namespace bdlat_EnumFunctions
224/// } // close enterprise namespace
225/// @endcode
226/// We can now use the methods in `EnumUtil` to decode integral and string
227/// values into `mine::ImageType` values, falling back to the `mine::UNKNOWN`
228/// enumerator value when the integral or string value does not correspond to
229/// any enumerator:
230/// @code
231/// void usageExample()
232/// {
233/// using namespace BloombergLP;
234///
235/// mine::ImageType imageType;
236/// int rc;
237///
238/// rc = bdlat::EnumUtil::fromIntOrFallbackIfEnabled(&imageType, 1);
239/// assert(0 == rc);
240/// assert(mine::PNG == imageType);
241///
242/// rc = bdlat::EnumUtil::fromIntOrFallbackIfEnabled(&imageType, 4);
243/// assert(0 == rc);
244/// assert(mine::UNKNOWN == imageType);
245///
246/// rc = bdlat::EnumUtil::fromStringOrFallbackIfEnabled(&imageType,
247/// "GIF",
248/// 3);
249/// assert(0 == rc);
250/// assert(mine::GIF == imageType);
251///
252/// rc = bdlat::EnumUtil::fromStringOrFallbackIfEnabled(&imageType,
253/// "WEBP",
254/// 4);
255/// assert(0 == rc);
256/// assert(mine::UNKNOWN == imageType);
257/// }
258/// @endcode
259/// Note that the methods in `EnumUtil` may also be used with `bdlat`
260/// "enumeration" types that do not support a fallback enumerator. In such
261/// cases, they can fail by returning a non-zero error code.
262/// @}
263/** @} */
264/** @} */
265
266/** @addtogroup bdl
267 * @{
268 */
269/** @addtogroup bdlat
270 * @{
271 */
272/** @addtogroup bdlat_enumutil
273 * @{
274 */
275
276#include <bdlat_enumfunctions.h>
277
278#include <bslmf_assert.h>
280
281#include <bsls_platform.h>
282
283
284namespace bdlat {
285
286 // ===============
287 // struct EnumUtil
288 // ===============
289
290/// This `struct` provides a namespace for functions that decode `bdlat`
291/// "enumerations" from integral and string representations, possibly to the
292/// "fallback" value (if one exists).
293struct EnumUtil {
294
295 // CLASS METHODS
296
297 /// Load into the specified `result` the enumerator matching the
298 /// specified `number` and return 0 if such an enumerator exists.
299 /// Otherwise, if `result` supports a fallback enumerator, load into
300 /// `result` the fallback enumerator value and return 0. Otherwise,
301 /// return a non-zero value with no effect on `result`. The behavior is
302 /// undefined unless `TYPE` is a `bdlat` enumeration type.
303 template <class TYPE>
304 static int fromIntOrFallbackIfEnabled(TYPE *result, int number);
305
306 /// Load into the specified `result` the enumerator matching the
307 /// specified `string` of the specified `stringLength` and return 0 if
308 /// such an enumerator exists. Otherwise, if `result` supports a
309 /// fallback enumerator, load into `result` the fallback enumerator
310 /// value and return 0. Otherwise, return a non-zero value with no
311 /// effect on `result`. The behavior is undefined unless `TYPE` is a
312 /// `bdlat` enumeration type.
313 template <class TYPE>
314 static int fromStringOrFallbackIfEnabled(TYPE *result,
315 const char *string,
316 int stringLength);
317};
318
319// ============================================================================
320// INLINE DEFINITIONS
321// ============================================================================
322
323 // ---------------
324 // struct EnumUtil
325 // ---------------
326
327// CLASS METHODS
328template <class TYPE>
329int EnumUtil::fromIntOrFallbackIfEnabled(TYPE *result, int number)
330{
331#if !defined(BSLS_PLATFORM_CMP_SUN)
333#endif
334
335 if (0 == bdlat_EnumFunctions::fromInt(result, number)) {
336 return 0; // RETURN
337 }
338
340}
341
342template <class TYPE>
344 const char *string,
345 int stringLength)
346{
347#if !defined(BSLS_PLATFORM_CMP_SUN)
349#endif
350
351 if (0 == bdlat_EnumFunctions::fromString(result, string, stringLength)) {
352 return 0; // RETURN
353 }
354
356}
357
358} // close package namespace
359
360
361#endif // INCLUDED_BDLAT_ENUMUTIL
362
363// ----------------------------------------------------------------------------
364// Copyright 2022 Bloomberg Finance L.P.
365//
366// Licensed under the Apache License, Version 2.0 (the "License");
367// you may not use this file except in compliance with the License.
368// You may obtain a copy of the License at
369//
370// http://www.apache.org/licenses/LICENSE-2.0
371//
372// Unless required by applicable law or agreed to in writing, software
373// distributed under the License is distributed on an "AS IS" BASIS,
374// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
375// See the License for the specific language governing permissions and
376// limitations under the License.
377// ----------------------------- END-OF-FILE ----------------------------------
378
379/** @} */
380/** @} */
381/** @} */
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
int fromString(TYPE *result, const char *string, int stringLength)
int fromInt(TYPE *result, int number)
int makeFallback(TYPE *result)
Definition bdlat_arrayutil.h:198
Definition bdlat_enumutil.h:293
static int fromIntOrFallbackIfEnabled(TYPE *result, int number)
Definition bdlat_enumutil.h:329
static int fromStringOrFallbackIfEnabled(TYPE *result, const char *string, int stringLength)
Definition bdlat_enumutil.h:343
Definition bdlat_enumfunctions.h:403