BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_userfieldvalue.h
Go to the documentation of this file.
1/// @file ball_userfieldvalue.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_userfieldvalue.h -*-C++-*-
8#ifndef INCLUDED_BALL_USERFIELDVALUE
9#define INCLUDED_BALL_USERFIELDVALUE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_userfieldvalue ball_userfieldvalue
15/// @brief Provide a type for the value of a user supplied field.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_userfieldvalue
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_userfieldvalue-purpose"> Purpose</a>
25/// * <a href="#ball_userfieldvalue-classes"> Classes </a>
26/// * <a href="#ball_userfieldvalue-description"> Description </a>
27/// * <a href="#ball_userfieldvalue-usage"> Usage </a>
28/// * <a href="#ball_userfieldvalue-example-1-basic-use-of-ball-userfieldvalue"> Example 1: Basic Use of ball::UserFieldValue </a>
29///
30/// # Purpose {#ball_userfieldvalue-purpose}
31/// Provide a type for the value of a user supplied field.
32///
33/// # Classes {#ball_userfieldvalue-classes}
34///
35/// - ball::UserFieldValue: the value of a user supplied field
36///
37/// @see ball_userfields, ball_userfieldtype
38///
39/// # Description {#ball_userfieldvalue-description}
40/// This component provides a value-semantic class,
41/// `ball::UserFieldValue`, that represents the value of a user supplied log
42/// field value. A user field value acts as a discriminated union, and may
43/// represent a value of any of types described in `ball::UserFieldType` or an
44/// unset value (indicated by the type `ball::UserFieldType::e_VOID`).
45///
46/// ## Usage {#ball_userfieldvalue-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Basic Use of ball::UserFieldValue {#ball_userfieldvalue-example-1-basic-use-of-ball-userfieldvalue}
52///
53///
54/// The following snippets of code illustrate how to create and use a
55/// `ball::UserFieldValue` object. Note that `ball::UserFieldValue` objects are
56/// typically used in a description of a sequence of user fields (see
57/// @ref ball_userfields ).
58///
59/// First, we create a default `ball::UserFieldValue`, `valueA`, and observe
60/// that it is in the unset state, meaning that `isUnset` is true and its type
61/// is `ball::UserFieldValue::e_VOID`:
62/// @code
63/// ball::UserFieldValue valueA;
64///
65/// assert(true == valueA.isUnset());
66/// assert(ball::UserFieldValue::e_VOID == valueA.type());
67/// @endcode
68/// Next, we create a second `ball::UserFieldValue` having the value 5, and then
69/// confirm its value and observe that it does not compare equal to the
70/// `valueA`:
71/// @code
72/// ball::UserFieldValue valueB(5);
73///
74/// assert(false == valueB.isUnset());
75/// assert(ball::UserFieldValue::e_INT64 == valueB.type());
76/// assert(5 == valueB.theInt64();
77///
78/// assert(valueA != valueB);
79/// @endcode
80/// Finally, we call `reset` of `valueB` resetting it to the unset state, and
81/// observe that `valueA` now compares equal to `valueB`:
82/// @code
83/// valueB.reset();
84///
85/// assert(valueA == valueB);
86/// @endcode
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bal
92 * @{
93 */
94/** @addtogroup ball
95 * @{
96 */
97/** @addtogroup ball_userfieldvalue
98 * @{
99 */
100
101#include <balscm_version.h>
102
103#include <ball_userfieldtype.h>
104
105#include <bdlb_variant.h>
106
107#include <bdlt_datetimetz.h>
108
109#include <bslma_allocator.h>
111
113
114#include <bsls_assert.h>
115#include <bsls_review.h>
116#include <bsls_types.h>
117
118#include <bsl_string.h>
119#include <bsl_vector.h>
120
121
122namespace ball {
123
124 // ====================
125 // class UserFieldValue
126 // ====================
127
128/// This class implements a value-semantic type for representing the value
129/// of a user field in a log record. A user field value acts as a
130/// discriminated union, and may represent a value of any of the types
131/// described in `ball::UserFieldType` or an unset value (indicated by type
132/// `ball::UserFieldType::e_VOID`).
133///
134/// See @ref ball_userfieldvalue
136
137 // PRIVATE TYPES
139 double,
143
144 // DATA
145 ValueVariant d_value; // value
146
147 // FRIENDS
148 friend bool operator==(const UserFieldValue&, const UserFieldValue&);
150
151 public:
152 // TRAITS
154
155 // CREATORS
156
157 /// Create a user field value having the unset value. Optionally
158 /// specify a `basicAllocator` used to supply memory. If
159 /// `basicAllocator` is 0, the currently installed default allocator is
160 /// used.
161 explicit UserFieldValue(bslma::Allocator *basicAllocator = 0);
162
163 /// Create a user field value having the specified `value`. Optionally
164 /// specify a `basicAllocator` used to supply memory. If
165 /// `basicAllocator` is 0, the currently installed default allocator is
166 /// used.
167 explicit UserFieldValue(bsls::Types::Int64 value,
168 bslma::Allocator *basicAllocator = 0);
169 explicit UserFieldValue(double value,
170 bslma::Allocator *basicAllocator = 0);
171 explicit UserFieldValue(const bsl::string_view& value,
172 bslma::Allocator *basicAllocator = 0);
173 explicit UserFieldValue(const bdlt::DatetimeTz& value,
174 bslma::Allocator *basicAllocator = 0);
175 explicit UserFieldValue(const bsl::vector<char>& value,
176 bslma::Allocator *basicAllocator = 0);
177
178 /// Create a user field value having the specified integral `value`.
179 /// Optionally specify a `basicAllocator` used to supply memory. If
180 /// `basicAllocator` is 0, the currently installed default allocator is
181 /// used.
182 ///
183 ///
184 /// \note Note that this constructor is provided to disambiguate between
185 /// constructors taking `double` and `bsls::Types::Int64` when supplied
186 /// an integer that is not of type `bsls::Types::Int64`. Also note that
187 /// the implementation is (temporarily) provided inline to avoid issues
188 /// with MSVC 2008.
189 template <class t_INTEGRAL_TYPE>
191 t_INTEGRAL_TYPE value,
192 bslma::Allocator *basicAllocator = 0,
194 * = 0)
195 : d_value(static_cast<bsls::Types::Int64>(value), basicAllocator) {}
196
197 /// Create a `UserFieldValue` object having the same value as the
198 /// specified `original` object. Optionally specify a `basicAllocator`
199 /// used to supply memory. If `basicAllocator` is 0, the currently
200 /// installed default allocator is used.
201 UserFieldValue(const UserFieldValue& original,
202 bslma::Allocator *basicAllocator = 0);
203
204 /// Destroy this object.
205 ~UserFieldValue() = default;
206
207 // MANIPULATORS
208
209 /// Assign to this object the value of the specified `rhs` object, and
210 /// return a reference providing modifiable access to this object.
212
213 /// Set this object to have the unset value. After this operation,
214 /// `type() == ball::UserFieldType::e_VOID`.
215 void reset();
216
217 /// Set this object to have the specified `value`. After this
218 /// operation, `type() == ball::UserFieldType::e_INT64`.
219 void setInt64(bsls::Types::Int64 value);
220
221 /// Set this object to have the specified `value`. After this
222 /// operation, `type() == ball::UserFieldType::e_DOUBLE`.
223 void setDouble(double value);
224
225 /// Set this object to have the specified `value`. After this
226 /// operation, `type() == ball::UserFieldType::e_STRING`.
227 void setString(const bsl::string_view& value);
228
229 /// Set this object to have the specified `value`. After this
230 /// operation, `type() == ball::UserFieldType::e_DATETIMETZ`.
231 void setDatetimeTz(const bdlt::DatetimeTz& value);
232
233 /// Set this object to have the specified `value`. After this
234 /// operation, `type() == ball::UserFieldType::e_CHAR_ARRAY`.
235 void setCharArray(const bsl::vector<char>& value);
236
237 // Aspects
238
239 /// Efficiently exchange the value of this object with the value of the
240 /// specified `other` object. This method provides the no-throw
241 /// exception-safety guarantee if either `type()` is the same as
242 /// `other.type()`, or neither `type()` nor `other.type()` is a type
243 /// that requires allocation; otherwise, it provides the basic guarantee.
244 ///
245 /// \pre The behavior is undefined unless this object was created
246 /// with the same allocator as `other`.
247 void swap(UserFieldValue& other);
248
249 // ACCESSORS
250
251 /// Return `true` if this object has the unset value, and `false` otherwise.
252 ///
253 /// \note Note that if `isUnset()` returns `true`, then `type()`
254 /// returns `ball::UserFieldType::e_VOID`.
255 bool isUnset() const;
256
257 /// Return the type of this user field value. The type
258 /// `ball::UserFieldValue::e_VOID` represents the unset value.
260
261 /// Return a reference providing non-modifiable access to the 64-bit integer value of this object.
262 ///
263 /// \pre The behavior is undefined unless
264 /// `type() == ball::UserFieldType::e_INT64`.
265 const bsls::Types::Int64& theInt64() const;
266
267 /// Return a reference providing non-modifiable access to the double value of this object.
268 ///
269 /// \pre The behavior is undefined unless
270 /// `type() == ball::UserFieldType::e_DOUBLE`.
271 const double& theDouble() const;
272
273 /// Return a reference providing non-modifiable access to the string value of this object.
274 ///
275 /// \pre The behavior is undefined unless
276 /// `type() == ball::UserFieldType::e_STRING`.
277 const bsl::string& theString() const;
278
279 /// Return a reference providing non-modifiable access to the
280 /// `DatetimeTz` value of this object.
281 ///
282 /// \pre The behavior is undefined unless `type() == ball::UserFieldType::e_DATETIMETZ`.
283 const bdlt::DatetimeTz& theDatetimeTz() const;
284
285 /// Return a reference providing non-modifiable access to the
286 /// `bsl::vector<char>` value of this object.
287 ///
288 /// \pre The behavior is undefined unless `type() == ball::UserFieldType::e_CHAR_ARRAY`.
289 const bsl::vector<char>& theCharArray() const;
290
291 // Aspects
292
293 /// Return the allocator used by this object to supply memory.
294 ///
295 /// \note Note that if no allocator was supplied at construction the currently
296 /// installed default allocator is used.
298
299 /// Write the value of this object to the specified output `stream` in
300 /// a human-readable format, and return a reference to `stream`.
301 /// Optionally specify an initial indentation `level`, whose absolute
302 /// value is incremented recursively for nested objects. If `level` is
303 /// specified, optionally specify `spacesPerLevel`, whose absolute
304 /// value indicates the number of spaces per indentation level for this
305 /// and all of its nested objects. If `level` is negative, suppress
306 /// indentation of the first line. If `spacesPerLevel` is negative,
307 /// format the entire output on one line, suppressing all but the
308 /// initial indentation (as governed by `level`). If `stream` is not valid on entry, this operation has no effect.
309 ///
310 /// \note Note that the format
311 /// is not fully specified, and can change without notice.
312 bsl::ostream& print(bsl::ostream& stream,
313 int level = 0,
314 int spacesPerLevel = 4) const;
315};
316
317// FREE OPERATORS
318
319/// Return `true` if the specified `lhs` and `rhs` objects have the same
320/// value, and `false` otherwise. Two `UserFieldValue` objects have the
321/// same value if they have the same type, and (if the type is not
322/// `e_VOID`) the value of that type (as accessed through `the*` methods)
323/// is the same.
324bool operator==(const UserFieldValue& lhs, const UserFieldValue& rhs);
325
326/// Return `true` if the specified `lhs` and `rhs` objects do not have the
327/// same value, and `false` otherwise. Two `UserFieldValue` objects do not
328/// have the same value if their type is not the same, or (if their type
329/// is not `e_VOID`) the value of that type (as accessed through `the*`
330/// methods) is not the same.
331bool operator!=(const UserFieldValue& lhs, const UserFieldValue& rhs);
332
333/// Write the value of the specified `object` to the specified output
334/// `stream` in a single-line format, and return a reference to `stream`.
335/// If `stream` is not valid on entry, this operation has no effect.
336///
337/// \note Note that this human-readable format is not fully specified, can change
338/// without notice, and is logically equivalent to:
339/// @code
340/// print(stream, 0, -1);
341/// @endcode
342bsl::ostream& operator<<(bsl::ostream& stream, const UserFieldValue& object);
343
344// FREE FUNCTIONS
345
346/// Swap the value of the specified `a` object with the value of the
347/// specified `b` object. This method provides the no-throw
348/// exception-safety guarantee if either `a.type()` is the same as
349/// `b.type()` and `a` and `b` were created with the same allocator, or
350/// neither `a.type()` nor `b.type()` is a type that requires allocation;
351/// otherwise, it provides the basic guarantee.
353
354// ============================================================================
355// INLINE DEFINITIONS
356// ============================================================================
357
358 // --------------------
359 // class UserFieldValue
360 // --------------------
361
362// CREATORS
363inline
365: d_value(basicAllocator)
366{
367}
368
369inline
371 bslma::Allocator *basicAllocator)
372: d_value(value, basicAllocator)
373{
374}
375
376inline
378: d_value(value, basicAllocator)
379{
380}
381
382inline
384 bslma::Allocator *basicAllocator)
385: d_value(basicAllocator)
386{
387 d_value.assignTo<bsl::string>(value);
388}
389
390inline
392 bslma::Allocator *basicAllocator)
393: d_value(value, basicAllocator)
394{
395}
396
397inline
399 bslma::Allocator *basicAllocator)
400: d_value(value, basicAllocator)
401{
402}
403
404inline
406 bslma::Allocator *basicAllocator)
407: d_value(original.d_value, basicAllocator)
408{
409}
410
411// MANIPULATORS
412inline
414{
415 d_value = rhs.d_value;
416 return *this;
417}
418
419inline
421{
422 d_value.reset();
423}
424
425inline
427{
428 d_value.assign(value);
429}
430
431inline
433{
434 d_value.assign(value);
435}
436
437inline
439{
440 d_value.assignTo<bsl::string>(value);
441}
442
443inline
445{
446 d_value.assign(value);
447}
448
449inline
451{
452 d_value.assign(value);
453}
454
455inline
457{
458 BSLS_ASSERT(allocator() == other.allocator());
459
460 d_value.swap(other.d_value);
461}
462
463// ACCESSORS
464inline
466{
467 return d_value.isUnset();
468}
469
470inline
472{
474
475 return d_value.the<bsls::Types::Int64>();
476}
477
478inline
479const double& UserFieldValue::theDouble() const
480{
481 BSLS_ASSERT_SAFE(d_value.is<double>());
482
483 return d_value.the<double>();
484}
485
486inline
488{
489 BSLS_ASSERT_SAFE(d_value.is<bsl::string>());
490
491 return d_value.the<bsl::string>();
492}
493
494inline
496{
498
499 return d_value.the<bdlt::DatetimeTz>();
500}
501
502inline
504{
506
507 return d_value.the<bsl::vector<char> >();
508}
509
510 // Aspects
511
512inline
514{
515 return d_value.getAllocator();
516}
517
518} // close package namespace
519
520// FREE OPERATORS
521inline
522bool ball::operator==(const UserFieldValue& lhs, const UserFieldValue& rhs)
523{
524 return lhs.d_value == rhs.d_value;
525}
526
527inline
528bool ball::operator!=(const UserFieldValue& lhs, const UserFieldValue& rhs)
529{
530 return !(lhs == rhs);
531}
532
533inline
534bsl::ostream& ball::operator<<(bsl::ostream& stream,
535 const UserFieldValue& object)
536{
537 return object.print(stream, 0, -1);
538}
539
540// FREE FUNCTIONS
541inline
542void ball::swap(UserFieldValue& a, UserFieldValue& b)
543{
544 // 'bdlb::Variant' member 'swap' supports differing allocators.
545
546 a.d_value.swap(b.d_value);
547}
548
549
550
551#endif
552
553// ----------------------------------------------------------------------------
554// Copyright 2015 Bloomberg Finance L.P.
555//
556// Licensed under the Apache License, Version 2.0 (the "License");
557// you may not use this file except in compliance with the License.
558// You may obtain a copy of the License at
559//
560// http://www.apache.org/licenses/LICENSE-2.0
561//
562// Unless required by applicable law or agreed to in writing, software
563// distributed under the License is distributed on an "AS IS" BASIS,
564// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
565// See the License for the specific language governing permissions and
566// limitations under the License.
567// ----------------------------- END-OF-FILE ----------------------------------
568
569/** @} */
570/** @} */
571/** @} */
Definition ball_userfieldvalue.h:135
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool isUnset() const
Definition ball_userfieldvalue.h:465
void setDouble(double value)
Definition ball_userfieldvalue.h:432
friend void swap(UserFieldValue &, UserFieldValue &)
const bsl::string & theString() const
Definition ball_userfieldvalue.h:487
const bdlt::DatetimeTz & theDatetimeTz() const
Definition ball_userfieldvalue.h:495
void setInt64(bsls::Types::Int64 value)
Definition ball_userfieldvalue.h:426
const bsls::Types::Int64 & theInt64() const
Definition ball_userfieldvalue.h:471
void setDatetimeTz(const bdlt::DatetimeTz &value)
Definition ball_userfieldvalue.h:444
UserFieldValue(t_INTEGRAL_TYPE value, bslma::Allocator *basicAllocator=0, typename bsl::enable_if< bsl::is_integral< t_INTEGRAL_TYPE >::value >::type *=0)
Definition ball_userfieldvalue.h:190
UserFieldValue & operator=(const UserFieldValue &rhs)
Definition ball_userfieldvalue.h:413
friend bool operator==(const UserFieldValue &, const UserFieldValue &)
void setCharArray(const bsl::vector< char > &value)
Definition ball_userfieldvalue.h:450
void reset()
Definition ball_userfieldvalue.h:420
const double & theDouble() const
Definition ball_userfieldvalue.h:479
const bsl::vector< char > & theCharArray() const
Definition ball_userfieldvalue.h:503
ball::UserFieldType::Enum type() const
BSLMF_NESTED_TRAIT_DECLARATION(UserFieldValue, bslma::UsesBslmaAllocator)
UserFieldValue(bslma::Allocator *basicAllocator=0)
Definition ball_userfieldvalue.h:364
~UserFieldValue()=default
Destroy this object.
void setString(const bsl::string_view &value)
Definition ball_userfieldvalue.h:438
bslma::Allocator * allocator() const
Definition ball_userfieldvalue.h:513
void reset()
Definition bdlb_variant.h:7592
bool is() const
Definition bdlb_variant.h:7725
bool isUnset() const
Definition bdlb_variant.h:7732
TYPE & the()
Definition bdlb_variant.h:7637
VariantImp & assign(const TYPE &value)
void swap(VariantImp &other)
Definition bdlb_variant.h:7602
VariantImp & assignTo(const SOURCE_TYPE &value)
Definition bdlb_variant.h:2389
Definition bdlt_datetimetz.h:308
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
Definition bslstl_vector.h:1120
Definition bslma_allocator.h:545
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
void swap(CategoryManager_RadixTree< t_VALUE > &a, CategoryManager_RadixTree< t_VALUE > &b)
bool operator!=(const Attribute &lhs, const Attribute &rhs)
bool operator==(const Attribute &lhs, const Attribute &rhs)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
basic_string< char > string
Definition bslstl_string.h:844
Definition bdlt_iso8601util.h:707
Enum
Definition ball_userfieldtype.h:125
Definition bslmf_enableif.h:530
Definition bslmf_isintegral.h:140
Definition bslma_usesbslmaallocator.h:344
long long Int64
Definition bsls_types.h:134