BDE 4.14.0 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 struct AttributeHash {
179
180 private:
181 // CLASS DATA
182 static int s_hashtableSize; // default hashtable size for which the
183 // hash value is calculated
184 public:
185 // ACCESSORS
186
187 /// Return the hash value of the specified `attribute`.
188 int operator()(const Attribute& attribute) const
189 {
190 return Attribute::hash(attribute, s_hashtableSize);
191 }
192 };
193
194 // CLASS DATA
195 static int s_initialSize; // initial size of the
196 // attribute map
197
198 // DATA
200 d_attributeSet; // hash table that stores
201 // all the attributes
202 // managed by this object
203
204 public:
205 // TRAITS
208
209 // TYPES
210
211 /// This `typedef` is an alias for the allocator used by this object.
213
215 const_iterator; // type of iterator for
216 // iterating through the
217 // non-modifiable attributes
218 // managed by this object
219
220 // CREATORS
221
223 /// Create an empty `DefaultAttributeContainer` object. Optionally
224 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
225 /// object) to supply memory; otherwise, the default allocator is used.
226 explicit DefaultAttributeContainer(const allocator_type& allocator);
227
228 /// Create a `DefaultAttributeContainer` object having the same value as
229 /// the specified `original` object. Optionally specify an `allocator`
230 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
231 /// otherwise, the default allocator is used.
233 const DefaultAttributeContainer& original,
234 const allocator_type& allocator = allocator_type());
235
236 /// Destroy this object.
238
239 // MANIPULATORS
240
241 /// Assign to this object the value of the specified `rhs` object, and
242 /// return a non-`const` reference to this object.
244
245 /// Add an attribute having the specified `value` to this object.
246 /// Return `true` on success and `false` if an attribute having the
247 /// same `value` already exists in this object.
248 bool addAttribute(const Attribute& value);
249
250 /// Remove the attribute having the specified `value` from this object.
251 /// Return the `true` on success and `false` if the attribute having the
252 /// `value` does not exist in this object.
253 bool removeAttribute(const Attribute& value);
254
255 /// Remove every attribute in this attribute set.
256 void removeAllAttributes();
257
258 // ACCESSORS
259
260 /// Return the number of attributes managed by this object.
261 int numAttributes() const;
262
263 /// Return `true` if the attribute having specified `value` exists in
264 /// this object, and `false` otherwise.
265 bool hasValue(const Attribute& value) const BSLS_KEYWORD_OVERRIDE;
266
267 /// Return an iterator pointing at the beginning of the (unordered)
268 /// sequence of attributes managed by this map, or `end()` if
269 /// `numAttributes()` is 0.
270 const_iterator begin() const;
271
272 /// Return an iterator pointing at one past the end of the map.
273 const_iterator end() const;
274
275 /// Format this object to the specified output `stream` at the (absolute
276 /// value of) the optionally specified indentation `level` and return a
277 /// reference to `stream`. If `level` is specified, optionally specify
278 /// `spacesPerLevel`, the number of spaces per indentation level for
279 /// this and all of its nested objects. If `level` is negative,
280 /// suppress indentation of the first line. If `spacesPerLevel` is
281 /// negative, format the entire output on one line, suppressing all but
282 /// the initial indentation (as governed by `level`). If `stream` is
283 /// not valid on entry, this operation has no effect.
284 bsl::ostream& print(bsl::ostream& stream,
285 int level = 0,
286 int spacesPerLevel = 4) const
288
289 /// Invoke the specified `visitor` function for all attributes in this
290 /// container.
292 const bsl::function<void(const ball::Attribute&)> &visitor) const
294
295 // Aspects
296
297 /// Return the allocator used by this object to supply memory. Note
298 /// that if no allocator was supplied at construction the default
299 /// allocator in effect at construction is used.
301
302};
303
304// FREE OPERATORS
305
306/// Return `true` if the specified `lhs` and `rhs` objects have the same
307/// value, and `false` otherwise. Two `DefaultAttributeContainer` objects
308/// have the same value if they contain the same number of (unique)
309/// attributes, and every attribute that appears in one object also appears
310/// in the other.
311bool operator==(const DefaultAttributeContainer& lhs,
312 const DefaultAttributeContainer& rhs);
313
314/// Return `true` if the specified `lhs` and `rhs` objects do not have the
315/// same value, and `false` otherwise. Two `DefaultAttributeContainer`
316/// objects do not have the same value if they contain differing numbers of
317/// attributes or if there is at least one attribute that appears in one
318/// object, but not in the other.
319bool operator!=(const DefaultAttributeContainer& lhs,
320 const DefaultAttributeContainer& rhs);
321
322/// Write the value of the specified `attributeContainer` to the specified
323/// `output` stream in some single-line, human readable format. Return the
324/// `output` stream.
325bsl::ostream& operator<<(bsl::ostream& output,
326 const DefaultAttributeContainer& attributeContainer);
327
328// ============================================================================
329// INLINE DEFINITIONS
330// ============================================================================
331
332 // -------------------------------
333 // class DefaultAttributeContainer
334 // -------------------------------
335
336// CREATORS
337inline
339: d_attributeSet(s_initialSize, // initial size
340 AttributeHash(), // hash functor
341 bsl::equal_to<Attribute>(), // equal functor
343{
344}
345
346inline
348 const allocator_type& allocator)
349: d_attributeSet(s_initialSize, // initial size
350 AttributeHash(), // hash functor
351 bsl::equal_to<Attribute>(), // equal functor
352 allocator)
353{
354}
355
356inline
358 const DefaultAttributeContainer& original,
359 const allocator_type& allocator)
360: d_attributeSet(original.d_attributeSet, allocator)
361{
362}
363
364inline
368
369// MANIPULATORS
370inline
372{
373 return d_attributeSet.insert(value).second;
374}
375
376inline
378{
379 return d_attributeSet.erase(value) != 0;
380}
381
382inline
384{
385 d_attributeSet.clear();
386}
387
388// ACCESSORS
389inline
391{
392 return static_cast<int>(d_attributeSet.size());
393}
394
395inline
398{
399 return d_attributeSet.begin();
400}
401
402inline
405{
406 return d_attributeSet.end();
407}
408
409 // Aspects
410
411inline
414{
415 return d_attributeSet.get_allocator();
416}
417
418} // close package namespace
419
420// FREE OPERATORS
421inline
422bsl::ostream& ball::operator<<(
423 bsl::ostream& output,
424 const DefaultAttributeContainer& attributeContainer)
425{
426 return attributeContainer.print(output, 0, -1);
427}
428
429
430
431#endif
432
433// ----------------------------------------------------------------------------
434// Copyright 2015 Bloomberg Finance L.P.
435//
436// Licensed under the Apache License, Version 2.0 (the "License");
437// you may not use this file except in compliance with the License.
438// You may obtain a copy of the License at
439//
440// http://www.apache.org/licenses/LICENSE-2.0
441//
442// Unless required by applicable law or agreed to in writing, software
443// distributed under the License is distributed on an "AS IS" BASIS,
444// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
445// See the License for the specific language governing permissions and
446// limitations under the License.
447// ----------------------------- END-OF-FILE ----------------------------------
448
449/** @} */
450/** @} */
451/** @} */
Definition ball_attributecontainer.h:426
Definition ball_attribute.h:198
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:413
bsl::unordered_set< Attribute, AttributeHash >::const_iterator const_iterator
Definition ball_defaultattributecontainer.h:215
bsl::allocator< char > allocator_type
This typedef is an alias for the allocator used by this object.
Definition ball_defaultattributecontainer.h:212
bool addAttribute(const Attribute &value)
Definition ball_defaultattributecontainer.h:371
const_iterator end() const
Return an iterator pointing at one past the end of the map.
Definition ball_defaultattributecontainer.h:404
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const BSLS_KEYWORD_OVERRIDE
bool removeAttribute(const Attribute &value)
Definition ball_defaultattributecontainer.h:377
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_defaultattributecontainer.h:390
void removeAllAttributes()
Remove every attribute in this attribute set.
Definition ball_defaultattributecontainer.h:383
bool hasValue(const Attribute &value) const BSLS_KEYWORD_OVERRIDE
~DefaultAttributeContainer() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition ball_defaultattributecontainer.h:365
DefaultAttributeContainer()
Definition ball_defaultattributecontainer.h:338
const_iterator begin() const
Definition ball_defaultattributecontainer.h:397
Definition bslma_bslallocator.h:580
Definition bslstl_unorderedset.h:704
void swap(unordered_set &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:1219
pair< iterator, bool > insert(const value_type &value)
Definition bslstl_unorderedset.h:2283
unordered_set &operator=(BloombergLP::bslmf::MovableRef< unordered_set > rhs) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(AllocatorTraits iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2126
ALLOCATOR get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2411
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedset.h:2134
iterator erase(const_iterator position)
Definition bslstl_unorderedset.h:2224
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this set.
Definition bslstl_unorderedset.h:2469
iterator const_iterator
Definition bslstl_unorderedset.h:767
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
Definition bdlb_printmethods.h:283
Definition bslma_usesbslmaallocator.h:343