BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_managedattributeset.h
Go to the documentation of this file.
1/// @file ball_managedattributeset.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_managedattributeset.h -*-C++-*-
8#ifndef INCLUDED_BALL_MANAGEDATTRIBUTESET
9#define INCLUDED_BALL_MANAGEDATTRIBUTESET
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_managedattributeset ball_managedattributeset
15/// @brief Provide a container for managed attributes.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_managedattributeset
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_managedattributeset-purpose"> Purpose</a>
25/// * <a href="#ball_managedattributeset-classes"> Classes </a>
26/// * <a href="#ball_managedattributeset-description"> Description </a>
27/// * <a href="#ball_managedattributeset-usage"> Usage </a>
28/// * <a href="#ball_managedattributeset-example-1-basic-properties-of-ball-managedattributeset"> Example 1: Basic Properties of ball::ManagedAttributeSet </a>
29///
30/// # Purpose {#ball_managedattributeset-purpose}
31/// Provide a container for managed attributes.
32///
33/// # Classes {#ball_managedattributeset-classes}
34///
35/// - ball::ManagedAttributeSet: a container for managed attributes
36///
37/// @see ball_managedattribute, ball_rule
38///
39/// # Description {#ball_managedattributeset-description}
40/// This component implements a value-semantic container class,
41/// `ball::ManagedAttributeSet`, that manages a set of `ball::ManagedAttribute`
42/// objects.
43///
44/// This component participates in the implementation of "Rule-Based Logging".
45/// For more information on how to use that feature, please see the
46/// package-level documentation and usage examples for "Rule-Based Logging".
47///
48/// ## Usage {#ball_managedattributeset-usage}
49///
50///
51/// In this section we show intended usage of this component.
52///
53/// ### Example 1: Basic Properties of ball::ManagedAttributeSet {#ball_managedattributeset-example-1-basic-properties-of-ball-managedattributeset}
54///
55///
56/// This example shows basic operations on a managed attribute set.
57///
58/// First, we create an empty attribute set:
59/// @code
60/// ball::ManagedAttributeSet attributeSet;
61/// @endcode
62/// Then, we add two attributes to the attribute set:
63/// @code
64/// ball::ManagedAttribute p1("uuid", 4044457);
65/// assert(attributeSet.addAttribute(p1));
66/// assert(attributeSet.addAttribute(ball::ManagedAttribute("uuid", 3133246)));
67/// @endcode
68/// Next, we look up (by value) via the `isMember` method:
69/// @code
70/// assert(attributeSet.isMember(p1));
71/// assert(attributeSet.isMember(ball::ManagedAttribute("uuid", 3133246)));
72/// @endcode
73/// Then, we add duplicated value and observe the status of the operation:
74/// @code
75/// assert(!attributeSet.addAttribute(ball::ManagedAttribute("uuid",
76/// 3133246)));
77/// @endcode
78/// Finally, we remove an attribute and check that it is not a member of the
79/// attribute set:
80/// @code
81/// assert(attributeSet.removeAttribute(p1));
82/// assert(!attributeSet.isMember(p1));
83/// @endcode
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bal
89 * @{
90 */
91/** @addtogroup ball
92 * @{
93 */
94/** @addtogroup ball_managedattributeset
95 * @{
96 */
97
98#include <balscm_version.h>
99
100#include <ball_attribute.h>
102
103#include <bslma_allocator.h>
105
107
108#include <bsl_functional.h>
109#include <bsl_unordered_set.h>
110
111
112namespace ball {
113
114class AttributeContainerList;
115
116 // =========================
117 // class ManagedAttributeSet
118 // =========================
119
120/// This class implements a value-semantic collection of unique attributes. (
121///
122/// \note Note that an attribute is a compound entity that, as a whole, must be
123/// unique although individual parts need not be.) Additionally, the
124/// `evaluate` accessor can be used to determine if every attribute in the
125/// set is present in the specified attribute container list.
126///
127/// See @ref ball_managedattributeset
129
130 // PRIVATE TYPES
131
132 /// A hash functor for `ManagedAttribute`.
133 ///
134 /// See @ref ball_managedattributeset
135 struct AttributeHash {
136
137 private:
138 // CLASS DATA
139 static int s_hashtableSize; // default hashtable size for which the
140 // hash value is calculated
141 public:
142 // ACCESSORS
143
144 /// Return the hash value of the specified `attribute`.
145 int operator()(const ManagedAttribute& attribute) const
146 {
147 return ManagedAttribute::hash(attribute, s_hashtableSize);
148 }
149 };
150
151 /// This `typedef` is an alias for the container of managed attributes
152 /// used by this object.
154
155 // CLASS DATA
156 static int s_initialSize; // the initial size of the set
157
158 // DATA
159 SetType d_attributeSet; // the set of attributes
160
161 // FRIENDS
162 friend bool operator==(const ManagedAttributeSet&,
163 const ManagedAttributeSet&);
164 friend bool operator!=(const ManagedAttributeSet&,
165 const ManagedAttributeSet&);
166 friend bsl::ostream& operator<<(bsl::ostream&, const ManagedAttributeSet&);
167
168 public:
169 // TYPES
171
173
174 // TRAITS
177
178 // CLASS METHODS
179
180 /// Return a hash value calculated from the specified `set` using the
181 /// specified `size` as the number of slots. The hash value is
182 /// guaranteed to be in the range `[0 .. size - 1]`.
183 ///
184 /// \pre The behavior is undefined unless `0 < size`.
185 static int hash(const ManagedAttributeSet& set, int size);
186
187 // CREATORS
188
189 /// Create an empty `ManagedAttributeSet` object. Optionally specify an
190 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to
191 /// supply memory; otherwise, the default allocator is used.
193 explicit ManagedAttributeSet(const allocator_type& allocator);
194
195 /// Create a `ManagedAttributeSet` object having the same value as the
196 /// specified `original` object. Optionally specify an `allocator`
197 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
198 /// otherwise, the default allocator is used.
200 const ManagedAttributeSet& original,
201 const allocator_type& allocator = allocator_type());
202
203 /// Destroy this attribute set.
205
206 // MANIPULATORS
207
208 /// Assign the value of the specified `rhs` to this object, and return a
209 /// reference providing modifiable access to this object.
211
212 /// Add an attribute having the specified `value` to this object. Return
213 /// `true` on success and `false` if an attribute having the same `value`
214 /// already exists in this object.
215 bool addAttribute(const ManagedAttribute& value);
216
217 /// Add an attribute having the specified `value` to this object. Return 1
218 /// on success and 0 if an attribute having the same value already exists
219 /// in this object.
220 ///
221 /// @deprecated Use @ref addAttribute instead.
222 int addPredicate(const ManagedAttribute& value);
223
224 /// Remove all attributes from this attribute set.
225 void removeAll();
226
227 /// @deprecated Use @ref removeAll instead.
228 void removeAllPredicates();
229
230 /// Remove the attribute having the specified `value` from this object.
231 /// Return `true` on success and `false` if an attribute having the
232 /// `value` does not exist in this object.
233 bool removeAttribute(const ManagedAttribute& value);
234
235 /// Remove the attribute having the specified `value` from this object.
236 /// Return the number of attributes removed (i.e., 1 on success and 0 if
237 /// an attribute having `value` does not exist in this object).
238 ///
239 /// @deprecated Use @ref removeAttribute instead.
240 int removePredicate(const ManagedAttribute& value);
241
242 // ACCESSORS
243
244 /// Return `true` if for every attribute maintained by this object, an
245 /// attribute with the same name and value exists in the specified
246 /// `containerList`, or if this object has no attributes; otherwise return
247 /// `false`.
248 bool evaluate(const AttributeContainerList& containerList) const;
249
250 /// Return the allocator used by this object to supply memory.
251 ///
252 /// \note Note that if no allocator was supplied at construction the default allocator in
253 /// effect at construction is used.
255
256 /// Return `true` if an attribute having specified `value` exists in this
257 /// object, and `false` otherwise.
258 bool isMember(const ManagedAttribute& value) const;
259
260 /// Return the number of attributes managed by this object.
261 int numAttributes() const;
262
263 /// @deprecated Use @ref numAttributes instead.
264 int numPredicates() const;
265
266 /// Return an iterator referring to the first member of this attribute set.
267 const_iterator begin() const;
268
269 /// Return an iterator referring to one past the last member of this
270 /// attribute set.
271 const_iterator end() const;
272
273 /// Format this object to the specified output `stream` at the (absolute
274 /// value of) the optionally specified indentation `level` and return a
275 /// reference to `stream`. If `level` is specified, optionally specify
276 /// `spacesPerLevel`, the number of spaces per indentation level for
277 /// this and all of its nested objects. If `level` is negative,
278 /// suppress indentation of the first line. If `spacesPerLevel` is
279 /// negative, format the entire output on one line, suppressing all but
280 /// the initial indentation (as governed by `level`). If `stream` is
281 /// not valid on entry, this operation has no effect.
282 bsl::ostream& print(bsl::ostream& stream,
283 int level = 0,
284 int spacesPerLevel = 4) const;
285};
286
287// FREE OPERATORS
288
289/// Return `true` if the specified `lhs` and `rhs` objects have the same
290/// value, and `false` otherwise. Two `ManagedAttributeSet` objects have
291/// the same value if they have the same number of attributes and every
292/// attribute value that appears in one object also appears in the other.
293bool operator==(const ManagedAttributeSet& lhs,
294 const ManagedAttributeSet& rhs);
295
296/// Return `true` if the specified `lhs` and `rhs` objects do not have the
297/// same value, and `false` otherwise. Two `ManagedAttributeSet` objects do
298/// not have the same value if they do not have the same number of
299/// attributes or there is at least one attribute value that appears in one
300/// object, but not in the other.
301bool operator!=(const ManagedAttributeSet& lhs,
302 const ManagedAttributeSet& rhs);
303
304/// Write the value of the specified `attributeSet` to the specified
305/// `output` stream. Return the specified `output` stream.
306bsl::ostream& operator<<(bsl::ostream& output,
307 const ManagedAttributeSet& attributeSet);
308
309// ============================================================================
310// INLINE DEFINITIONS
311// ============================================================================
312
313 // -------------------------
314 // class ManagedAttributeSet
315 // -------------------------
316
317// CREATORS
318inline
320: d_attributeSet(s_initialSize, // initial size
321 AttributeHash(), // hash functor
322 bsl::equal_to<ManagedAttribute>()) // equal functor
323{
324}
325
326inline
328: d_attributeSet(s_initialSize, // initial size
329 AttributeHash(), // hash functor
330 bsl::equal_to<ManagedAttribute>(), // equal functor
331 allocator.mechanism())
332{
333}
334
335inline
337 const allocator_type& allocator)
338: d_attributeSet(original.d_attributeSet, allocator)
339{
340}
341
342// MANIPULATORS
343inline
345{
346 return d_attributeSet.insert(value).second;
347}
348
349inline
351{
352 return addAttribute(value);
353}
354
355inline
357{
358 return !!d_attributeSet.erase(value);
359}
360
361inline
363{
364 return static_cast<int>(d_attributeSet.erase(value));
365}
366
367inline
369{
370 d_attributeSet.clear();
371}
372
373inline
378
379// ACCESSORS
380inline
383{
384 return d_attributeSet.get_allocator();
385}
386
387inline
389{
390 return d_attributeSet.find(value) != d_attributeSet.end();
391}
392
393inline
395{
396 return static_cast<int>(d_attributeSet.size());
397}
398
399inline
401{
402 return numAttributes();
403}
404
405inline
407{
408 return d_attributeSet.begin();
409}
410
411inline
413{
414 return d_attributeSet.end();
415}
416
417} // close package namespace
418
419// FREE OPERATORS
420inline
421bsl::ostream& ball::operator<<(bsl::ostream& output,
422 const ManagedAttributeSet& attributeSet)
423{
424 attributeSet.print(output, 0, -1);
425 return output;
426}
427
428
429
430#endif
431
432// ----------------------------------------------------------------------------
433// Copyright 2015 Bloomberg Finance L.P.
434//
435// Licensed under the Apache License, Version 2.0 (the "License");
436// you may not use this file except in compliance with the License.
437// You may obtain a copy of the License at
438//
439// http://www.apache.org/licenses/LICENSE-2.0
440//
441// Unless required by applicable law or agreed to in writing, software
442// distributed under the License is distributed on an "AS IS" BASIS,
443// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
444// See the License for the specific language governing permissions and
445// limitations under the License.
446// ----------------------------- END-OF-FILE ----------------------------------
447
448/** @} */
449/** @} */
450/** @} */
Definition ball_attributecontainerlist.h:271
Definition ball_managedattributeset.h:128
allocator_type get_allocator() const
Definition ball_managedattributeset.h:382
friend bsl::ostream & operator<<(bsl::ostream &, const ManagedAttributeSet &)
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_managedattributeset.h:394
friend bool operator==(const ManagedAttributeSet &, const ManagedAttributeSet &)
bool removeAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:356
const_iterator end() const
Definition ball_managedattributeset.h:412
const_iterator begin() const
Return an iterator referring to the first member of this attribute set.
Definition ball_managedattributeset.h:406
BSLMF_NESTED_TRAIT_DECLARATION(ManagedAttributeSet, bslma::UsesBslmaAllocator)
SetType::const_iterator const_iterator
Definition ball_managedattributeset.h:172
ManagedAttributeSet()
Definition ball_managedattributeset.h:319
int numPredicates() const
Definition ball_managedattributeset.h:400
bool addAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:344
void removeAll()
Remove all attributes from this attribute set.
Definition ball_managedattributeset.h:368
friend bool operator!=(const ManagedAttributeSet &, const ManagedAttributeSet &)
int addPredicate(const ManagedAttribute &value)
Definition ball_managedattributeset.h:350
~ManagedAttributeSet()=default
Destroy this attribute set.
ManagedAttributeSet & operator=(const ManagedAttributeSet &rhs)
int removePredicate(const ManagedAttribute &value)
Definition ball_managedattributeset.h:362
static int hash(const ManagedAttributeSet &set, int size)
bool isMember(const ManagedAttribute &value) const
Definition ball_managedattributeset.h:388
bsl::allocator< char > allocator_type
Definition ball_managedattributeset.h:170
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void removeAllPredicates()
Definition ball_managedattributeset.h:374
bool evaluate(const AttributeContainerList &containerList) const
Definition ball_managedattribute.h:117
static int hash(const ManagedAttribute &attribute, int size)
Definition ball_managedattribute.h:315
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
enable_if< BloombergLP::bslmf::IsTransparentPredicate< HASH, LOOKUP_KEY >::value &&BloombergLP::bslmf::IsTransparentPredicate< EQUAL, LOOKUP_KEY >::value, iterator >::type find(const LOOKUP_KEY &key)
Definition bslstl_unorderedset.h:1500
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
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
Definition bdlat_valuetypefunctions.h:939
Definition bslma_usesbslmaallocator.h:344