BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlar_enumref.h
Go to the documentation of this file.
1/// @file bdlar_enumref.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlar_enumref.h -*-C++-*-
8#ifndef INCLUDED_BDLAR_ENUMREF
9#define INCLUDED_BDLAR_ENUMREF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlar_enumref bdlar_enumref
15/// @brief Provide ...
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlar
19/// @{
20/// @addtogroup bdlar_enumref
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlar_enumref-purpose"> Purpose</a>
25/// * <a href="#bdlar_enumref-classes"> Classes </a>
26/// * <a href="#bdlar_enumref-description"> Description </a>
27///
28/// # Purpose {#bdlar_enumref-purpose}
29/// Provide ...
30///
31/// # Classes {#bdlar_enumref-classes}
32///
33/// - bdlar::EnumRef: ...
34/// - bdlar::EnumConstRef: ...
35///
36/// # Description {#bdlar_enumref-description}
37/// This component provides ...
38///
39/// @}
40/** @} */
41/** @} */
42
43/** @addtogroup bdl
44 * @{
45 */
46/** @addtogroup bdlar
47 * @{
48 */
49/** @addtogroup bdlar_enumref
50 * @{
51 */
52
53#include <bdlar_enumvtable.h>
55#include <bdlar_typecategory.h>
56
58
59#include <bsl_type_traits.h>
60
61
62namespace bdlar {
63
64 // =============
65 // class EnumRef
66 // =============
67
68class EnumRef {
69 // DATA
70 void *d_object_p;
71 const EnumVtable *d_vtable_p;
72
73 public:
74 // CREATORS
75
76 /// Create an `EnumRef` object that type erases the supplied `enumeration`
77 /// object.
78 template <class t_ENUM>
79 explicit EnumRef(t_ENUM& enumeration,
80 typename bsl::enable_if<
81 IsEnum<t_ENUM>::value>::type * = 0);
82
83 /// Create an `EnumRef` object.
84 /// \pre The behaviour is undefined unless
85 /// `objectAddress` and `vtable` point to the valid objects.
87
88 // MANIPULATORS
89
90 /// Assign the enumerator value matching the specified `number`. Return 0
91 /// on success, and a non-zero value with no effect on the object if
92 /// `number` does not match any enumerator.
93 int fromInt(int number) const;
94
95 /// Assign the enumerator value matching the specified `string` of the
96 /// specified `stringLength`. Return 0 on success, and a non-zero value
97 /// with no effect on the object if `string` and `stringLength` do not
98 /// match any enumerator.
99 int fromString(const char *string, int stringLength) const;
100
101 /// Assign the fallback enumerator value. Return 0 on success, and a
102 /// non-zero value with no effect on the object if it does not have a
103 /// fallback enumerator.
104 int makeFallback() const;
105
106 /// Reset the referred object to the default value.
107 void reset() const;
108
109 // ACCESSORS
110
111 /// Return a pointer to the vtable.
112 const EnumVtable *vtable() const;
113
114 /// Return `true` if the specified enumerator held by this object supports
115 /// a fallback enumerator, and `false` otherwise.
116 bool hasFallback() const;
117
118 /// Return `true` if the specified enumerator held by this object is equal
119 /// to a fallback enumerator, and `false` otherwise.
120 bool isFallback() const;
121
122 /// Return a pointer to the wrapped object.
123 void *objectAddress() const;
124
125 /// Load into the specified `result` the integer representation of the
126 /// enumerator value held by this object.
127 void toInt(int *result) const;
128
129 /// Load into the specified `result` the string representation of the
130 /// enumerator value held by this object.
131 void toString(bsl::string *result) const;
132};
133
134 // ==================
135 // class EnumConstRef
136 // ==================
137
139 // DATA
140 const void *d_object_p;
141 const EnumConstVtable *d_vtable_p;
142
143 public:
144 // CREATORS
145
146 /// Create an `EnumConstRef` object that type erases the supplied
147 /// `enumeration` object.
148 template <class t_ENUM>
149 explicit EnumConstRef(const t_ENUM& enumeration,
150 typename bsl::enable_if<
151 IsEnum<t_ENUM>::value>::type * = 0);
152
153 /// Create an `EnumConstRef` object.
154 /// \pre The behaviour is undefined unless
155 /// `objectAddress` and `vtable` point to the valid objects.
157
158 /// Create an `EnumConstRef` object that wraps the same object as the
159 /// specified `object`.
160 EnumConstRef(const EnumRef& object); // IMPLICIT
161
162 // ACCESSORS
163
164 /// Return a pointer to the vtable.
165 const EnumConstVtable *vtable() const;
166
167 /// Return `true` if the specified enumerator held by this object supports
168 /// a fallback enumerator, and `false` otherwise.
169 bool hasFallback() const;
170
171 /// Return `true` if the specified enumerator held by this object is equal
172 /// to a fallback enumerator, and `false` otherwise.
173 bool isFallback() const;
174
175 /// Return a pointer to the wrapped object.
176 const void *objectAddress() const;
177
178 /// Load into the specified `result` the integer representation of the
179 /// enumerator value held by this object.
180 void toInt(int *result) const;
181
182 /// Load into the specified `result` the string representation of the
183 /// enumerator value held by this object.
184 void toString(bsl::string *result) const;
185
186 /// Return the `bdlat_TypeName::xsdName` value for the underlying object.
187 const char *xsdName(int format) const;
188};
189
190// ============================================================================
191// INLINE DEFINITIONS
192// ============================================================================
193
194 // -------------
195 // class EnumRef
196 // -------------
197
198// CREATORS
199template <class t_ENUM>
200inline
201EnumRef::EnumRef(t_ENUM& enumeration,
202 typename bsl::enable_if<
203 IsEnum<t_ENUM>::value>::type *)
204: d_object_p(&enumeration)
205, d_vtable_p(EnumVtableUtil::getVtable<t_ENUM>())
206{
207}
208
209inline
210EnumRef::EnumRef(void *objectAddress, const EnumVtable *vtable)
211: d_object_p(objectAddress)
212, d_vtable_p(vtable)
213{
214}
215
216// MANIPULATORS
217inline
218int EnumRef::fromInt(int number) const
219{
220 return d_vtable_p->d_fromInt_fp(d_object_p, number);
221}
222
223inline
224int EnumRef::fromString(const char *string, int stringLength) const
225{
226 return d_vtable_p->d_fromString_fp(d_object_p, string, stringLength);
227}
228
229inline
231{
232 return d_vtable_p->d_makeFallback_fp(d_object_p);
233}
234
235inline
236void EnumRef::reset() const
237{
238 return d_vtable_p->d_reset_fp(d_object_p);
239}
240
241// ACCESSORS
242inline
244{
245 return d_vtable_p;
246}
247
248inline
250{
251 return d_vtable_p->d_const.d_hasFallback_fp(d_object_p);
252}
253
254inline
256{
257 return d_vtable_p->d_const.d_isFallback_fp(d_object_p);
258}
259
260inline
262{
263 return d_object_p;
264}
265
266inline
267void EnumRef::toInt(int *result) const
268{
269 d_vtable_p->d_const.d_toInt_fp(result, d_object_p);
270}
271
272inline
274{
275 d_vtable_p->d_const.d_toString_fp(result, d_object_p);
276}
277
278// FREE FUNCTIONS
279// Customization-point functions (`bdlat_EnumFunctions`)
280
281inline
282int bdlat_enumFromInt(EnumRef *result, int number)
283{
284 return result->fromInt(number);
285}
286
287inline
288int bdlat_enumFromString(EnumRef *result, const char *string, int stringLength)
289{
290 return result->fromString(string, stringLength);
291}
292
293inline
295{
296 return result->makeFallback();
297}
298
299inline
301{
302 return value.hasFallback();
303}
304
305inline
307{
308 return value.isFallback();
309}
310
311inline
312void bdlat_enumToInt(int *result, const EnumRef& value)
313{
314 value.toInt(result);
315}
316
317inline
318void bdlat_enumToString(bsl::string *result, const EnumRef& value)
319{
320 value.toString(result);
321}
322
323inline
325{
326 ref->reset();
327}
328
329 // ------------------
330 // class EnumConstRef
331 // ------------------
332
333// CREATORS
334template <class t_ENUM>
335inline
336EnumConstRef::EnumConstRef(const t_ENUM& enumeration,
337 typename bsl::enable_if<
338 IsEnum<t_ENUM>::value>::type *)
339: d_object_p(&enumeration)
340, d_vtable_p(EnumVtableUtil::getConstVtable<t_ENUM>())
341{
342}
343
344inline
345EnumConstRef::EnumConstRef(const void *objectAddress,
346 const EnumConstVtable *vtable)
347: d_object_p(objectAddress)
348, d_vtable_p(vtable)
349{
350}
351
352inline
354: d_object_p(object.objectAddress())
355, d_vtable_p(&object.vtable()->d_const)
356{
357}
358
359// ACCESSORS
360inline
362{
363 return d_vtable_p;
364}
365
366inline
368{
369 return d_vtable_p->d_hasFallback_fp(d_object_p);
370}
371
372inline
374{
375 return d_vtable_p->d_isFallback_fp(d_object_p);
376}
377
378inline
380{
381 return d_object_p;
382}
383
384inline
385void EnumConstRef::toInt(int *result) const
386{
387 d_vtable_p->d_toInt_fp(result, d_object_p);
388}
389
390inline
392{
393 d_vtable_p->d_toString_fp(result, d_object_p);
394}
395
396inline
397const char *EnumConstRef::xsdName(int format) const
398{
399 return d_vtable_p->d_xsdName_fp(d_object_p, format);
400}
401
402// FREE FUNCTIONS
403// Customization-point functions (`bdlat_EnumFunctions`)
404
405inline
407{
408 return value.hasFallback();
409}
410
411inline
413{
414 return value.isFallback();
415}
416
417inline
418void bdlat_enumToInt(int *result, const EnumConstRef& value)
419{
420 value.toInt(result);
421}
422
423inline
424void bdlat_enumToString(bsl::string *result, const EnumConstRef& value)
425{
426 value.toString(result);
427}
428
429inline
430const char *bdlat_TypeName_xsdName(const EnumConstRef& ref, int format)
431{
432 return ref.xsdName(format);
433}
434
435} // close package namespace
436
438
439template <>
441};
442
443template <>
444struct IsEnumeration<bdlar::EnumConstRef> : bsl::true_type {
445};
446
447template <>
449};
450
451template <>
453};
454
455} // close namespace bdlat_EnumFunctions
456
457
458#endif
459
460// ----------------------------------------------------------------------------
461// Copyright 2024 Bloomberg Finance L.P.
462//
463// Licensed under the Apache License, Version 2.0 (the "License");
464// you may not use this file except in compliance with the License.
465// You may obtain a copy of the License at
466//
467// http://www.apache.org/licenses/LICENSE-2.0
468//
469// Unless required by applicable law or agreed to in writing, software
470// distributed under the License is distributed on an "AS IS" BASIS,
471// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
472// See the License for the specific language governing permissions and
473// limitations under the License.
474// ----------------------------- END-OF-FILE ----------------------------------
475
476/** @} */
477/** @} */
478/** @} */
Definition bdlar_enumref.h:138
const char * xsdName(int format) const
Return the bdlat_TypeName::xsdName value for the underlying object.
Definition bdlar_enumref.h:397
const EnumConstVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_enumref.h:361
bool isFallback() const
Definition bdlar_enumref.h:373
void toInt(int *result) const
Definition bdlar_enumref.h:385
bool hasFallback() const
Definition bdlar_enumref.h:367
EnumConstRef(const t_ENUM &enumeration, typename bsl::enable_if< IsEnum< t_ENUM >::value >::type *=0)
Definition bdlar_enumref.h:336
const void * objectAddress() const
Return a pointer to the wrapped object.
Definition bdlar_enumref.h:379
void toString(bsl::string *result) const
Definition bdlar_enumref.h:391
Definition bdlar_enumref.h:68
void toInt(int *result) const
Definition bdlar_enumref.h:267
bool isFallback() const
Definition bdlar_enumref.h:255
int makeFallback() const
Definition bdlar_enumref.h:230
int fromInt(int number) const
Definition bdlar_enumref.h:218
bool hasFallback() const
Definition bdlar_enumref.h:249
void reset() const
Reset the referred object to the default value.
Definition bdlar_enumref.h:236
void * objectAddress() const
Return a pointer to the wrapped object.
Definition bdlar_enumref.h:261
EnumRef(t_ENUM &enumeration, typename bsl::enable_if< IsEnum< t_ENUM >::value >::type *=0)
Definition bdlar_enumref.h:201
int fromString(const char *string, int stringLength) const
Definition bdlar_enumref.h:224
void toString(bsl::string *result) const
Definition bdlar_enumref.h:273
const EnumVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_enumref.h:243
Definition bdlar_enumvtableutil.h:65
Definition bslstl_string.h:1252
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlar_accessorref.h:59
void bdlat_enumToString(bsl::string *result, const EnumRef &value)
Definition bdlar_enumref.h:318
int bdlat_enumFromString(EnumRef *result, const char *string, int stringLength)
Definition bdlar_enumref.h:288
int bdlat_enumFromInt(EnumRef *result, int number)
Definition bdlar_enumref.h:282
void bdlat_enumToInt(int *result, const EnumRef &value)
Definition bdlar_enumref.h:312
const char * bdlat_TypeName_xsdName(const AnyConstRef &ref, int format)
Definition bdlar_anyref.h:716
void bdlat_valueTypeReset(AnyRef *ref)
Definition bdlar_anyref.h:550
int bdlat_enumMakeFallback(EnumRef *result)
Definition bdlar_enumref.h:294
bool bdlat_enumHasFallback(const EnumRef &value)
Definition bdlar_enumref.h:300
bool bdlat_enumIsFallback(const EnumRef &value)
Definition bdlar_enumref.h:306
Definition bdlar_enumref.h:437
Definition bdlar_enumvtable.h:67
ToInt * d_toInt_fp
Definition bdlar_enumvtable.h:76
ToString * d_toString_fp
Definition bdlar_enumvtable.h:77
HasFallback * d_hasFallback_fp
Definition bdlar_enumvtable.h:78
IsFallback * d_isFallback_fp
Definition bdlar_enumvtable.h:79
XsdName * d_xsdName_fp
Definition bdlar_enumvtable.h:80
Definition bdlar_enumvtable.h:92
Reset * d_reset_fp
Definition bdlar_enumvtable.h:104
EnumConstVtable d_const
Definition bdlar_enumvtable.h:100
FromInt * d_fromInt_fp
Definition bdlar_enumvtable.h:101
MakeFallback * d_makeFallback_fp
Definition bdlar_enumvtable.h:103
FromString * d_fromString_fp
Definition bdlar_enumvtable.h:102
Definition bdlar_typecategory.h:105
Definition bdlat_enumfunctions.h:411
Definition bdlat_enumfunctions.h:398
Definition bslmf_enableif.h:530