BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlar_choiceref.h
Go to the documentation of this file.
1/// @file bdlar_choiceref.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlar_choiceref.h -*-C++-*-
8#ifndef INCLUDED_BDLAR_CHOICEREF
9#define INCLUDED_BDLAR_CHOICEREF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlar_choiceref bdlar_choiceref
15/// @brief Provide ...
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlar
19/// @{
20/// @addtogroup bdlar_choiceref
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlar_choiceref-purpose"> Purpose</a>
25/// * <a href="#bdlar_choiceref-classes"> Classes </a>
26/// * <a href="#bdlar_choiceref-description"> Description </a>
27///
28/// # Purpose {#bdlar_choiceref-purpose}
29/// Provide ...
30///
31/// # Classes {#bdlar_choiceref-classes}
32///
33/// - bdlar::ChoiceRef: ...
34/// - bdlar::ChoiceConstRef: ...
35///
36/// # Description {#bdlar_choiceref-description}
37/// This component provides ...
38///
39/// @}
40/** @} */
41/** @} */
42
43/** @addtogroup bdl
44 * @{
45 */
46/** @addtogroup bdlar
47 * @{
48 */
49/** @addtogroup bdlar_choiceref
50 * @{
51 */
52
53#include <bdlar_choicevtable.h>
55#include <bdlar_typecategory.h>
56
58
59#include <bslmf_enableif.h>
60
61
62namespace bdlar {
63
64 // ===============
65 // class ChoiceRef
66 // ===============
67
68class ChoiceRef {
69 // DATA
70 void *d_object_p;
71 const ChoiceVtable *d_vtable_p;
72
73 public:
74 // CREATORS
75
76 /// Create `ChoiceRef` object that type erases the supplied `choice`
77 /// object.
78 template <class t_CHOICE>
79 explicit ChoiceRef(t_CHOICE& choice,
80 typename bsl::enable_if<
81 IsChoice<t_CHOICE>::value>::type * = 0);
82
83 /// Create `ChoiceRef` object.
84 /// \pre The behaviour is undefined unless
85 /// `objectAddress` and `vtable` point to the valid objects.
87
88 // MANIPULATORS
89
90 /// Set the value to be the default for the selection indicated by the
91 /// specified `selectionId`. Return 0 on success, and non-zero value
92 /// otherwise (i.e., the selection is not found).
93 int makeSelection(int selectionId) const;
94
95 /// Set the value to be the default for the selection indicated by the
96 /// specified `selectionName` of the specified `selectionNameLength`.
97 /// Return 0 on success, and non-zero value otherwise (i.e., the selection
98 /// is not found).
99 int makeSelection(const char *selectionName,
100 int selectionNameLength) const;
101
102 /// Invoke the specified `manipulator` on the address of the (modifiable)
103 /// selection, supplying `manipulator` with the corresponding selection
104 /// information structure. The supplied `manipulator` must be a callable
105 /// type that can be called as if it had the following signature:
106 /// @code
107 /// template <class t_INFO>
108 /// int manipulator(SELECTION *refOrValue, const t_INFO& info);
109 /// @endcode
110 /// Return the value returned from the invocation of `manipulator`.
111 ///
112 /// \pre The behavior is undefined unless `selectionId() != -1`.
113 template <typename t_MANIPULATOR>
114 int manipulateSelection(t_MANIPULATOR& manipulator) const;
115
116 /// Reset the referred object to the default value.
117 void reset() const;
118
119 // ACCESSORS
120
121 /// Invoke the specified `accessor` on the (non-modifiable) selection,
122 /// supplying `accessor` with the corresponding selection information
123 /// structure. The supplied `accessor` must be a callable type that can be
124 /// called as if it had the following signature:
125 /// @code
126 /// template <class t_INFO>
127 /// int accessor(const SELECTION& refOrValue, const t_INFO& info);
128 /// @endcode
129 /// Return the value returned from the invocation of `accessor`.
130 ///
131 /// \pre The behavior is undefined unless `selectionId() != -1`.
132 template <typename t_ACCESSOR>
133 int accessSelection(t_ACCESSOR& accessor) const;
134
135 /// Return a pointer to the vtable.
136 const ChoiceVtable *vtable() const;
137
138 /// Return true if the object has a selection with the specified
139 /// `selectionId`, and false otherwise.
140 bool hasSelection(int selectionId) const;
141
142 /// Return true if the object has a selection with the specified
143 /// `selectionName` of the specified `selectionNameLength`, and false
144 /// otherwise.
145 bool hasSelection(const char *selectionName,
146 int selectionNameLength) const;
147
148 /// Return a pointer to the wrapped array.
149 void *objectAddress() const;
150
151 /// Return the id of the current selection if the selection is defined, and
152 /// -1 otherwise.
153 int selectionId() const;
154};
155
156 // ====================
157 // class ChoiceConstRef
158 // ====================
159
161 // DATA
162 const void *d_object_p;
163 const ChoiceConstVtable *d_vtable_p;
164
165 public:
166 // CREATORS
167
168 /// Create `ChoiceConstRef` object that type erases the supplied `choice`
169 /// object.
170 template <class t_CHOICE>
171 explicit ChoiceConstRef(const t_CHOICE& choice,
172 typename bsl::enable_if<
173 IsChoice<t_CHOICE>::value>::type * = 0);
174
175 /// Create `ChoiceConstRef` object.
176 /// \pre The behaviour is undefined unless
177 /// `objectAddress` and `vtable` point to the valid objects.
179
180 /// Create `ChoiceConstRef` object that wraps the same object as the
181 /// specified `object`.
182 ChoiceConstRef(const ChoiceRef& object); // IMPLICIT
183
184 // ACCESSORS
185
186 /// Invoke the specified `accessor` on the (non-modifiable) selection,
187 /// supplying `accessor` with the corresponding selection information
188 /// structure. The supplied `accessor` must be a callable type that can be
189 /// called as if it had the following signature:
190 /// @code
191 /// template <class t_INFO>
192 /// int accessor(const SELECTION& refOrValue, const t_INFO& info);
193 /// @endcode
194 /// Return the value returned from the invocation of `accessor`.
195 ///
196 /// \pre The behavior is undefined unless `selectionId() != -1`.
197 template <typename t_ACCESSOR>
198 int accessSelection(t_ACCESSOR& accessor) const;
199
200 /// Return a pointer to the vtable.
201 const ChoiceConstVtable *vtable() const;
202
203 /// Return true if the object has a selection with the specified
204 /// `selectionId`, and false otherwise.
205 bool hasSelection(int selectionId) const;
206
207 /// Return true if the object has a selection with the specified
208 /// `selectionName` of the specified `selectionNameLength`, and false
209 /// otherwise.
210 bool hasSelection(const char *selectionName,
211 int selectionNameLength) const;
212
213 /// Return a pointer to the wrapped array.
214 const void *objectAddress() const;
215
216 /// Return the id of the current selection if the selection is defined, and
217 /// -1 otherwise.
218 int selectionId() const;
219
220 /// Return the `bdlat_TypeName::xsdName` value for the underlying object.
221 const char *xsdName(int format) const;
222};
223
224// ============================================================================
225// INLINE DEFINITIONS
226// ============================================================================
227
228 // ---------------
229 // class ChoiceRef
230 // ---------------
231
232// CREATORS
233template <class t_CHOICE>
234inline
235ChoiceRef::ChoiceRef(t_CHOICE& choice,
236 typename bsl::enable_if<
237 IsChoice<t_CHOICE>::value>::type *)
238: d_object_p(&choice)
239, d_vtable_p(ChoiceVtableUtil::getVtable<t_CHOICE>())
240{
241}
242
243inline
244ChoiceRef::ChoiceRef(void *objectAddress, const ChoiceVtable *vtable)
245: d_object_p(objectAddress)
246, d_vtable_p(vtable)
247{
248}
249
250// MANIPULATORS
251inline
252int ChoiceRef::makeSelection(int selectionId) const
253{
254 return d_vtable_p->d_makeSelectionById_fp(d_object_p, selectionId);
255}
256
257inline
258int ChoiceRef::makeSelection(const char *selectionName,
259 int selectionNameLength) const
260{
261 return d_vtable_p->d_makeSelectionByName_fp(d_object_p,
262 selectionName,
263 selectionNameLength);
264}
265
266template <typename t_MANIPULATOR>
267inline
268int ChoiceRef::manipulateSelection(t_MANIPULATOR& manipulator) const
269{
271 manipulatorRef(manipulator);
272 return d_vtable_p->d_manipulateSelection_fp(d_object_p, manipulatorRef);
273}
274
275inline
277{
278 return d_vtable_p->d_reset_fp(d_object_p);
279}
280
281// ACCESSORS
282template <typename t_ACCESSOR>
283inline
284int ChoiceRef::accessSelection(t_ACCESSOR& accessor) const
285{
287 accessorRef(accessor);
288 return d_vtable_p->d_const.d_accessSelection_fp(d_object_p, accessorRef);
289}
290
291inline
292bool ChoiceRef::hasSelection(int selectionId) const
293{
294 return d_vtable_p->d_const.d_hasSelectionId_fp(d_object_p, selectionId);
295}
296
297inline
298bool ChoiceRef::hasSelection(const char *selectionName,
299 int selectionNameLength) const
300{
301 return d_vtable_p->d_const.d_hasSelectionName_fp(d_object_p,
302 selectionName,
303 selectionNameLength);
304}
305
306inline
308{
309 return d_vtable_p;
310}
311
312inline
314{
315 return d_object_p;
316}
317
318inline
320{
321 return d_vtable_p->d_const.d_selectionId_fp(d_object_p);
322}
323
324// FREE FUNCTIONS
325// Customization-point functions (`bdlat_ChoiceFunctions`)
326
327inline
328int bdlat_choiceMakeSelection(ChoiceRef *ref, int selectionId)
329{
330 return ref->makeSelection(selectionId);
331}
332
333inline
335 const char *selectionName,
336 int selectionNameLength)
337{
338 return ref->makeSelection(selectionName, selectionNameLength);
339}
340
341template <class t_MANIPULATOR>
342inline
343int bdlat_choiceManipulateSelection(ChoiceRef *ref, t_MANIPULATOR& manipulator)
344{
345 return ref->manipulateSelection(manipulator);
346}
347
348template <class t_ACCESSOR>
349inline
350int bdlat_choiceAccessSelection(const ChoiceRef& ref, t_ACCESSOR& accessor)
351{
352 return ref.accessSelection(accessor);
353}
354
355inline
357 const char *selectionName,
358 int selectionNameLength)
359{
360 return ref.hasSelection(selectionName, selectionNameLength);
361}
362
363inline
364bool bdlat_choiceHasSelection(const ChoiceRef& ref, int selectionId)
365{
366 return ref.hasSelection(selectionId);
367}
368
369inline
371{
372 return ref.selectionId();
373}
374
375inline
377{
378 ref->reset();
379}
380
381 // --------------------
382 // class ChoiceConstRef
383 // --------------------
384
385// CREATORS
386template <class t_CHOICE>
387inline
388ChoiceConstRef::ChoiceConstRef(const t_CHOICE& choice,
389 typename bsl::enable_if<
390 IsChoice<t_CHOICE>::value>::type *)
391: d_object_p(&choice)
392, d_vtable_p(ChoiceVtableUtil::getConstVtable<t_CHOICE>())
393{
394}
395
396inline
397ChoiceConstRef::ChoiceConstRef(const void *objectAddress,
398 const ChoiceConstVtable *vtable)
399: d_object_p(objectAddress)
400, d_vtable_p(vtable)
401{
402}
403
404inline
406: d_object_p(object.objectAddress())
407, d_vtable_p(&object.vtable()->d_const)
408{
409}
410
411// ACCESSORS
412template <typename t_ACCESSOR>
413inline
414int ChoiceConstRef::accessSelection(t_ACCESSOR& accessor) const
415{
417 accessorRef(accessor);
418 return d_vtable_p->d_accessSelection_fp(d_object_p, accessorRef);
419}
420
421inline
422bool ChoiceConstRef::hasSelection(int selectionId) const
423{
424 return d_vtable_p->d_hasSelectionId_fp(d_object_p, selectionId);
425}
426
427inline
428bool ChoiceConstRef::hasSelection(const char *selectionName,
429 int selectionNameLength) const
430{
431 return d_vtable_p->d_hasSelectionName_fp(d_object_p,
432 selectionName,
433 selectionNameLength);
434}
435
436inline
438{
439 return d_vtable_p;
440}
441
442inline
444{
445 return d_object_p;
446}
447
448inline
450{
451 return d_vtable_p->d_selectionId_fp(d_object_p);
452}
453
454inline
455const char *ChoiceConstRef::xsdName(int format) const
456{
457 return d_vtable_p->d_xsdName_fp(d_object_p, format);
458}
459
460// FREE FUNCTIONS
461// Customization-point functions (`bdlat_ChoiceFunctions`)
462
463template <class t_ACCESSOR>
464inline
466 t_ACCESSOR& accessor)
467{
468 return ref.accessSelection(accessor);
469}
470
471inline
473 const char *selectionName,
474 int selectionNameLength)
475{
476 return ref.hasSelection(selectionName, selectionNameLength);
477}
478
479inline
480bool bdlat_choiceHasSelection(const ChoiceConstRef& ref, int selectionId)
481{
482 return ref.hasSelection(selectionId);
483}
484
485inline
487{
488 return ref.selectionId();
489}
490
491inline
492const char *bdlat_TypeName_xsdName(const ChoiceConstRef& ref, int format)
493{
494 return ref.xsdName(format);
495}
496
497} // close package namespace
498
500
501template <>
502struct IsChoice<bdlar::ChoiceRef> : bsl::true_type {
503};
504
505template <>
506struct IsChoice<bdlar::ChoiceConstRef> : bsl::true_type {
507};
508
509} // close namespace bdlat_ChoiceFunctions
510
511
512#endif
513
514// ----------------------------------------------------------------------------
515// Copyright 2024 Bloomberg Finance L.P.
516//
517// Licensed under the Apache License, Version 2.0 (the "License");
518// you may not use this file except in compliance with the License.
519// You may obtain a copy of the License at
520//
521// http://www.apache.org/licenses/LICENSE-2.0
522//
523// Unless required by applicable law or agreed to in writing, software
524// distributed under the License is distributed on an "AS IS" BASIS,
525// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
526// See the License for the specific language governing permissions and
527// limitations under the License.
528// ----------------------------- END-OF-FILE ----------------------------------
529
530/** @} */
531/** @} */
532/** @} */
Definition bdlar_choiceref.h:160
int accessSelection(t_ACCESSOR &accessor) const
Definition bdlar_choiceref.h:414
bool hasSelection(int selectionId) const
Definition bdlar_choiceref.h:422
ChoiceConstRef(const t_CHOICE &choice, typename bsl::enable_if< IsChoice< t_CHOICE >::value >::type *=0)
Definition bdlar_choiceref.h:388
const char * xsdName(int format) const
Return the bdlat_TypeName::xsdName value for the underlying object.
Definition bdlar_choiceref.h:455
const void * objectAddress() const
Return a pointer to the wrapped array.
Definition bdlar_choiceref.h:443
int selectionId() const
Definition bdlar_choiceref.h:449
const ChoiceConstVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_choiceref.h:437
Definition bdlar_choiceref.h:68
bool hasSelection(int selectionId) const
Definition bdlar_choiceref.h:292
int manipulateSelection(t_MANIPULATOR &manipulator) const
Definition bdlar_choiceref.h:268
int makeSelection(int selectionId) const
Definition bdlar_choiceref.h:252
int selectionId() const
Definition bdlar_choiceref.h:319
int accessSelection(t_ACCESSOR &accessor) const
Definition bdlar_choiceref.h:284
void reset() const
Reset the referred object to the default value.
Definition bdlar_choiceref.h:276
void * objectAddress() const
Return a pointer to the wrapped array.
Definition bdlar_choiceref.h:313
const ChoiceVtable * vtable() const
Return a pointer to the vtable.
Definition bdlar_choiceref.h:307
ChoiceRef(t_CHOICE &choice, typename bsl::enable_if< IsChoice< t_CHOICE >::value >::type *=0)
Definition bdlar_choiceref.h:235
Definition bdlar_choicevtableutil.h:65
Definition bdlar_dynamictyperef.h:128
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlar_accessorref.h:59
int bdlat_choiceAccessSelection(const ChoiceRef &ref, t_ACCESSOR &accessor)
Definition bdlar_choiceref.h:350
const char * bdlat_TypeName_xsdName(const AnyConstRef &ref, int format)
Definition bdlar_anyref.h:716
int bdlat_choiceSelectionId(const ChoiceRef &ref)
Definition bdlar_choiceref.h:370
void bdlat_valueTypeReset(AnyRef *ref)
Definition bdlar_anyref.h:550
bool bdlat_choiceHasSelection(const ChoiceRef &ref, const char *selectionName, int selectionNameLength)
Definition bdlar_choiceref.h:356
int bdlat_choiceMakeSelection(ChoiceRef *ref, int selectionId)
Definition bdlar_choiceref.h:328
int bdlat_choiceManipulateSelection(ChoiceRef *ref, t_MANIPULATOR &manipulator)
Definition bdlar_choiceref.h:343
Definition bdlar_choiceref.h:499
Definition bdlar_choicevtable.h:69
HasSelectionId * d_hasSelectionId_fp
Definition bdlar_choicevtable.h:82
XsdName * d_xsdName_fp
Definition bdlar_choicevtable.h:85
HasSelectionName * d_hasSelectionName_fp
Definition bdlar_choicevtable.h:83
SelectionId * d_selectionId_fp
Definition bdlar_choicevtable.h:81
AccessSelection * d_accessSelection_fp
Definition bdlar_choicevtable.h:84
Definition bdlar_choicevtable.h:97
ChoiceConstVtable d_const
Definition bdlar_choicevtable.h:108
MakeSelectionByName * d_makeSelectionByName_fp
Definition bdlar_choicevtable.h:110
ManipulateSelection * d_manipulateSelection_fp
Definition bdlar_choicevtable.h:111
MakeSelectionById * d_makeSelectionById_fp
Definition bdlar_choicevtable.h:109
Reset * d_reset_fp
Definition bdlar_choicevtable.h:112
Definition bdlat_choicefunctions.h:532
Definition bslmf_enableif.h:530