BDE 4.39.x 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 /// Append an element having the unset value to this object.
184 void appendNull();
185
187 void appendDouble(double value);
190
191 /// Append the specified `value` to this object.
193
194 /// Return a reference providing modifiable access to the value at the specified `index`.
195 ///
196 /// \pre The behavior is undefined unless
197 /// `0 <= index && index < length()`.
199 ball::UserFieldValue& value(int index);
200
201 // Aspects
202
203 /// Efficiently exchange the value of this object with the value of the
204 /// specified `other` object. This method provides the no-throw exception-safety guarantee.
205 ///
206 /// \pre The behavior is undefined unless this
207 /// object was created with the same allocator as `other`.
208 void swap(UserFields& other);
209
210 // ACCESSORS
211
212 /// Return an iterator providing non-modifiable access to the first
213 /// element in the sequence of user field values maintained by this
214 /// object, or the `end` iterator if this object is empty.
215 ConstIterator begin() const;
216
217 /// Return an iterator providing non-modifiable access to the
218 /// past-the-end element in the sequence of user field values
219 /// maintained by this object.
220 ConstIterator end() const;
221
222 /// Return the number of user field values in this object.
223 int length () const;
224
225 /// Return a reference providing non-modifiable access to the value at the specified `index`.
226 ///
227 /// \pre The behavior is undefined unless
228 /// `0 <= index && index < length()`.
229 const ball::UserFieldValue& operator[](int index) const;
230 const ball::UserFieldValue& value(int index) const;
231
232 // Aspects
233
234 /// Return the allocator used by this object to supply memory.
235 ///
236 /// \note Note that if no allocator was supplied at construction the currently
237 /// installed default allocator is used.
239
240 /// Write the value of this object to the specified output `stream` in
241 /// a human-readable format, and return a reference to `stream`.
242 /// Optionally specify an initial indentation `level`, whose absolute
243 /// value is incremented recursively for nested objects. If `level` is
244 /// specified, optionally specify `spacesPerLevel`, whose absolute
245 /// value indicates the number of spaces per indentation level for this
246 /// and all of its nested objects. If `level` is negative, suppress
247 /// indentation of the first line. If `spacesPerLevel` is negative,
248 /// format the entire output on one line, suppressing all but the
249 /// initial indentation (as governed by `level`). If `stream` is not valid on entry, this operation has no effect.
250 ///
251 /// \note Note that the format
252 /// is not fully specified, and can change without notice.
253 bsl::ostream& print(bsl::ostream& stream,
254 int level = 0,
255 int spacesPerLevel = 4) const;
256};
257
258// FREE OPERATORS
259
260/// Return `true` if the specified `lhs` and `rhs` objects have the same
261/// value, and `false` otherwise. Two `ball::UserFields` objects have the
262/// same value if they have the same number of elements, and each element in
263/// `lhs` has the same value as corresponding element at the same index in
264/// `rhs`.
265bool operator==(const UserFields& lhs, const UserFields& rhs);
266
267/// Return `true` if the specified `lhs` and `rhs` objects do not have the
268/// same value, and `false` otherwise. Two `UserFields` objects do not
269/// have the same value if they have a different number of elements, or if
270/// any element in `lhs` has a different value from the corresponding
271/// element at the same index in `rhs`.
272bool operator!=(const UserFields& lhs, const UserFields& rhs);
273
274/// Write the value of the specified `object` to the specified output
275/// `stream` in a single-line format, and return a reference to `stream`.
276/// If `stream` is not valid on entry, this operation has no effect.
277///
278/// \note Note that this human-readable format is not fully specified, can change
279/// without notice, and is logically equivalent to:
280/// @code
281/// print(stream, 0, -1);
282/// @endcode
283bsl::ostream& operator<<(bsl::ostream& stream, const UserFields& object);
284
285// FREE FUNCTIONS
286
287/// Swap the value of the specified `a` object with the value of the
288/// specified `b` object. If `a` and `b` were created with the same
289/// allocator, then this method provides the no-throw exception-safety
290/// guarantee; otherwise, it provides the basic guarantee.
292
293// ============================================================================
294// INLINE DEFINITIONS
295// ============================================================================
296
297 // ----------------
298 // class UserFields
299 // ----------------
300
301// CREATORS
302inline
304: d_values(basicAllocator)
305{
306}
307
308inline
310 bslma::Allocator *basicAllocator)
311: d_values(original.d_values, basicAllocator)
312{
313}
314
315// MANIPULATORS
316inline
318{
319 d_values = rhs.d_values;
320 return *this;
321}
322
323inline
325{
326 d_values.clear();
327}
328
329inline
331{
332 d_values.emplace_back(value);
333}
334
335
336inline
338{
339 d_values.emplace_back();
340}
341
342inline
347
348inline
349void UserFields::appendDouble(double value)
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 d_values.emplace_back(value);
370}
371
372inline
374{
375 return d_values[index];
376}
377
378inline
380{
381 return d_values[index];
382}
383
384inline
386{
387 BSLS_ASSERT(allocator() == other.allocator());
388
389 d_values.swap(other.d_values);
390}
391
392// ACCESSORS
393inline
395{
396 return d_values.get_allocator().mechanism();
397}
398
399inline
401{
402 return d_values.begin();
403}
404
405inline
407{
408 return d_values.end();
409}
410
411inline
413{
414 return static_cast<int>(d_values.size());
415}
416
417inline
419{
420 return d_values[index];
421}
422
423inline
424const UserFieldValue& UserFields::value(int index) const
425{
426 return d_values[index];
427}
428
429} // close package namespace
430
431// FREE OPERATORS
432inline
433bool ball::operator==(const UserFields& lhs, const UserFields& rhs)
434{
435 return lhs.d_values == rhs.d_values;
436}
437
438inline
439bool ball::operator!=(const UserFields& lhs, const UserFields& rhs)
440{
441 return !(lhs == rhs);
442}
443
444inline
445bsl::ostream& ball::operator<<(bsl::ostream& stream, const UserFields& object)
446{
447 return object.print(stream, 0, -1);
448}
449
450// FREE FUNCTIONS
451inline
452void ball::swap(UserFields& a, UserFields& b)
453{
454 bslalg::SwapUtil::swap(&a.d_values, &b.d_values);
455}
456
457
458
459#endif
460
461// ----------------------------------------------------------------------------
462// Copyright 2015 Bloomberg Finance L.P.
463//
464// Licensed under the Apache License, Version 2.0 (the "License");
465// you may not use this file except in compliance with the License.
466// You may obtain a copy of the License at
467//
468// http://www.apache.org/licenses/LICENSE-2.0
469//
470// Unless required by applicable law or agreed to in writing, software
471// distributed under the License is distributed on an "AS IS" BASIS,
472// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
473// See the License for the specific language governing permissions and
474// limitations under the License.
475// ----------------------------- END-OF-FILE ----------------------------------
476
477/** @} */
478/** @} */
479/** @} */
Definition ball_userfieldvalue.h:135
Definition ball_userfields.h:136
void appendDouble(double value)
Definition ball_userfields.h:349
void appendNull()
Append an element having the unset value to this object.
Definition ball_userfields.h:337
void appendInt64(bsls::Types::Int64 value)
Definition ball_userfields.h:343
~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:406
void appendDatetimeTz(const bdlt::DatetimeTz &value)
Definition ball_userfields.h:361
void append(const UserFieldValue &value)
Append the specified value to this object.
Definition ball_userfields.h:330
friend void swap(UserFields &, UserFields &)
void appendCharArray(const bsl::vector< char > &value)
Append the specified value to this object.
Definition ball_userfields.h:367
UserFields & operator=(const UserFields &rhs)
Definition ball_userfields.h:317
ball::UserFieldValue & value(int index)
Definition ball_userfields.h:379
bsl::vector< ball::UserFieldValue >::const_iterator ConstIterator
Definition ball_userfields.h:148
bslma::Allocator * allocator() const
Definition ball_userfields.h:394
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void appendString(const bsl::string_view &value)
Definition ball_userfields.h:355
ConstIterator begin() const
Definition ball_userfields.h:400
void removeAll()
Definition ball_userfields.h:324
int length() const
Return the number of user field values in this object.
Definition ball_userfields.h:412
ball::UserFieldValue & operator[](int index)
Definition ball_userfields.h:373
UserFields(bslma::Allocator *basicAllocator=0)
Definition ball_userfields.h:303
Definition bdlt_datetimetz.h:308
Definition bslstl_stringview.h:471
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2866
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2874
Definition bslstl_vector.h:1120
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:4621
VALUE_TYPE & emplace_back(Args &&... arguments)
Definition bslstl_vector.h:4324
VALUE_TYPE const * const_iterator
Definition bslstl_vector.h:1153
void swap(vector &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:1938
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
Definition bslma_allocator.h:545
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#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
Definition bslma_usesbslmaallocator.h:344
long long Int64
Definition bsls_types.h:134