BDE 4.39.x Production Release
Loading...
Searching...
No Matches
s_baltst_mychoice.h
Go to the documentation of this file.
1/// @file s_baltst_mychoice.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// s_baltst_mychoice.h *DO NOT EDIT* @generated -*-C++-*-
8#ifndef INCLUDED_S_BALTST_MYCHOICE
9#define INCLUDED_S_BALTST_MYCHOICE
10
11#include <bsls_ident.h>
12BSLS_IDENT_RCSID(s_baltst_mychoice_h, "$Id$ $CSID$")
14
15/// @defgroup s_baltst_mychoice s_baltst_mychoice
16/// @brief Provide value-semantic attribute classes
17/// @addtogroup Standalones
18/// @{
19/// @addtogroup s_baltst
20/// @{
21/// @addtogroup s_baltst_mychoice
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#s_baltst_mychoice-purpose"> Purpose</a>
26///
27/// # Purpose {#s_baltst_mychoice-purpose}
28/// Provide value-semantic attribute classes
29/// @}
30/** @} */
31/** @} */
32
33/** @addtogroup Standalones
34 * @{
35 */
36/** @addtogroup s_baltst
37 * @{
38 */
39/** @addtogroup s_baltst_mychoice
40 * @{
41 */
42
43#include <bslalg_typetraits.h>
44
45#include <bdlat_attributeinfo.h>
46
47#include <bdlat_selectioninfo.h>
48
49#include <bdlat_typetraits.h>
50
51#include <bsls_objectbuffer.h>
52
53#include <bslma_default.h>
54
55#include <bsls_assert.h>
56
57#include <bsl_string.h>
58
59#include <bsl_iosfwd.h>
60#include <bsl_limits.h>
61
62
63
64namespace bslma { class Allocator; }
65
66namespace s_baltst { class MyChoice; }
67namespace s_baltst {
68
69 // ==============
70 // class MyChoice
71 // ==============
72
73class MyChoice {
74
75 // INSTANCE DATA
76 union {
79 };
80
81 int d_selectionId;
82 bslma::Allocator *d_allocator_p;
83
84 // PRIVATE ACCESSORS
85 bool isEqualTo(const MyChoice& rhs) const;
86
87 public:
88 // TYPES
89
90 enum {
94 };
95
96 enum {
98 };
99
100 enum {
103 };
104
105 // CONSTANTS
106 static const char CLASS_NAME[];
107
109
110 // CLASS METHODS
111
112 /// Return selection information for the selection indicated by the
113 /// specified `id` if the selection exists, and 0 otherwise.
115
116 /// Return selection information for the selection indicated by the
117 /// specified `name` of the specified `nameLength` if the selection
118 /// exists, and 0 otherwise.
120 const char *name,
121 int nameLength);
122
123 // CREATORS
124
125 /// Create an object of type `MyChoice` having the default value. Use
126 /// the optionally specified `basicAllocator` to supply memory. If
127 /// `basicAllocator` is 0, the currently installed default allocator is
128 /// used.
129 explicit MyChoice(bslma::Allocator *basicAllocator = 0);
130
131 /// Create an object of type `MyChoice` having the value of the
132 /// specified `original` object. Use the optionally specified
133 /// `basicAllocator` to supply memory. If `basicAllocator` is 0, the
134 /// currently installed default allocator is used.
135 MyChoice(const MyChoice& original,
136 bslma::Allocator *basicAllocator = 0);
137
138#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES) \
139 && defined(BSLS_COMPILERFEATURES_SUPPORT_NOEXCEPT)
140 /// Create an object of type `MyChoice` having the value of the
141 /// specified `original` object. After performing this action, the
142 /// `original` object will be left in a valid, but unspecified state.
143 MyChoice(MyChoice&& original) noexcept;
144
145 /// Create an object of type `MyChoice` having the value of the
146 /// specified `original` object. After performing this action, the
147 /// `original` object will be left in a valid, but unspecified state.
148 /// Use the optionally specified `basicAllocator` to supply memory. If
149 /// `basicAllocator` is 0, the currently installed default allocator is
150 /// used.
151 MyChoice(MyChoice&& original,
152 bslma::Allocator *basicAllocator);
153#endif
154
155 /// Destroy this object.
156 ~MyChoice();
157
158 // MANIPULATORS
159
160 /// Assign to this object the value of the specified `rhs` object.
162
163#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES) \
164 && defined(BSLS_COMPILERFEATURES_SUPPORT_NOEXCEPT)
165 /// Assign to this object the value of the specified `rhs` object.
166 /// After performing this action, the `rhs` object will be left in a
167 /// valid, but unspecified state.
169#endif
170
171 /// Reset this object to the default value (i.e., its value upon default
172 /// construction).
173 void reset();
174
175 /// Set the value of this object to be the default for the selection
176 /// indicated by the specified `selectionId`. Return 0 on success, and
177 /// non-zero value otherwise (i.e., the selection is not found).
179
180 /// Set the value of this object to be the default for the selection
181 /// indicated by the specified `name` of the specified `nameLength`.
182 /// Return 0 on success, and non-zero value otherwise (i.e., the
183 /// selection is not found).
184 int makeSelection(const char *name, int nameLength);
185
186 /// Set the value of this object to be a "Selection1" value. Optionally
187 /// specify the `value` of the "Selection1". If `value` is not
188 /// specified, the default "Selection1" value is used.
190 int& makeSelection1(int value);
191
194#if defined(BSLS_COMPILERFEATURES_SUPPORT_RVALUE_REFERENCES) \
195 && defined(BSLS_COMPILERFEATURES_SUPPORT_NOEXCEPT)
197#endif
198 // Set the value of this object to be a "Selection2" value. Optionally
199 // specify the 'value' of the "Selection2". If 'value' is not
200 // specified, the default "Selection2" value is used.
201
202 /// Invoke the specified `manipulator` on the address of the modifiable
203 /// selection, supplying `manipulator` with the corresponding selection
204 /// information structure. Return the value returned from the
205 /// invocation of `manipulator` if this object has a defined selection,
206 /// and -1 otherwise.
207 template <typename t_MANIPULATOR>
208 int manipulateSelection(t_MANIPULATOR& manipulator);
209
210 /// Return a reference to the modifiable "Selection1" selection of this
211 /// object if "Selection1" is the current selection.
212 ///
213 /// \pre The behavior is undefined unless "Selection1" is the selection of this object.
214 int& selection1();
215
216 /// Return a reference to the modifiable "Selection2" selection of this
217 /// object if "Selection2" is the current selection.
218 ///
219 /// \pre The behavior is undefined unless "Selection2" is the selection of this object.
221
222 // ACCESSORS
223
224 /// Format this object to the specified output `stream` at the
225 /// optionally specified indentation `level` and return a reference to
226 /// the modifiable `stream`. If `level` is specified, optionally
227 /// specify `spacesPerLevel`, the number of spaces per indentation level
228 /// for this and all of its nested objects. Each line is indented by
229 /// the absolute value of `level * spacesPerLevel`. If `level` is
230 /// negative, suppress indentation of the first line. If
231 /// `spacesPerLevel` is negative, suppress line breaks and format the
232 /// entire output on one line. If `stream` is initially invalid, this operation has no effect.
233 ///
234 /// \note Note that a trailing newline is provided
235 /// in multiline mode only.
236 bsl::ostream& print(bsl::ostream& stream,
237 int level = 0,
238 int spacesPerLevel = 4) const;
239
240 /// Return the id of the current selection if the selection is defined,
241 /// and -1 otherwise.
242 int selectionId() const;
243
244 /// Invoke the specified `accessor` on the non-modifiable selection,
245 /// supplying `accessor` with the corresponding selection information
246 /// structure. Return the value returned from the invocation of
247 /// `accessor` if this object has a defined selection, and -1 otherwise.
248 template <typename t_ACCESSOR>
249 int accessSelection(t_ACCESSOR& accessor) const;
250
251 /// Return a reference to the non-modifiable "Selection1" selection of
252 /// this object if "Selection1" is the current selection.
253 ///
254 /// \pre The behavior is undefined unless "Selection1" is the selection of this object.
255 const int& selection1() const;
256
257 /// Return a reference to the non-modifiable "Selection2" selection of
258 /// this object if "Selection2" is the current selection.
259 ///
260 /// \pre The behavior is undefined unless "Selection2" is the selection of this object.
261 const bsl::string& selection2() const;
262
263 /// Return `true` if the value of this object is a "Selection1" value,
264 /// and return `false` otherwise.
265 bool isSelection1Value() const;
266
267 /// Return `true` if the value of this object is a "Selection2" value,
268 /// and return `false` otherwise.
269 bool isSelection2Value() const;
270
271 /// Return `true` if the value of this object is undefined, and `false`
272 /// otherwise.
273 bool isUndefinedValue() const;
274
275 /// Return the symbolic name of the current selection of this object.
276 const char *selectionName() const;
277
278 // HIDDEN FRIENDS
279
280 /// Return `true` if the specified `lhs` and `rhs` objects have the same
281 /// value, and `false` otherwise. Two `MyChoice` objects have the same
282 /// value if either the selections in both objects have the same ids and
283 /// the same values, or both selections are undefined.
284 friend bool operator==(const MyChoice& lhs, const MyChoice& rhs)
285 {
286 return lhs.isEqualTo(rhs);
287 }
288
289 /// Return `true` if the specified `lhs` and `rhs` objects do not have
290 /// the same values, as determined by `operator==`, and `false`
291 /// otherwise.
292 friend bool operator!=(const MyChoice& lhs, const MyChoice& rhs)
293 {
294 return !(lhs == rhs);
295 }
296
297 /// Format the specified `rhs` to the specified output `stream` and
298 /// return a reference to the modifiable `stream`.
299 friend bsl::ostream& operator<<(bsl::ostream& stream, const MyChoice& rhs)
300 {
301 return rhs.print(stream, 0, -1);
302 }
303};
304
305} // close package namespace
306
307// TRAITS
308
310
311// ============================================================================
312// INLINE DEFINITIONS
313// ============================================================================
314
315namespace s_baltst {
316
317 // --------------
318 // class MyChoice
319 // --------------
320
321// CLASS METHODS
322// PRIVATE ACCESSORS
323inline
324bool MyChoice::isEqualTo(const MyChoice& rhs) const
325{
326 typedef MyChoice Class;
327 if (this->selectionId() == rhs.selectionId()) {
328 switch (rhs.selectionId()) {
329 case Class::SELECTION_ID_SELECTION1:
330 return this->selection1() == rhs.selection1();
331 case Class::SELECTION_ID_SELECTION2:
332 return this->selection2() == rhs.selection2();
333 default:
334 BSLS_ASSERT(Class::SELECTION_ID_UNDEFINED == rhs.selectionId());
335 return true;
336 }
337 }
338 else {
339 return false;
340 }
341}
342
343// CREATORS
344inline
346: d_selectionId(SELECTION_ID_UNDEFINED)
347, d_allocator_p(bslma::Default::allocator(basicAllocator))
348{
349}
350
351inline
353{
354 reset();
355}
356
357// MANIPULATORS
358template <typename t_MANIPULATOR>
359int MyChoice::manipulateSelection(t_MANIPULATOR& manipulator)
360{
361 switch (d_selectionId) {
363 return manipulator(&d_selection1.object(),
366 return manipulator(&d_selection2.object(),
368 default:
370 return -1;
371 }
372}
373
374inline
376{
377 BSLS_ASSERT(SELECTION_ID_SELECTION1 == d_selectionId);
378 return d_selection1.object();
379}
380
381inline
383{
384 BSLS_ASSERT(SELECTION_ID_SELECTION2 == d_selectionId);
385 return d_selection2.object();
386}
387
388// ACCESSORS
389inline
391{
392 return d_selectionId;
393}
394
395template <typename t_ACCESSOR>
396int MyChoice::accessSelection(t_ACCESSOR& accessor) const
397{
398 switch (d_selectionId) {
400 return accessor(d_selection1.object(),
403 return accessor(d_selection2.object(),
405 default:
406 BSLS_ASSERT(SELECTION_ID_UNDEFINED == d_selectionId);
407 return -1;
408 }
409}
410
411inline
412const int& MyChoice::selection1() const
413{
414 BSLS_ASSERT(SELECTION_ID_SELECTION1 == d_selectionId);
415 return d_selection1.object();
416}
417
418inline
420{
421 BSLS_ASSERT(SELECTION_ID_SELECTION2 == d_selectionId);
422 return d_selection2.object();
423}
424
425inline
427{
428 return SELECTION_ID_SELECTION1 == d_selectionId;
429}
430
431inline
433{
434 return SELECTION_ID_SELECTION2 == d_selectionId;
435}
436
437inline
439{
440 return SELECTION_ID_UNDEFINED == d_selectionId;
441}
442} // close package namespace
443
444// FREE FUNCTIONS
445
446
447#endif
448
449// GENERATED BY BLP_BAS_CODEGEN_2025.08.21
450// USING bas_codegen.pl s_baltst_mychoice.xsd --mode msg --includedir . --msgComponent mychoice --noRecurse --noExternalization --noHashSupport --noAggregateConversion
451// ----------------------------------------------------------------------------
452// NOTICE:
453// Copyright 2025 Bloomberg Finance L.P. All rights reserved.
454// Property of Bloomberg Finance L.P. (BFLP)
455// This software is made available solely pursuant to the
456// terms of a BFLP license agreement which governs its use.
457// ------------------------------- END-OF-FILE --------------------------------
458
459/** @} */
460/** @} */
461/** @} */
Definition bslstl_string.h:1252
Definition bslma_allocator.h:545
Definition s_baltst_mychoice.h:73
static const bdlat_SelectionInfo SELECTION_INFO_ARRAY[]
Definition s_baltst_mychoice.h:108
@ SELECTION_ID_SELECTION1
Definition s_baltst_mychoice.h:92
@ SELECTION_ID_SELECTION2
Definition s_baltst_mychoice.h:93
@ SELECTION_ID_UNDEFINED
Definition s_baltst_mychoice.h:91
bool isSelection1Value() const
Definition s_baltst_mychoice.h:426
const char * selectionName() const
Return the symbolic name of the current selection of this object.
int makeSelection(int selectionId)
@ NUM_SELECTIONS
Definition s_baltst_mychoice.h:97
friend bsl::ostream & operator<<(bsl::ostream &stream, const MyChoice &rhs)
Definition s_baltst_mychoice.h:299
int makeSelection(const char *name, int nameLength)
bsls::ObjectBuffer< int > d_selection1
Definition s_baltst_mychoice.h:77
MyChoice & operator=(const MyChoice &rhs)
Assign to this object the value of the specified rhs object.
int manipulateSelection(t_MANIPULATOR &manipulator)
Definition s_baltst_mychoice.h:359
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
int accessSelection(t_ACCESSOR &accessor) const
Definition s_baltst_mychoice.h:396
bsl::string & selection2()
Definition s_baltst_mychoice.h:382
@ SELECTION_INDEX_SELECTION1
Definition s_baltst_mychoice.h:101
@ SELECTION_INDEX_SELECTION2
Definition s_baltst_mychoice.h:102
bool isUndefinedValue() const
Definition s_baltst_mychoice.h:438
static const bdlat_SelectionInfo * lookupSelectionInfo(int id)
static const bdlat_SelectionInfo * lookupSelectionInfo(const char *name, int nameLength)
bsl::string & makeSelection2(const bsl::string &value)
friend bool operator==(const MyChoice &lhs, const MyChoice &rhs)
Definition s_baltst_mychoice.h:284
int & selection1()
Definition s_baltst_mychoice.h:375
bsl::string & makeSelection2()
~MyChoice()
Destroy this object.
Definition s_baltst_mychoice.h:352
static const char CLASS_NAME[]
Definition s_baltst_mychoice.h:106
int & makeSelection1(int value)
MyChoice(bslma::Allocator *basicAllocator=0)
Definition s_baltst_mychoice.h:345
friend bool operator!=(const MyChoice &lhs, const MyChoice &rhs)
Definition s_baltst_mychoice.h:292
MyChoice(const MyChoice &original, bslma::Allocator *basicAllocator=0)
int selectionId() const
Definition s_baltst_mychoice.h:390
bsls::ObjectBuffer< bsl::string > d_selection2
Definition s_baltst_mychoice.h:78
bool isSelection2Value() const
Definition s_baltst_mychoice.h:432
#define BDLAT_DECL_CHOICE_WITH_ALLOCATOR_BITWISEMOVEABLE_TRAITS(ClassName)
Definition bdlat_typetraits.h:268
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT_RCSID(tag, str)
BSLS_IDENT_RCSID() - insert ident str (specific to platform/compiler)
Definition bsls_ident.h:244
#define BSLS_IDENT_PRAGMA_ONCE
BSLS_IDENT_PRAGMA_ONCE - macro to avoid multiple inclusion
Definition bsls_ident.h:263
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
Definition baljsn_encoder_testtypes.h:76
Definition s_baltst_address.h:66
Definition bdlat_selectioninfo.h:138
TYPE & object()
Definition bsls_objectbuffer.h:352