BDE 4.14.0 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/// (Note that an attribute is a compound entity that, as a whole, must be
122/// unique although individual parts need not be.) Additionally, the
123/// `evaluate` accessor can be used to determine if every attribute in the
124/// set is present in the specified attribute container list.
125///
126/// See @ref ball_managedattributeset
128
129 // PRIVATE TYPES
130
131 /// A hash functor for `ManagedAttribute`.
132 struct AttributeHash {
133
134 private:
135 // CLASS DATA
136 static int s_hashtableSize; // default hashtable size for which the
137 // hash value is calculated
138 public:
139 // ACCESSORS
140
141 /// Return the hash value of the specified `attribute`.
142 int operator()(const ManagedAttribute& attribute) const
143 {
144 return ManagedAttribute::hash(attribute, s_hashtableSize);
145 }
146 };
147
148 /// This `typedef` is an alias for the container of managed attributes
149 /// used by this object.
151
152 // CLASS DATA
153 static int s_initialSize; // the initial size of the set
154
155 // DATA
156 SetType d_attributeSet; // the set of attributes
157
158 // FRIENDS
159 friend bool operator==(const ManagedAttributeSet&,
160 const ManagedAttributeSet&);
161 friend bool operator!=(const ManagedAttributeSet&,
162 const ManagedAttributeSet&);
163 friend bsl::ostream& operator<<(bsl::ostream&, const ManagedAttributeSet&);
164
165 public:
166 // TYPES
168
170
171 // TRAITS
174
175 // CLASS METHODS
176
177 /// Return a hash value calculated from the specified `set` using the
178 /// specified `size` as the number of slots. The hash value is
179 /// guaranteed to be in the range `[0 .. size - 1]`. The behavior is
180 /// undefined unless `0 < size`.
181 static int hash(const ManagedAttributeSet& set, int size);
182
183 // CREATORS
184
186 /// Create an empty `ManagedAttributeSet` object. Optionally specify an
187 /// `allocator` (e.g., the address of a `bslma::Allocator` object) to
188 /// supply memory; otherwise, the default allocator is used.
189 explicit ManagedAttributeSet(const allocator_type& allocator);
190
191 /// Create a `ManagedAttributeSet` object having the same value as the
192 /// specified `original` object. Optionally specify an `allocator`
193 /// (e.g., the address of a `bslma::Allocator` object) to supply memory;
194 /// otherwise, the default allocator is used.
196 const ManagedAttributeSet& original,
197 const allocator_type& allocator = allocator_type());
198
199 /// Destroy this attribute set.
201
202 // MANIPULATORS
203
204 /// Assign the value of the specified `rhs` to this object, and return a
205 /// reference providing modifiable access to this object.
207
208 /// Add an attribute having the specified `value` to this object.
209 /// Return `true` on success and `false` if an attribute having the
210 /// same `value` already exists in this object.
211 bool addAttribute(const ManagedAttribute& value);
212
213 /// Add an attribute having the specified `value` to this object.
214 /// Return 1 on success and 0 if an attribute having the same value
215 /// already exists in this object.
216 /// @deprecated Use @ref addAttribute instead.
217 int addPredicate(const ManagedAttribute& value);
218
219 /// Remove all attributes from this attribute set.
220 void removeAll();
221
222 /// @deprecated Use @ref removeAll instead.
223 void removeAllPredicates();
224
225 /// Remove the attribute having the specified `value` from this object.
226 /// Return `true` on success and `false` if an attribute having the
227 /// `value` does not exist in this object.
228 bool removeAttribute(const ManagedAttribute& value);
229
230 /// Remove the attribute having the specified `value` from this object.
231 /// Return the number of attributes removed (i.e., 1 on success and 0 if
232 /// an attribute having `value` does not exist in this object).
233 /// @deprecated Use @ref removeAttribute instead.
234 int removePredicate(const ManagedAttribute& value);
235
236 // ACCESSORS
237
238 /// Return `true` if for every attribute maintained by this object, an
239 /// attribute with the same name and value exists in the specified
240 /// `containerList`, or if this object has no attributes; otherwise
241 /// return `false`.
242 bool evaluate(const AttributeContainerList& containerList) const;
243
244 /// Return the allocator used by this object to supply memory. Note
245 /// that if no allocator was supplied at construction the default
246 /// allocator in effect at construction is used.
248
249 /// Return `true` if an attribute having specified `value` exists in
250 /// this object, and `false` otherwise.
251 bool isMember(const ManagedAttribute& value) const;
252
253 /// Return the number of attributes managed by this object.
254 int numAttributes() const;
255
256 /// @deprecated Use @ref numAttributes instead.
257 int numPredicates() const;
258
259 /// Return an iterator referring to the first member of this attribute
260 /// set.
261 const_iterator begin() const;
262
263 /// Return an iterator referring to one past the last member of this
264 /// attribute set.
265 const_iterator end() const;
266
267 /// Format this object to the specified output `stream` at the (absolute
268 /// value of) the optionally specified indentation `level` and return a
269 /// reference to `stream`. If `level` is specified, optionally specify
270 /// `spacesPerLevel`, the number of spaces per indentation level for
271 /// this and all of its nested objects. If `level` is negative,
272 /// suppress indentation of the first line. If `spacesPerLevel` is
273 /// negative, format the entire output on one line, suppressing all but
274 /// the initial indentation (as governed by `level`). If `stream` is
275 /// not valid on entry, this operation has no effect.
276 bsl::ostream& print(bsl::ostream& stream,
277 int level = 0,
278 int spacesPerLevel = 4) const;
279};
280
281// FREE OPERATORS
282
283/// Return `true` if the specified `lhs` and `rhs` objects have the same
284/// value, and `false` otherwise. Two `ManagedAttributeSet` objects have
285/// the same value if they have the same number of attributes and every
286/// attribute value that appears in one object also appears in the other.
287bool operator==(const ManagedAttributeSet& lhs,
288 const ManagedAttributeSet& rhs);
289
290/// Return `true` if the specified `lhs` and `rhs` objects do not have the
291/// same value, and `false` otherwise. Two `ManagedAttributeSet` objects do
292/// not have the same value if they do not have the same number of
293/// attributes or there is at least one attribute value that appears in one
294/// object, but not in the other.
295bool operator!=(const ManagedAttributeSet& lhs,
296 const ManagedAttributeSet& rhs);
297
298/// Write the value of the specified `attributeSet` to the specified
299/// `output` stream. Return the specified `output` stream.
300bsl::ostream& operator<<(bsl::ostream& output,
301 const ManagedAttributeSet& attributeSet);
302
303// ============================================================================
304// INLINE DEFINITIONS
305// ============================================================================
306
307 // -------------------------
308 // class ManagedAttributeSet
309 // -------------------------
310
311// CREATORS
312inline
314: d_attributeSet(s_initialSize, // initial size
315 AttributeHash(), // hash functor
316 bsl::equal_to<ManagedAttribute>()) // equal functor
317{
318}
319
320inline
322: d_attributeSet(s_initialSize, // initial size
323 AttributeHash(), // hash functor
324 bsl::equal_to<ManagedAttribute>(), // equal functor
325 allocator.mechanism())
326{
327}
328
329inline
331 const allocator_type& allocator)
332: d_attributeSet(original.d_attributeSet, allocator)
333{
334}
335
336// MANIPULATORS
337inline
339{
340 return d_attributeSet.insert(value).second;
341}
342
343inline
345{
346 return addAttribute(value);
347}
348
349inline
351{
352 return !!d_attributeSet.erase(value);
353}
354
355inline
357{
358 return static_cast<int>(d_attributeSet.erase(value));
359}
360
361inline
363{
364 d_attributeSet.clear();
365}
366
367inline
372
373// ACCESSORS
374inline
377{
378 return d_attributeSet.get_allocator();
379}
380
381inline
383{
384 return d_attributeSet.find(value) != d_attributeSet.end();
385}
386
387inline
389{
390 return static_cast<int>(d_attributeSet.size());
391}
392
393inline
395{
396 return numAttributes();
397}
398
399inline
401{
402 return d_attributeSet.begin();
403}
404
405inline
407{
408 return d_attributeSet.end();
409}
410
411} // close package namespace
412
413// FREE OPERATORS
414inline
415bsl::ostream& ball::operator<<(bsl::ostream& output,
416 const ManagedAttributeSet& attributeSet)
417{
418 attributeSet.print(output, 0, -1);
419 return output;
420}
421
422
423
424#endif
425
426// ----------------------------------------------------------------------------
427// Copyright 2015 Bloomberg Finance L.P.
428//
429// Licensed under the Apache License, Version 2.0 (the "License");
430// you may not use this file except in compliance with the License.
431// You may obtain a copy of the License at
432//
433// http://www.apache.org/licenses/LICENSE-2.0
434//
435// Unless required by applicable law or agreed to in writing, software
436// distributed under the License is distributed on an "AS IS" BASIS,
437// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
438// See the License for the specific language governing permissions and
439// limitations under the License.
440// ----------------------------- END-OF-FILE ----------------------------------
441
442/** @} */
443/** @} */
444/** @} */
Definition ball_attributecontainerlist.h:267
Definition ball_managedattributeset.h:127
allocator_type get_allocator() const
Definition ball_managedattributeset.h:376
friend bsl::ostream & operator<<(bsl::ostream &, const ManagedAttributeSet &)
int numAttributes() const
Return the number of attributes managed by this object.
Definition ball_managedattributeset.h:388
friend bool operator==(const ManagedAttributeSet &, const ManagedAttributeSet &)
bool removeAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:350
const_iterator end() const
Definition ball_managedattributeset.h:406
const_iterator begin() const
Definition ball_managedattributeset.h:400
BSLMF_NESTED_TRAIT_DECLARATION(ManagedAttributeSet, bslma::UsesBslmaAllocator)
SetType::const_iterator const_iterator
Definition ball_managedattributeset.h:169
ManagedAttributeSet()
Definition ball_managedattributeset.h:313
int numPredicates() const
Definition ball_managedattributeset.h:394
bool addAttribute(const ManagedAttribute &value)
Definition ball_managedattributeset.h:338
void removeAll()
Remove all attributes from this attribute set.
Definition ball_managedattributeset.h:362
friend bool operator!=(const ManagedAttributeSet &, const ManagedAttributeSet &)
int addPredicate(const ManagedAttribute &value)
Definition ball_managedattributeset.h:344
~ManagedAttributeSet()=default
Destroy this attribute set.
ManagedAttributeSet & operator=(const ManagedAttributeSet &rhs)
int removePredicate(const ManagedAttribute &value)
Definition ball_managedattributeset.h:356
static int hash(const ManagedAttributeSet &set, int size)
bool isMember(const ManagedAttribute &value) const
Definition ball_managedattributeset.h:382
bsl::allocator< char > allocator_type
Definition ball_managedattributeset.h:167
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void removeAllPredicates()
Definition ball_managedattributeset.h:368
bool evaluate(const AttributeContainerList &containerList) const
Definition ball_managedattribute.h:117
static int hash(const ManagedAttribute &attribute, int size)
Definition ball_managedattribute.h:312
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
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:1231
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
Definition ball_administration.h:214
bsl::ostream & operator<<(bsl::ostream &output, const Attribute &attribute)
Definition bdlb_printmethods.h:283
Definition bslma_usesbslmaallocator.h:343