BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_defaultattributecontainer.h
Go to the documentation of this file.
1/// @file ball_defaultattributecontainer.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_defaultattributecontainer.h -*-C++-*-
8#ifndef INCLUDED_BALL_DEFAULTATTRIBUTECONTAINER
9#define INCLUDED_BALL_DEFAULTATTRIBUTECONTAINER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_defaultattributecontainer ball_defaultattributecontainer
15/// @brief Provide a default container for storing attribute name/value pairs.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_defaultattributecontainer
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_defaultattributecontainer-purpose"> Purpose</a>
25/// * <a href="#ball_defaultattributecontainer-classes"> Classes </a>
26/// * <a href="#ball_defaultattributecontainer-description"> Description </a>
27/// * <a href="#ball_defaultattributecontainer-thread-safety"> Thread Safety </a>
28/// * <a href="#ball_defaultattributecontainer-usage"> Usage </a>
29/// * <a href="#ball_defaultattributecontainer-example-1-basic-usage-of-ball-defaultattributecontainer"> Example 1: Basic Usage of ball::DefaultAttributeContainer </a>
30///
31/// # Purpose {#ball_defaultattributecontainer-purpose}
32/// Provide a default container for storing attribute name/value pairs.
33///
34/// # Classes {#ball_defaultattributecontainer-classes}
35///
36/// - ball::DefaultAttributeContainer: a collection of unique attributes
37///
38/// @see ball_attributecontainer
39///
40/// # Description {#ball_defaultattributecontainer-description}
41/// This component provides a default implementation of the
42/// `ball::AttributeContainer` protocol, `ball::DefaultAttributeContainer`
43/// providing an `unordered_set`-based container of `ball::Attribute` values.
44/// Each attribute within the default attribute container holds a
45/// (case-sensitive) name and a value, which may be an `int`, a 64-bit integer,
46/// or a `bsl::string`.
47///
48/// This component participates in the implementation of "Rule-Based Logging".
49/// For more information on how to use that feature, please see the package
50/// level documentation and usage examples for "Rule-Based Logging".
51///
52/// ## Thread Safety {#ball_defaultattributecontainer-thread-safety}
53///
54///
55/// `ball::DefaultAttributeContainer` is *const* *thread-safe*, meaning that
56/// accessors may be invoked concurrently from different threads, but it is not
57/// safe to access or modify a `ball::DefaultAttributeContainer` in one thread
58/// while another thread modifies the same object.
59///
60/// ## Usage {#ball_defaultattributecontainer-usage}
61///
62///
63/// This section illustrates the intended use of this component.
64///
65/// ### Example 1: Basic Usage of ball::DefaultAttributeContainer {#ball_defaultattributecontainer-example-1-basic-usage-of-ball-defaultattributecontainer}
66///
67///
68/// A `ball::DefaultAttributeContainer` initially has no attributes when created
69/// by the default constructor:
70/// @code
71/// ball::DefaultAttributeContainer attributeContainer;
72/// @endcode
73/// Let's now create some attributes and add them to the attribute map:
74/// @code
75/// ball::Attribute a1("uuid", 1111);
76/// ball::Attribute a2("sid", "111-1");
77/// assert(true == attributeContainer.addAttribute(a1));
78/// assert(true == attributeContainer.addAttribute(a2));
79/// @endcode
80/// New attributes with a name that already exists in the map can be added, as
81/// long as they have a different value:
82/// @code
83/// ball::Attribute a3("uuid", 2222);
84/// ball::Attribute a4("sid", "222-2");
85/// assert(true == attributeContainer.addAttribute(a3));
86/// assert(true == attributeContainer.addAttribute(a4));
87/// @endcode
88/// But attributes having the same name and value cannot be added:
89/// @code
90/// ball::Attribute a5("uuid", 1111); // same as 'a1'
91/// assert(false == attributeContainer.addAttribute(a5));
92/// @endcode
93/// Note that the attribute name is case-sensitive:
94/// @code
95/// ball::Attribute a6("UUID", 1111);
96/// assert(true == attributeContainer.addAttribute(a6));
97/// @endcode
98/// Existing attributes can be looked up by the `hasValue` method:
99/// @code
100/// assert(true == attributeContainer.hasValue(a1));
101/// assert(true == attributeContainer.hasValue(a2));
102/// assert(true == attributeContainer.hasValue(a3));
103/// assert(true == attributeContainer.hasValue(a4));
104/// assert(true == attributeContainer.hasValue(a5));
105/// assert(true == attributeContainer.hasValue(a6));
106/// @endcode
107/// Or removed by the `removeAttribute` method:
108/// @code
109/// defaultattributecontainer.removeAttribute(a1);
110/// assert(false == attributeContainer.hasValue(a1));
111/// @endcode
112/// Also, the `ball::DefaultAttributeContainer` class provides an iterator:
113/// @code
114/// ball::DefaultAttributeContainer::const_iterator iter =
115/// attributeContainer.begin();
116/// for ( ; iter != attributeContainer.end(); ++iter ) {
117/// bsl::cout << *iter << bsl::endl;
118/// }
119/// @endcode
120/// Finally, we can provide a visitor functor and visit all attributes in the
121/// container. Note that this usage example uses lambdas and requires C++11.
122/// Lambdas can be replaced with named functions for C++03.
123/// @code
124/// bsl::vector<ball::Attribute> result;
125/// attributeContainer.visitAttributes(
126/// [&result](const ball::Attribute& attribute)
127/// {
128/// result.push_back(attribute);
129/// });
130/// assert(4 == result.size());
131/// @endcode
132/// @}
133/** @} */
134/** @} */
135
136/** @addtogroup bal
137 * @{
138 */
139/** @addtogroup ball
140 * @{
141 */
142/** @addtogroup ball_defaultattributecontainer
143 * @{
144 */
145
146#include <balscm_version.h>
147
148#include <ball_attribute.h>
150
151#include <bslma_allocator.h>
152#include <bslma_bslallocator.h>
154
156
157#include <bsls_keyword.h>
158
159#include <bsl_functional.h>
160#include <bsl_unordered_set.h>
161
162
163namespace ball {
164
165 // ===============================
166 // class DefaultAttributeContainer
167 // ===============================
168
169/// A `DefaultAttributeContainer` object contains a collection of (unique)
170/// attributes values.
171///
172/// See @ref ball_defaultattributecontainer
174
175 // PRIVATE TYPES
176
177 /// A hash functor for `Attribute`.
178 ///
179 /// See @ref ball_defaultattributecontainer
180 struct AttributeHash {
181
182 private:
183 // CLASS DATA
184 static int s_hashtableSize; // default hashtable size for which the
185 // hash value is calculated
186 public:
187 // ACCESSORS
188
189 /// Return the hash value of the specified `attribute`.
190 int operator()(const Attribute& attribute) const
191 {
192 return Attribute::hash(attribute, s_hashtableSize);
193 }
194 };
195
196 // CLASS DATA
197 static int s_initialSize; // initial size of the
198 // attribute map
199
200 // DATA
202 d_attributeSet; // hash table that stores
203 // all the attributes
204 // managed by this object
205
206 public:
207 // TRAITS
210
211 // TYPES
212
213 /// This `typedef` is an alias for the allocator used by this object.
215
217 const_iterator; // type of iterator for
218 // iterating through the
219 // non-modifiable attributes
220 // managed by this object
221
222 // CREATORS
223
224 /// Create an empty `DefaultAttributeContainer` object. Optionally
225 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
226 /// object) to supply memory; otherwise, the default allocator is used.
228 explicit DefaultAttributeContainer(const allocator_type& allocator);
229
230 /// Create a `DefaultAttributeContainer` object having the same value as
231 /// the specified `original` object. Optionally specify an `allocator`
232 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
233 /// otherwise, the default allocator is used.
235 const DefaultAttributeContainer& original,
236 const allocator_type& allocator = allocator_type());
237
238 /// Destroy this object.
240
241 // MANIPULATORS
242
243 /// Assign to this object the value of the specified `rhs` object, and
244 /// return a non-`const` reference to this object.
246
247 /// Add an attribute having the specified `value` to this object.
248 /// Return `true` on success and `false` if an attribute having the
249 /// same `value` already exists in this object.
250 bool addAttribute(const Attribute& value);
251
252 /// Remove the attribute having the specified `value` from this object.
253 /// Return the `true` on success and `false` if the attribute having the
254 /// `value` does not exist in this object.
255 bool removeAttribute(const Attribute& value);
256
257 /// Remove every attribute in this attribute set.
258 void removeAllAttributes();
259
260 // ACCESSORS
261
262 /// Return the number of attributes managed by this object.
263 int numAttributes() const;
264
265 /// Return `true` if the attribute having specified `value` exists in
266 /// this object, and `false` otherwise.
267 bool hasValue(const Attribute& value) const BSLS_KEYWORD_OVERRIDE;
268
269 /// Return an iterator pointing at the beginning of the (unordered)
270 /// sequence of attributes managed by this map, or `end()` if
271 /// `numAttributes()` is 0.
272 const_iterator begin() const;
273
274 /// Return an iterator pointing at one past the end of the map.
275 const_iterator end() const;
276
277 /// Format this object to the specified output `stream` at the (absolute
278 /// value of) the optionally specified indentation `level` and return a
279 /// reference to `stream`. If `level` is specified, optionally specify
280 /// `spacesPerLevel`, the number of spaces per indentation level for
281 /// this and all of its nested objects. If `level` is negative,
282 /// suppress indentation of the first line. If `spacesPerLevel` is
283 /// negative, format the entire output on one line, suppressing all but
284 /// the initial indentation (as governed by `level`). If `stream` is
285 /// not valid on entry, this operation has no effect.
286 bsl::ostream& print(bsl::ostream& stream,
287 int level = 0,
288 int spacesPerLevel = 4) const
290
291 /// Invoke the specified `visitor` function for all attributes in this
292 /// container.
294 const bsl::function<void(const ball::Attribute&)> &visitor) const
296
297 // Aspects
298
299 /// Return the allocator used by this object to supply memory.
300 ///
301 /// \note Note that if no allocator was supplied at construction the default
302 /// allocator in effect at construction is used.
304
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 `DefaultAttributeContainer` objects
311/// have the same value if they contain the same number of (unique)
312/// attributes, and every attribute that appears in one object also appears
313/// in the other.
314bool operator==(const DefaultAttributeContainer& lhs,
315 const DefaultAttributeContainer& rhs);
316
317/// Return `true` if the specified `lhs` and `rhs` objects do not have the
318/// same value, and `false` otherwise. Two `DefaultAttributeContainer`
319/// objects do not have the same value if they contain differing numbers of
320/// attributes or if there is at least one attribute that appears in one
321/// object, but not in the other.
322bool operator!=(const DefaultAttributeContainer& lhs,
323 const DefaultAttributeContainer& rhs);
324
325/// Write the value of the specified `attributeContainer` to the specified
326/// `output` stream in some single-line, human readable format. Return the
327/// `output` stream.
328bsl::ostream& operator<<(bsl::ostream& output,
329 const DefaultAttributeContainer& attributeContainer);
330
331// ============================================================================
332// INLINE DEFINITIONS
333// ============================================================================
334
335 // -------------------------------
336 // class DefaultAttributeContainer
337 // -------------------------------
338
339// CREATORS
340inline
342: d_attributeSet(s_initialSize, // initial size
343 AttributeHash(), // hash functor
344 bsl::equal_to<Attribute>(), // equal functor
346{
347}
348
349inline
351 const allocator_type& allocator)
352: d_attributeSet(s_initialSize, // initial size
353 AttributeHash(), // hash functor
354 bsl::equal_to<Attribute>(), // equal functor
355 allocator)
356{
357}
358
359inline
361 const DefaultAttributeContainer& original,
362 const allocator_type& allocator)
363: d_attributeSet(original.d_attributeSet, allocator)
364{
365}
366
367inline
371
372// MANIPULATORS
373inline
375{
376 return d_attributeSet.insert(value).second;
377}
378
379inline
381{
382 return d_attributeSet.erase(value) != 0;
383}
384
385inline
387{
388 d_attributeSet.clear();
389}
390
391// ACCESSORS
392inline
394{
395 return static_cast<int>(d_attributeSet.size());
396}
397
398inline
401{
402 return d_attributeSet.begin();
403}
404
405inline
408{
409 return d_attributeSet.end();
410}
411
412 // Aspects
413
414inline
417{
418 return d_attributeSet.get_allocator();
419}
420
421} // close package namespace
422
423// FREE OPERATORS
424inline
425bsl::ostream& ball::operator<<(
426 bsl::ostream& output,
427 const DefaultAttributeContainer& attributeContainer)
428{
429 return attributeContainer.print(output, 0, -1);
430}
431
432
433
434#endif
435
436// ----------------------------------------------------------------------------
437// Copyright 2015 Bloomberg Finance L.P.
438//
439// Licensed under the Apache License, Version 2.0 (the "License");
440// you may not use this file except in compliance with the License.
441// You may obtain a copy of the License at
442//
443// http://www.apache.org/licenses/LICENSE-2.0
444//
445// Unless required by applicable law or agreed to in writing, software
446// distributed under the License is distributed on an "AS IS" BASIS,
447// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
448// See the License for the specific language governing permissions and
449// limitations under the License.
450// ----------------------------- END-OF-FILE ----------------------------------
451
452/** @} */
453/** @} */
454/** @} */
Definition ball_attributecontainer.h:426
Definition ball_attribute.h:199
static int hash(const Attribute &attribute, int size)
Definition ball_defaultattributecontainer.h:173
void visitAttributes(const bsl::function< void(const ball::Attribute &)> &visitor) const BSLS_KEYWORD_OVERRIDE
BSLMF_NESTED_TRAIT_DECLARATION(DefaultAttributeContainer, bslma::UsesBslmaAllocator)
allocator_type get_allocator() const
Definition ball_defaultattributecontainer.h:416
bsl::unordered_set< Attribute, AttributeHash >::const_iterator const_iterator
Definition ball_defaultattributecontainer.h:217
bsl::allocator< char > allocator_type
This typedef is an alias for the allocator used by this object.
Definition ball_defaultattributecontainer.h:214
bool addAttribute(const Attribute &value)
Definition ball_defaultattributecontainer.h:374
const_iterator end() const
Return an iterator pointing at one past the end of the map.
Definition ball_defaultattributecontainer.h:407
bool removeAttribute(const Attribute &value)
Definition ball_defaultattributecontainer.h:380
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_defaultattributecontainer.h:393
void removeAllAttributes()
Remove every attribute in this attribute set.
Definition ball_defaultattributecontainer.h:386
bool hasValue(const Attribute &value) const BSLS_KEYWORD_OVERRIDE
~DefaultAttributeContainer() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition ball_defaultattributecontainer.h:368
DefaultAttributeContainer()
Definition ball_defaultattributecontainer.h:341
const_iterator begin() const
Definition ball_defaultattributecontainer.h:400
Definition bslma_bslallocator.h:588
Definition bslstl_unorderedset.h:733
void swap(unordered_set &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:1485
pair< iterator, bool > insert(const value_type &value)
Definition bslstl_unorderedset.h:2751
unordered_set &operator=(BloombergLP::bslmf::MovableRef< unordered_set > rhs) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2594
ALLOCATOR get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2869
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2602
iterator erase(const_iterator position)
Definition bslstl_unorderedset.h:2692
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this set.
Definition bslstl_unorderedset.h:2927
iterator const_iterator
Definition bslstl_unorderedset.h:796
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
Definition bdlat_valuetypefunctions.h:939
Definition bslma_usesbslmaallocator.h:344