BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_userfields.h
Go to the documentation of this file.
1/// @file ball_userfields.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_userfields.h -*-C++-*-
8#ifndef INCLUDED_BALL_USERFIELDS
9#define INCLUDED_BALL_USERFIELDS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_userfields ball_userfields
15/// @brief Provide a container of user supplied field values.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_userfields
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_userfields-purpose"> Purpose</a>
25/// * <a href="#ball_userfields-classes"> Classes </a>
26/// * <a href="#ball_userfields-description"> Description </a>
27/// * <a href="#ball_userfields-usage"> Usage </a>
28/// * <a href="#ball_userfields-example-1-basic-use-of-ball-userfields"> Example 1: Basic Use of ball::UserFields </a>
29///
30/// # Purpose {#ball_userfields-purpose}
31/// Provide a container of user supplied field values.
32///
33/// # Classes {#ball_userfields-classes}
34///
35/// - ball::UserFields: a container of user supplied field values
36///
37/// @see ball_userfieldvalue
38///
39/// # Description {#ball_userfields-description}
40/// This component provides a value-semantic container-type,
41/// `ball::UserFields`, that represents a (randomly accessible) sequence of
42/// `ball::UserFieldValue` objects. Each user field value contained in the
43/// sequence functions as a discriminated union of the types described by
44/// `ball::UserFieldType::Enum` (integer, double, string, etc). Values can be
45/// added to the sequence using the `append*` manipulators, and can be
46/// manipulated and accessed using `operator[]`. Additionally,
47/// `ball::UserFields` exposes a random-access iterator providing non-modifiable
48/// access to the sequence through the `begin` and `end` methods.
49///
50/// ## Usage {#ball_userfields-usage}
51///
52///
53/// This section illustrates intended use of this component.
54///
55/// ### Example 1: Basic Use of ball::UserFields {#ball_userfields-example-1-basic-use-of-ball-userfields}
56///
57///
58/// In the following example we demonstrate populating a `ball::UserFields`
59/// object with a sequence of field values.
60///
61/// First, we define the signature for a callback, `populateUserFields`. Most
62/// often `ball::UserFields` objects are populated by a callback, such as the
63/// one described by the `ball::LoggerManagerConfiguration`
64/// `UserFieldsPopulatorCallback`.
65/// @code
66/// /// Populate the specified `fields` with the username and current task
67/// /// identifier. The behavior is undefined unless `fields` is empty.
68/// void populateLoggingFields(ball::UserFields *fields)
69/// {
70/// @endcode
71/// Next, we assert the precondition that `fields` is empty:
72/// @code
73/// BSLS_ASSERT(0 == fields->length());
74/// @endcode
75/// Now, we populate the `fields` object with the username and current task
76/// identifier (for the purpose of illustration, these are simply constants):
77/// @code
78/// static const char *TEST_USER = "testUser";
79/// static const bsls::Types::Int64 TEST_TASK = 4315;
80///
81/// fields->appendString(TEST_USER);
82/// fields->appendInt64(TEST_TASK);
83/// @endcode
84/// Finally, for the purposes of illustration, we verify that `fields` has been
85/// set correctly:
86/// @code
87/// assert(2 == fields->length());
88/// assert(Type::e_STRING == fields->value(0).type());
89/// assert(TEST_USER == fields->value(0).theString());
90/// assert(Type::e_INT64 == fields->value(1).type());
91/// assert(TEST_TASK == fields->value(1).theInt64());
92/// }
93/// @endcode
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bal
99 * @{
100 */
101/** @addtogroup ball
102 * @{
103 */
104/** @addtogroup ball_userfields
105 * @{
106 */
107
108#include <balscm_version.h>
109
110#include <ball_userfieldvalue.h>
111
112#include <bslalg_swaputil.h>
113
114#include <bslma_allocator.h>
116
118
119#include <bsls_assert.h>
120#include <bsls_review.h>
121#include <bsls_types.h>
122
123#include <bsl_vector.h>
124
125
126namespace ball {
127
128 // ================
129 // class UserFields
130 // ================
131
132/// This class implements a value-semantic type for representing a sequence
133/// of (randomly accessible) user field values.
134///
135/// See @ref ball_userfields
137
138 private:
139 // DATA
140 bsl::vector<ball::UserFieldValue> d_values; // sequence of values
141
142 // FRIENDS
143 friend bool operator==(const UserFields&, const UserFields&);
144 friend void swap(UserFields&, UserFields&);
145
146 public:
147 // TYPES
149
150 // TRAITS
152
153 // CREATORS
154
155 /// Create an empty `UserFields` object. Optionally specify a
156 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
157 /// the currently installed default allocator is used.
158 explicit UserFields(bslma::Allocator *basicAllocator = 0);
159
160 /// Create a `UserFields` object having the same value as the specified
161 /// `original` object. Optionally specify a `basicAllocator` used to
162 /// supply memory. If `basicAllocator` is 0, the currently installed
163 /// default allocator is used.
164 UserFields(const UserFields& original,
165 bslma::Allocator *basicAllocator = 0);
166
167 /// Destroy this object.
168 ~UserFields() = default;
169
170 // MANIPULATORS
171
172 /// Assign to this object the value of the specified `rhs` object, and
173 /// return a reference providing modifiable access to this object.
174 UserFields& operator=(const UserFields& rhs);
175
176 /// Remove all of the user field values from this object. After this
177 /// method is called `length` is 0.
178 void removeAll();
179
180 /// Append the specified `value` to this object.
181 void append(const UserFieldValue& value);
182
183 void appendNull();
184
186 void appendDouble(double value);
188 /// Append an element having the unset value to this object.
190
191 /// Append the specified `value` to this object.
193
194 /// Return a reference providing modifiable access to the value at the
195 /// specified `index`. The behavior is undefined unless
196 /// `0 <= index && index < length()`.
198 ball::UserFieldValue& value(int index);
199
200 // Aspects
201
202 /// Efficiently exchange the value of this object with the value of the
203 /// specified `other` object. This method provides the no-throw
204 /// exception-safety guarantee. The behavior is undefined unless this
205 /// object was created with the same allocator as `other`.
206 void swap(UserFields& other);
207
208 // ACCESSORS
209
210 /// Return an iterator providing non-modifiable access to the first
211 /// element in the sequence of user field values maintained by this
212 /// object, or the `end` iterator if this object is empty.
213 ConstIterator begin() const;
214
215 /// Return an iterator providing non-modifiable access to the
216 /// past-the-end element in the sequence of user field values
217 /// maintained by this object.
218 ConstIterator end() const;
219
220 /// Return the number of user field values in this object.
221 int length () const;
222
223 /// Return a reference providing non-modifiable access to the value at
224 /// the specified `index`. The behavior is undefined unless
225 /// `0 <= index && index < length()`.
226 const ball::UserFieldValue& operator[](int index) const;
227 const ball::UserFieldValue& value(int index) const;
228
229 // Aspects
230
231 /// Return the allocator used by this object to supply memory. Note
232 /// that if no allocator was supplied at construction the currently
233 /// installed default allocator is used.
235
236 /// Write the value of this object to the specified output `stream` in
237 /// a human-readable format, and return a reference to `stream`.
238 /// Optionally specify an initial indentation `level`, whose absolute
239 /// value is incremented recursively for nested objects. If `level` is
240 /// specified, optionally specify `spacesPerLevel`, whose absolute
241 /// value indicates the number of spaces per indentation level for this
242 /// and all of its nested objects. If `level` is negative, suppress
243 /// indentation of the first line. If `spacesPerLevel` is negative,
244 /// format the entire output on one line, suppressing all but the
245 /// initial indentation (as governed by `level`). If `stream` is not
246 /// valid on entry, this operation has no effect. Note that the format
247 /// is not fully specified, and can change without notice.
248 bsl::ostream& print(bsl::ostream& stream,
249 int level = 0,
250 int spacesPerLevel = 4) const;
251};
252
253// FREE OPERATORS
254
255/// Return `true` if the specified `lhs` and `rhs` objects have the same
256/// value, and `false` otherwise. Two `ball::UserFields` objects have the
257/// same value if they have the same number of elements, and each element in
258/// `lhs` has the same value as corresponding element at the same index in
259/// `rhs`.
260bool operator==(const UserFields& lhs, const UserFields& rhs);
261
262/// Return `true` if the specified `lhs` and `rhs` objects do not have the
263/// same value, and `false` otherwise. Two `UserFields` objects do not
264/// have the same value if they have a different number of elements, or if
265/// any element in `lhs` has a different value from the corresponding
266/// element at the same index in `rhs`.
267bool operator!=(const UserFields& lhs, const UserFields& rhs);
268
269/// Write the value of the specified `object` to the specified output
270/// `stream` in a single-line format, and return a reference to `stream`.
271/// If `stream` is not valid on entry, this operation has no effect. Note
272/// that this human-readable format is not fully specified, can change
273/// without notice, and is logically equivalent to:
274/// @code
275/// print(stream, 0, -1);
276/// @endcode
277bsl::ostream& operator<<(bsl::ostream& stream, const UserFields& object);
278
279// FREE FUNCTIONS
280
281/// Swap the value of the specified `a` object with the value of the
282/// specified `b` object. If `a` and `b` were created with the same
283/// allocator, then this method provides the no-throw exception-safety
284/// guarantee; otherwise, it provides the basic guarantee.
286
287// ============================================================================
288// INLINE DEFINITIONS
289// ============================================================================
290
291 // ----------------
292 // class UserFields
293 // ----------------
294
295// CREATORS
296inline
298: d_values(basicAllocator)
299{
300}
301
302inline
304 bslma::Allocator *basicAllocator)
305: d_values(original.d_values, basicAllocator)
306{
307}
308
309// MANIPULATORS
310inline
312{
313 d_values = rhs.d_values;
314 return *this;
315}
316
317inline
319{
320 d_values.clear();
321}
322
323inline
325{
326 d_values.emplace_back(value);
327}
328
329
330inline
332{
333 d_values.emplace_back();
334}
335
336inline
341
342inline
343void UserFields::appendDouble(double value)
344{
345 d_values.emplace_back(value);
346}
347
348inline
350{
351 d_values.emplace_back(value);
352}
353
354inline
356{
357 d_values.emplace_back(value);
358}
359
360inline
362{
363 d_values.emplace_back(value);
364}
365
366inline
368{
369 return d_values[index];
370}
371
372inline
374{
375 return d_values[index];
376}
377
378inline
380{
381 BSLS_ASSERT(allocator() == other.allocator());
382
383 d_values.swap(other.d_values);
384}
385
386// ACCESSORS
387inline
389{
390 return d_values.get_allocator().mechanism();
391}
392
393inline
395{
396 return d_values.begin();
397}
398
399inline
401{
402 return d_values.end();
403}
404
405inline
407{
408 return static_cast<int>(d_values.size());
409}
410
411inline
413{
414 return d_values[index];
415}
416
417inline
418const UserFieldValue& UserFields::value(int index) const
419{
420 return d_values[index];
421}
422
423} // close package namespace
424
425// FREE OPERATORS
426inline
427bool ball::operator==(const UserFields& lhs, const UserFields& rhs)
428{
429 return lhs.d_values == rhs.d_values;
430}
431
432inline
433bool ball::operator!=(const UserFields& lhs, const UserFields& rhs)
434{
435 return !(lhs == rhs);
436}
437
438inline
439bsl::ostream& ball::operator<<(bsl::ostream& stream, const UserFields& object)
440{
441 return object.print(stream, 0, -1);
442}
443
444// FREE FUNCTIONS
445inline
446void ball::swap(UserFields& a, UserFields& b)
447{
448 bslalg::SwapUtil::swap(&a.d_values, &b.d_values);
449}
450
451
452
453#endif
454
455// ----------------------------------------------------------------------------
456// Copyright 2015 Bloomberg Finance L.P.
457//
458// Licensed under the Apache License, Version 2.0 (the "License");
459// you may not use this file except in compliance with the License.
460// You may obtain a copy of the License at
461//
462// http://www.apache.org/licenses/LICENSE-2.0
463//
464// Unless required by applicable law or agreed to in writing, software
465// distributed under the License is distributed on an "AS IS" BASIS,
466// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
467// See the License for the specific language governing permissions and
468// limitations under the License.
469// ----------------------------- END-OF-FILE ----------------------------------
470
471/** @} */
472/** @} */
473/** @} */
Definition ball_userfieldvalue.h:135
Definition ball_userfields.h:136
void appendDouble(double value)
Definition ball_userfields.h:343
void appendNull()
Definition ball_userfields.h:331
void appendInt64(bsls::Types::Int64 value)
Definition ball_userfields.h:337
~UserFields()=default
Destroy this object.
friend bool operator==(const UserFields &, const UserFields &)
BSLMF_NESTED_TRAIT_DECLARATION(UserFields, bslma::UsesBslmaAllocator)
ConstIterator end() const
Definition ball_userfields.h:400
void appendDatetimeTz(const bdlt::DatetimeTz &value)
Append an element having the unset value to this object.
Definition ball_userfields.h:355
void append(const UserFieldValue &value)
Append the specified value to this object.
Definition ball_userfields.h:324
friend void swap(UserFields &, UserFields &)
void appendCharArray(const bsl::vector< char > &value)
Append the specified value to this object.
Definition ball_userfields.h:361
UserFields & operator=(const UserFields &rhs)
Definition ball_userfields.h:311
ball::UserFieldValue & value(int index)
Definition ball_userfields.h:373
bsl::vector< ball::UserFieldValue >::const_iterator ConstIterator
Definition ball_userfields.h:148
bslma::Allocator * allocator() const
Definition ball_userfields.h:388
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void appendString(const bsl::string_view &value)
Definition ball_userfields.h:349
ConstIterator begin() const
Definition ball_userfields.h:394
void removeAll()
Definition ball_userfields.h:318
int length() const
Return the number of user field values in this object.
Definition ball_userfields.h:406
ball::UserFieldValue & operator[](int index)
Definition ball_userfields.h:367
UserFields(bslma::Allocator *basicAllocator=0)
Definition ball_userfields.h:297
Definition bdlt_datetimetz.h:308
BloombergLP::bslma::Allocator * mechanism() const
Definition bslma_bslallocator.h:1126
Definition bslstl_stringview.h:441
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2511
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2519
Definition bslstl_vector.h:1025
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:4019
VALUE_TYPE & emplace_back(Args &&... arguments)
Definition bslstl_vector.h:3741
VALUE_TYPE const * const_iterator
Definition bslstl_vector.h:1058
void swap(vector &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:1712
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#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)
Definition bslma_usesbslmaallocator.h:343
long long Int64
Definition bsls_types.h:132