BDE 4.14.0 Production release
Loading...
Searching...
No Matches
s_baltst_testenumeration.h
Go to the documentation of this file.
1/// @file s_baltst_testenumeration.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// s_baltst_testenumeration.h -*-C++-*-
8#ifndef INCLUDED_S_BALTST_TESTENUMERATION
9#define INCLUDED_S_BALTST_TESTENUMERATION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup s_baltst_testenumeration s_baltst_testenumeration
15/// @brief Provide a test implementation of a `bdlat` "enumeration" type.
16/// @addtogroup Standalones
17/// @{
18/// @addtogroup s_baltst
19/// @{
20/// @addtogroup s_baltst_testenumeration
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#s_baltst_testenumeration-purpose"> Purpose</a>
25/// * <a href="#s_baltst_testenumeration-classes"> Classes </a>
26///
27/// # Purpose {#s_baltst_testenumeration-purpose}
28/// Provide a test implementation of a `bdlat` "enumeration" type.
29///
30/// # Classes {#s_baltst_testenumeration-classes}
31///
32/// - s_baltst::TestEnumeration: test implementation of a `bdlat` "enumeration"
33/// @}
34/** @} */
35/** @} */
36
37/** @addtogroup Standalones
38 * @{
39 */
40/** @addtogroup s_baltst
41 * @{
42 */
43/** @addtogroup s_baltst_testenumeration
44 * @{
45 */
46/
47#include <bdlat_enumfunctions.h>
48
49#include <bslmf_assert.h>
50#include <bslmf_issame.h>
51
54
55#include <bsl_ostream.h>
56#include <bsl_string.h>
57#include <bsl_string_view.h>
58
59
60namespace s_baltst {
61
62 // =====================
63 // class TestEnumeration
64 // =====================
65
66/// This in-core value-semantic class provides a basic implementation of the
67/// `bdlat` `Enumeration` concept.
68///
69/// See @ref s_baltst_testenumeration
70template <class E0, class E1 = TestNilEnumerator, class E2 = TestNilEnumerator>
72
73 public:
74 // INVARIANTS
76
77 // TYPES
78 typedef E0 Enumerator0;
79 typedef E1 Enumerator1;
80 typedef E2 Enumerator2;
81
82 // CLASS DATA
83 enum {
86 };
87
88 private:
89 // DATA
90 int d_value;
91
92 public:
93 // CLASS METHODS
94 static bool areEqual(const TestEnumeration& lhs,
95 const TestEnumeration& rhs);
96
97 // CREATORS
99
100 explicit TestEnumeration(int value);
101
102 TestEnumeration(const TestEnumeration& original);
103
104 // MANIPULATORS
105 TestEnumeration& operator=(const TestEnumeration& original);
106
107 int fromInt(int number);
108
109 int fromString(const char *string, int stringLength);
110
111 // ACCESSORS
112 const char *className() const;
113
114 void toInt(int *result) const;
115
116 void toString(bsl::string *result) const;
117};
118
119// FREE FUNCTIONS
120template <class E0, class E1, class E2>
121bsl::ostream& operator<<(bsl::ostream& stream,
122 const TestEnumeration<E0, E1, E2>& object);
123
124template <class E0, class E1, class E2>
125bool operator==(const TestEnumeration<E0, E1, E2>& lhs,
126 const TestEnumeration<E0, E1, E2>& rhs);
127
128template <class E0, class E1, class E2>
129bool operator!=(const TestEnumeration<E0, E1, E2>& lhs,
130 const TestEnumeration<E0, E1, E2>& rhs);
131
132// TRAITS
133template <class E0, class E1, class E2>
134const char *bdlat_TypeName_className(
135 const TestEnumeration<E0, E1, E2>& object);
136
137template <class E0, class E1, class E2>
138int bdlat_enumFromInt(TestEnumeration<E0, E1, E2> *result, int number);
139
140template <class E0, class E1, class E2>
141int bdlat_enumFromString(TestEnumeration<E0, E1, E2> *result,
142 const char *string,
143 int stringLength);
144
145template <class E0, class E1, class E2>
146void bdlat_enumToInt(int *result, const TestEnumeration<E0, E1, E2>& value);
147
148template <class E0, class E1, class E2>
150 const TestEnumeration<E0, E1, E2>& value);
151
152} // close package namespace
153
154namespace bdlat_EnumFunctions {
155
156template <class E0, class E1, class E2>
157struct IsEnumeration<s_baltst::TestEnumeration<E0, E1, E2> >
158: public bsl::true_type {
159};
160
161} // close bdlat_EnumFunctions namespace
162
163namespace s_baltst {
164
165// ============================================================================
166// INLINE DEFINITIONS
167// ============================================================================
168
169 // ---------------------
170 // class TestEnumeration
171 // ---------------------
172
173// CLASS METHODS
174template <class E0, class E1, class E2>
176 const TestEnumeration& rhs)
177{
178 return lhs.d_value == rhs.d_value;
179}
180
181// CREATORS
182template <class E0, class E1, class E2>
184: d_value(Enumerator0::intValue())
185{
186}
187
188template <class E0, class E1, class E2>
190: d_value(value)
191{
192}
193
194template <class E0, class E1, class E2>
196: d_value(original.d_value)
197{
198}
199
200// MANIPULATORS
201template <class E0, class E1, class E2>
203 const TestEnumeration& original)
204{
205 d_value = original.d_value;
206 return *this;
207}
208
209template <class E0, class E1, class E2>
211{
212 if (Enumerator0::intValue() == number) {
213 d_value = Enumerator0::intValue();
214 return 0; // RETURN
215 }
216
217 if (k_HAS_ENUMERATOR_1 && Enumerator1::intValue() == number) {
218 d_value = Enumerator1::intValue();
219 return 0; // RETURN
220 }
221
222 if (k_HAS_ENUMERATOR_2 && Enumerator2::intValue() == number) {
223 d_value = Enumerator2::intValue();
224 return 0; // RETURN
225 }
226
227 return -1;
228}
229
230template <class E0, class E1, class E2>
232 int stringLength)
233{
234 const bsl::string_view stringRef(string, stringLength);
235
236 if (Enumerator0::stringValue() == stringRef) {
237 d_value = Enumerator0::intValue();
238 return 0; // RETURN
239 }
240
241 if (k_HAS_ENUMERATOR_1 && Enumerator1::stringValue() == stringRef) {
242 d_value = Enumerator1::intValue();
243 return 0; // RETURN
244 }
245
246 if (k_HAS_ENUMERATOR_2 && Enumerator2::stringValue() == stringRef) {
247 d_value = Enumerator2::intValue();
248 return 0; // RETURN
249 }
250
251 return -1;
252}
253
254// ACCESSORS
255template <class E0, class E1, class E2>
257{
258 return "MyEnumeration";
259}
260
261template <class E0, class E1, class E2>
263{
264 BSLS_ASSERT(d_value == Enumerator0::intValue() ||
265 (k_HAS_ENUMERATOR_1 && d_value == Enumerator1::intValue()) ||
266 (k_HAS_ENUMERATOR_2 && d_value == Enumerator2::intValue()));
267
268 *result = d_value;
269}
270
271template <class E0, class E1, class E2>
273{
274 BSLS_ASSERT(d_value == Enumerator0::intValue() ||
275 (k_HAS_ENUMERATOR_1 && d_value == Enumerator1::intValue()) ||
276 (k_HAS_ENUMERATOR_2 && d_value == Enumerator2::intValue()));
277
278 if (Enumerator0::intValue() == d_value) {
279 *result = Enumerator0::stringValue();
280 }
281
282 if (k_HAS_ENUMERATOR_1 && Enumerator1::intValue() == d_value) {
283 *result = Enumerator1::stringValue();
284 }
285
286 if (k_HAS_ENUMERATOR_2 && Enumerator2::intValue() == d_value) {
287 *result = Enumerator2::stringValue();
288 }
289}
290
291// FREE FUNCTIONS
292template <class E0, class E1, class E2>
293bsl::ostream& operator<<(bsl::ostream& stream,
294 const TestEnumeration<E0, E1, E2>& object)
295{
296 bsl::string value;
297 object.toString(&value);
298
299 return stream << "[" << object.className() << " value " << value << "]";
300}
301
302template <class E0, class E1, class E2>
303bool operator==(const TestEnumeration<E0, E1, E2>& lhs,
305{
307}
308
309template <class E0, class E1, class E2>
310bool operator!=(const TestEnumeration<E0, E1, E2>& lhs,
312{
314}
315
316// TRAITS
317template <class E0, class E1, class E2>
319{
320 return object.className();
321}
322
323template <class E0, class E1, class E2>
325{
326 return result->fromInt(number);
327}
328
329template <class E0, class E1, class E2>
331 const char *string,
332 int stringLength)
333{
334 return result->fromString(string, stringLength);
335}
336
337template <class E0, class E1, class E2>
338void bdlat_enumToInt(int *result, const TestEnumeration<E0, E1, E2>& value)
339{
340 value.toInt(result);
341}
342
343template <class E0, class E1, class E2>
345 const TestEnumeration<E0, E1, E2>& value)
346{
347 value.toString(result);
348}
349
350} // close package namespace
351
352
353#endif // INCLUDED_S_BALTST_TESTENUMERATION
354
355// ----------------------------------------------------------------------------
356// Copyright 2021 Bloomberg Finance L.P.
357//
358// Licensed under the Apache License, Version 2.0 (the "License");
359// you may not use this file except in compliance with the License.
360// You may obtain a copy of the License at
361//
362// http://www.apache.org/licenses/LICENSE-2.0
363//
364// Unless required by applicable law or agreed to in writing, software
365// distributed under the License is distributed on an "AS IS" BASIS,
366// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
367// See the License for the specific language governing permissions and
368// limitations under the License.
369// ----------------------------- END-OF-FILE ----------------------------------
370
371/** @} */
372/** @} */
373/** @} */
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
Definition s_baltst_testenumeration.h:71
E0 Enumerator0
Definition s_baltst_testenumeration.h:78
const char * className() const
Definition s_baltst_testenumeration.h:256
BSLMF_ASSERT((!bslmf::IsSame< TestNilEnumerator, E0 >::value))
E1 Enumerator1
Definition s_baltst_testenumeration.h:79
TestEnumeration & operator=(const TestEnumeration &original)
Definition s_baltst_testenumeration.h:202
int fromString(const char *string, int stringLength)
Definition s_baltst_testenumeration.h:231
static bool areEqual(const TestEnumeration &lhs, const TestEnumeration &rhs)
Definition s_baltst_testenumeration.h:175
int fromInt(int number)
Definition s_baltst_testenumeration.h:210
@ k_HAS_ENUMERATOR_1
Definition s_baltst_testenumeration.h:84
@ k_HAS_ENUMERATOR_2
Definition s_baltst_testenumeration.h:85
void toInt(int *result) const
Definition s_baltst_testenumeration.h:262
TestEnumeration()
Definition s_baltst_testenumeration.h:183
E2 Enumerator2
Definition s_baltst_testenumeration.h:80
void toString(bsl::string *result) const
Definition s_baltst_testenumeration.h:272
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlat_enumfunctions.h:389
Definition s_baltst_address.h:66
int bdlat_enumFromInt(TestDynamicType< VALUE_TYPE > *object, int number)
Definition s_baltst_testdynamictype.h:1972
int bdlat_enumFromString(TestDynamicType< VALUE_TYPE > *object, const char *string, int stringLength)
Definition s_baltst_testdynamictype.h:1978
void bdlat_enumToInt(int *result, const TestDynamicType< VALUE_TYPE > &object)
Definition s_baltst_testdynamictype.h:1986
void bdlat_enumToString(bsl::string *result, const TestDynamicType< VALUE_TYPE > &object)
Definition s_baltst_testdynamictype.h:1992
const char * bdlat_TypeName_className(const TestChoice< C0, C1, C2 > &object)
Definition s_baltst_testchoice.h:718
bsl::ostream & operator<<(bsl::ostream &stream, const Address &rhs)
Definition bdlat_enumfunctions.h:403
Definition bslmf_issame.h:181