BDE 4.14.0 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 explicit UserFieldValue(bsls::Types::Int64 value,
164 bslma::Allocator *basicAllocator = 0);
165 explicit UserFieldValue(double value,
166 bslma::Allocator *basicAllocator = 0);
167 explicit UserFieldValue(const bsl::string_view& value,
168 bslma::Allocator *basicAllocator = 0);
169 explicit UserFieldValue(const bdlt::DatetimeTz& value,
170 bslma::Allocator *basicAllocator = 0);
171 /// Create a user field value having the specified `value`. Optionally
172 /// specify a `basicAllocator` used to supply memory. If
173 /// `basicAllocator` is 0, the currently installed default allocator is
174 /// used.
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 /// Note that this constructor is provided to disambiguate between
184 /// constructors taking `double` and `bsls::Types::Int64` when supplied
185 /// an integer that is not of type `bsls::Types::Int64`. Also note that
186 /// the implementation is (temporarily) provided inline to avoid issues
187 /// with MSVC 2008.
188 template <class t_INTEGRAL_TYPE>
190 t_INTEGRAL_TYPE value,
191 bslma::Allocator *basicAllocator = 0,
193 * = 0)
194 : d_value(static_cast<bsls::Types::Int64>(value), basicAllocator) {}
195
196 /// Create a `UserFieldValue` object having the same value as the
197 /// specified `original` object. Optionally specify a `basicAllocator`
198 /// used to supply memory. If `basicAllocator` is 0, the currently
199 /// installed default allocator is used.
200 UserFieldValue(const UserFieldValue& original,
201 bslma::Allocator *basicAllocator = 0);
202
203 /// Destroy this object.
204 ~UserFieldValue() = default;
205
206 // MANIPULATORS
207
208 /// Assign to this object the value of the specified `rhs` object, and
209 /// return a reference providing modifiable access to this object.
211
212 /// Set this object to have the unset value. After this operation,
213 /// `type() == ball::UserFieldType::e_VOID`.
214 void reset();
215
216 /// Set this object to have the specified `value`. After this
217 /// operation, `type() == ball::UserFieldType::e_INT64`.
218 void setInt64(bsls::Types::Int64 value);
219
220 /// Set this object to have the specified `value`. After this
221 /// operation, `type() == ball::UserFieldType::e_DOUBLE`.
222 void setDouble(double value);
223
224 /// Set this object to have the specified `value`. After this
225 /// operation, `type() == ball::UserFieldType::e_STRING`.
226 void setString(const bsl::string_view& value);
227
228 /// Set this object to have the specified `value`. After this
229 /// operation, `type() == ball::UserFieldType::e_DATETIMETZ`.
230 void setDatetimeTz(const bdlt::DatetimeTz& value);
231
232 /// Set this object to have the specified `value`. After this
233 /// operation, `type() == ball::UserFieldType::e_CHAR_ARRAY`.
234 void setCharArray(const bsl::vector<char>& value);
235
236 // Aspects
237
238 /// Efficiently exchange the value of this object with the value of the
239 /// specified `other` object. This method provides the no-throw
240 /// exception-safety guarantee if either `type()` is the same as
241 /// `other.type()`, or neither `type()` nor `other.type()` is a type
242 /// that requires allocation; otherwise, it provides the basic
243 /// guarantee. The behavior is undefined unless this object was created
244 /// with the same allocator as `other`.
245 void swap(UserFieldValue& other);
246
247 // ACCESSORS
248
249 /// Return `true` if this object has the unset value, and `false`
250 /// otherwise. Note that if `isUnset()` returns `true`, then `type()`
251 /// returns `ball::UserFieldType::e_VOID`.
252 bool isUnset() const;
253
254 /// Return the type of this user field value. The type
255 /// `ball::UserFieldValue::e_VOID` represents the unset value.
257
258 /// Return a reference providing non-modifiable access to the 64-bit
259 /// integer value of this object. The behavior is undefined unless
260 /// `type() == ball::UserFieldType::e_INT64`.
261 const bsls::Types::Int64& theInt64() const;
262
263 /// Return a reference providing non-modifiable access to the double
264 /// value of this object. The behavior is undefined unless
265 /// `type() == ball::UserFieldType::e_DOUBLE`.
266 const double& theDouble() const;
267
268 /// Return a reference providing non-modifiable access to the string
269 /// value of this object. The behavior is undefined unless
270 /// `type() == ball::UserFieldType::e_STRING`.
271 const bsl::string& theString() const;
272
273 /// Return a reference providing non-modifiable access to the
274 /// `DatetimeTz` value of this object. The behavior is undefined
275 /// unless `type() == ball::UserFieldType::e_DATETIMETZ`.
276 const bdlt::DatetimeTz& theDatetimeTz() const;
277
278 /// Return a reference providing non-modifiable access to the
279 /// `bsl::vector<char>` value of this object. The behavior is undefined
280 /// unless `type() == ball::UserFieldType::e_CHAR_ARRAY`.
281 const bsl::vector<char>& theCharArray() const;
282
283 // Aspects
284
285 /// Return the allocator used by this object to supply memory. Note
286 /// that if no allocator was supplied at construction the currently
287 /// installed default allocator is used.
289
290 /// Write the value of this object to the specified output `stream` in
291 /// a human-readable format, and return a reference to `stream`.
292 /// Optionally specify an initial indentation `level`, whose absolute
293 /// value is incremented recursively for nested objects. If `level` is
294 /// specified, optionally specify `spacesPerLevel`, whose absolute
295 /// value indicates the number of spaces per indentation level for this
296 /// and all of its nested objects. If `level` is negative, suppress
297 /// indentation of the first line. If `spacesPerLevel` is negative,
298 /// format the entire output on one line, suppressing all but the
299 /// initial indentation (as governed by `level`). If `stream` is not
300 /// valid on entry, this operation has no effect. Note that the format
301 /// is not fully specified, and can change without notice.
302 bsl::ostream& print(bsl::ostream& stream,
303 int level = 0,
304 int spacesPerLevel = 4) const;
305};
306
307// FREE OPERATORS
308
309/// Return `true` if the specified `lhs` and `rhs` objects have the same
310/// value, and `false` otherwise. Two `UserFieldValue` objects have the
311/// same value if they have the same type, and (if the type is not
312/// `e_VOID`) the value of that type (as accessed through `the*` methods)
313/// is the same.
314bool operator==(const UserFieldValue& lhs, const UserFieldValue& rhs);
315
316/// Return `true` if the specified `lhs` and `rhs` objects do not have the
317/// same value, and `false` otherwise. Two `UserFieldValue` objects do not
318/// have the same value if their type is not the same, or (if their type
319/// is not `e_VOID`) the value of that type (as accessed through `the*`
320/// methods) is not the same.
321bool operator!=(const UserFieldValue& lhs, const UserFieldValue& rhs);
322
323/// Write the value of the specified `object` to the specified output
324/// `stream` in a single-line format, and return a reference to `stream`.
325/// If `stream` is not valid on entry, this operation has no effect. Note
326/// that this human-readable format is not fully specified, can change
327/// without notice, and is logically equivalent to:
328/// @code
329/// print(stream, 0, -1);
330/// @endcode
331bsl::ostream& operator<<(bsl::ostream& stream, const UserFieldValue& object);
332
333// FREE FUNCTIONS
334
335/// Swap the value of the specified `a` object with the value of the
336/// specified `b` object. This method provides the no-throw
337/// exception-safety guarantee if either `a.type()` is the same as
338/// `b.type()` and `a` and `b` were created with the same allocator, or
339/// neither `a.type()` nor `b.type()` is a type that requires allocation;
340/// otherwise, it provides the basic guarantee.
342
343// ============================================================================
344// INLINE DEFINITIONS
345// ============================================================================
346
347 // --------------------
348 // class UserFieldValue
349 // --------------------
350
351// CREATORS
352inline
354: d_value(basicAllocator)
355{
356}
357
358inline
360 bslma::Allocator *basicAllocator)
361: d_value(value, basicAllocator)
362{
363}
364
365inline
367: d_value(value, basicAllocator)
368{
369}
370
371inline
373 bslma::Allocator *basicAllocator)
374: d_value(basicAllocator)
375{
376 d_value.assignTo<bsl::string>(value);
377}
378
379inline
381 bslma::Allocator *basicAllocator)
382: d_value(value, basicAllocator)
383{
384}
385
386inline
388 bslma::Allocator *basicAllocator)
389: d_value(value, basicAllocator)
390{
391}
392
393inline
395 bslma::Allocator *basicAllocator)
396: d_value(original.d_value, basicAllocator)
397{
398}
399
400// MANIPULATORS
401inline
403{
404 d_value = rhs.d_value;
405 return *this;
406}
407
408inline
410{
411 d_value.reset();
412}
413
414inline
416{
417 d_value.assign(value);
418}
419
420inline
422{
423 d_value.assign(value);
424}
425
426inline
428{
429 d_value.assignTo<bsl::string>(value);
430}
431
432inline
434{
435 d_value.assign(value);
436}
437
438inline
440{
441 d_value.assign(value);
442}
443
444inline
446{
447 BSLS_ASSERT(allocator() == other.allocator());
448
449 d_value.swap(other.d_value);
450}
451
452// ACCESSORS
453inline
455{
456 return d_value.isUnset();
457}
458
459inline
461{
463
464 return d_value.the<bsls::Types::Int64>();
465}
466
467inline
468const double& UserFieldValue::theDouble() const
469{
470 BSLS_ASSERT_SAFE(d_value.is<double>());
471
472 return d_value.the<double>();
473}
474
475inline
477{
478 BSLS_ASSERT_SAFE(d_value.is<bsl::string>());
479
480 return d_value.the<bsl::string>();
481}
482
483inline
485{
487
488 return d_value.the<bdlt::DatetimeTz>();
489}
490
491inline
493{
495
496 return d_value.the<bsl::vector<char> >();
497}
498
499 // Aspects
500
501inline
503{
504 return d_value.getAllocator();
505}
506
507} // close package namespace
508
509// FREE OPERATORS
510inline
511bool ball::operator==(const UserFieldValue& lhs, const UserFieldValue& rhs)
512{
513 return lhs.d_value == rhs.d_value;
514}
515
516inline
517bool ball::operator!=(const UserFieldValue& lhs, const UserFieldValue& rhs)
518{
519 return !(lhs == rhs);
520}
521
522inline
523bsl::ostream& ball::operator<<(bsl::ostream& stream,
524 const UserFieldValue& object)
525{
526 return object.print(stream, 0, -1);
527}
528
529// FREE FUNCTIONS
530inline
531void ball::swap(UserFieldValue& a, UserFieldValue& b)
532{
533 // 'bdlb::Variant' member 'swap' supports differing allocators.
534
535 a.d_value.swap(b.d_value);
536}
537
538
539
540#endif
541
542// ----------------------------------------------------------------------------
543// Copyright 2015 Bloomberg Finance L.P.
544//
545// Licensed under the Apache License, Version 2.0 (the "License");
546// you may not use this file except in compliance with the License.
547// You may obtain a copy of the License at
548//
549// http://www.apache.org/licenses/LICENSE-2.0
550//
551// Unless required by applicable law or agreed to in writing, software
552// distributed under the License is distributed on an "AS IS" BASIS,
553// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
554// See the License for the specific language governing permissions and
555// limitations under the License.
556// ----------------------------- END-OF-FILE ----------------------------------
557
558/** @} */
559/** @} */
560/** @} */
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:454
void setDouble(double value)
Definition ball_userfieldvalue.h:421
friend void swap(UserFieldValue &, UserFieldValue &)
const bsl::string & theString() const
Definition ball_userfieldvalue.h:476
const bdlt::DatetimeTz & theDatetimeTz() const
Definition ball_userfieldvalue.h:484
void setInt64(bsls::Types::Int64 value)
Definition ball_userfieldvalue.h:415
const bsls::Types::Int64 & theInt64() const
Definition ball_userfieldvalue.h:460
void setDatetimeTz(const bdlt::DatetimeTz &value)
Definition ball_userfieldvalue.h:433
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:189
UserFieldValue & operator=(const UserFieldValue &rhs)
Definition ball_userfieldvalue.h:402
friend bool operator==(const UserFieldValue &, const UserFieldValue &)
void setCharArray(const bsl::vector< char > &value)
Definition ball_userfieldvalue.h:439
void reset()
Definition ball_userfieldvalue.h:409
const double & theDouble() const
Definition ball_userfieldvalue.h:468
const bsl::vector< char > & theCharArray() const
Definition ball_userfieldvalue.h:492
ball::UserFieldType::Enum type() const
BSLMF_NESTED_TRAIT_DECLARATION(UserFieldValue, bslma::UsesBslmaAllocator)
UserFieldValue(bslma::Allocator *basicAllocator=0)
Definition ball_userfieldvalue.h:353
~UserFieldValue()=default
Destroy this object.
void setString(const bsl::string_view &value)
Definition ball_userfieldvalue.h:427
bslma::Allocator * allocator() const
Definition ball_userfieldvalue.h:502
void reset()
Definition bdlb_variant.h:7472
bool is() const
Definition bdlb_variant.h:7605
bool isUnset() const
Definition bdlb_variant.h:7612
TYPE & the()
Definition bdlb_variant.h:7517
VariantImp & assign(const TYPE &value)
void swap(VariantImp &other)
Definition bdlb_variant.h:7482
VariantImp & assignTo(const SOURCE_TYPE &value)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
Definition bdlb_variant.h:7619
Definition bdlb_variant.h:2312
Definition bdlt_datetimetz.h:308
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
Definition bslstl_vector.h:1025
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
void swap(ball::UserFields &a, ball::UserFields &b)
bool operator!=(const Attribute &lhs, const Attribute &rhs)
bool operator==(const Attribute &lhs, const Attribute &rhs)
basic_string< char > string
Definition bslstl_string.h:782
Definition bdlt_iso8601util.h:691
Enum
Definition ball_userfieldtype.h:123
Definition bslmf_enableif.h:525
Definition bslmf_isintegral.h:130
Definition bslma_usesbslmaallocator.h:343
long long Int64
Definition bsls_types.h:132