BDE 4.39.x Production Release
Loading...
Searching...
No Matches
ball_thresholdaggregate.h
Go to the documentation of this file.
1/// @file ball_thresholdaggregate.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_thresholdaggregate.h -*-C++-*-
8#ifndef INCLUDED_BALL_THRESHOLDAGGREGATE
9#define INCLUDED_BALL_THRESHOLDAGGREGATE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_thresholdaggregate ball_thresholdaggregate
15/// @brief Provide an aggregate of the four logging threshold levels.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_thresholdaggregate
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_thresholdaggregate-purpose"> Purpose</a>
25/// * <a href="#ball_thresholdaggregate-classes"> Classes </a>
26/// * <a href="#ball_thresholdaggregate-description"> Description </a>
27/// * <a href="#ball_thresholdaggregate-usage"> Usage </a>
28/// * <a href="#ball_thresholdaggregate-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#ball_thresholdaggregate-purpose}
31/// Provide an aggregate of the four logging threshold levels.
32///
33/// # Classes {#ball_thresholdaggregate-classes}
34///
35/// - ball::ThresholdAggregate: aggregate of four logging threshold levels
36/// - ball::ThresholdAggregateUtil: `ball::ThresholdAggregate` helpers
37///
38/// @see ball_severity
39///
40/// # Description {#ball_thresholdaggregate-description}
41/// This component implements a class, `ball::ThresholdAggregate`,
42/// that aggregates four threshold levels: record level, pass-through level,
43/// trigger level, and trigger-all level. Each of these levels must in the
44/// range `[0 .. 255]`, and represents a threshold which, if exceeded, will
45/// invoke a certain action. `ball::ThresholdAggregateUtil` provides helper
46/// methods for packing/unpacking `ball::ThresholdAggregate` objects to/from
47/// integers.
48///
49/// ## Usage {#ball_thresholdaggregate-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Usage {#ball_thresholdaggregate-example-1-basic-usage}
55///
56///
57/// The following code fragment illustrates some of the basic operations on
58/// threshold levels provided by `ball::ThresholdAggregate`.
59/// @code
60/// ball::ThresholdAggregate levels(192, 160, 128, 96);
61/// assert(192 == levels.recordLevel());
62/// assert(160 == levels.passLevel());
63/// assert(128 == levels.triggerLevel());
64/// assert( 96 == levels.triggerAllLevel());
65///
66/// levels.setLevels(160, 128, 96, 64);
67/// assert(160 == levels.recordLevel());
68/// assert(128 == levels.passLevel());
69/// assert( 96 == levels.triggerLevel());
70/// assert( 64 == levels.triggerAllLevel());
71/// @endcode
72/// @}
73/** @} */
74/** @} */
75
76/** @addtogroup bal
77 * @{
78 */
79/** @addtogroup ball
80 * @{
81 */
82/** @addtogroup ball_thresholdaggregate
83 * @{
84 */
85
86#include <balscm_version.h>
87
88#include <bsl_cstring.h>
89#include <bsl_iosfwd.h>
90
91
92namespace ball {
93
94 // ========================
95 // class ThresholdAggregate
96 // ========================
97
98/// This class implements an aggregate of the four logging threshold levels.
99///
100/// See @ref ball_thresholdaggregate
102
103 // DATA
104 unsigned char d_recordLevel; // record
105 unsigned char d_passLevel; // pass
106 unsigned char d_triggerLevel; // trigger
107 unsigned char d_triggerAllLevel; // trigger-all
108
109 // FRIENDS
110 friend bool operator==(const ThresholdAggregate&,
111 const ThresholdAggregate&);
112 public:
113 // CLASS METHODS
114
115 /// Return a hash value calculated from the specified threshold
116 /// `aggregate` using the specified `size` as the number of slots. The
117 /// hash value is guaranteed to be in the range `[0, size - 1]`.
118 ///
119 /// \pre The behavior is undefined unless `0 < size`.
120 static int hash(const ThresholdAggregate& aggregate, int size);
121
122 /// Return `true` if each of the specified `recordLevel`, `passLevel`,
123 /// `triggerLevel`, and `triggerAllLevel` threshold values is in the
124 /// range `[0 .. 255]`, and `false` otherwise.
125 static bool areValidThresholdLevels(int recordLevel,
126 int passLevel,
127 int triggerLevel,
128 int triggerAllLevel);
129
130 /// Return the numerically greatest of the specified `recordLevel`,
131 /// `passLevel`, `triggerLevel`, and `triggerAllLevel` values.
132 static int maxLevel(int recordLevel,
133 int passLevel,
134 int triggerLevel,
135 int triggerAllLevel);
136
137 /// Return the numerically greatest of the four logging threshold levels
138 /// held by the specified threshold `aggregate`.
139 static int maxLevel(const ThresholdAggregate& aggregate);
140
141 // CREATORS
142
143 /// Create a threshold aggregate whose threshold levels are all 0.
145
146 /// Create a threshold aggregate whose threshold levels are the
147 /// specified `recordLevel`, `passLevel`, `triggerLevel`, and `triggerAllLevel`, respectively.
148 ///
149 /// \pre The behavior is undefined if any
150 /// threshold level is not in the range `[0 .. 255]`.
152 int passLevel,
153 int triggerLevel,
154 int triggerAllLevel);
155
156 /// Create a threshold aggregate having the same value as that of the
157 /// specified `original` threshold aggregate.
158 ThresholdAggregate(const ThresholdAggregate& original);
159
160 /// Destroy this object.
162
163 // MANIPULATORS
164
165 /// Assign the value of the specified `rhs` threshold aggregate to this
166 /// aggregate, and return a reference to this modifiable aggregate.
168
169 /// Set the threshold levels of this threshold aggregate to the
170 /// specified `recordLevel`, `passLevel`, `triggerLevel`, and
171 /// `triggerAllLevel` values, respectively, if each of the specified
172 /// values is in the range `[0 .. 255]`. Return 0 on success, and a
173 /// non-zero value otherwise (with no effect on the threshold levels of
174 /// this aggregate).
176 int passLevel,
177 int triggerLevel,
178 int triggerAllLevel);
179
180 /// Set the record level of this threshold aggregate to the specified
181 /// `recordLevel`.
182 void setRecordLevel(int recordLevel);
183
184 /// Set the pass level of this threshold aggregate to the specified
185 /// `passLevel`.
186 void setPassLevel(int passLevel);
187
188 /// Set the trigger level of this threshold aggregate to the specified
189 /// `triggerLevel`.
191
192 /// Set the trigger-all level of this threshold aggregate to the
193 /// specified `triggerAllLevel`.
195
196 // ACCESSORS
197
198 /// Return the record level of this threshold aggregate.
199 int recordLevel() const;
200
201 /// Return the pass level of this threshold aggregate.
202 int passLevel() const;
203
204 /// Return the trigger level of this threshold aggregate.
205 int triggerLevel() const;
206
207 /// Return the trigger-all level of this threshold aggregate.
208 int triggerAllLevel() const;
209
210 /// Format this object to the specified output `stream` at the
211 /// (absolute value of) the optionally specified indentation `level`
212 /// and return a reference to `stream`. If `level` is specified,
213 /// optionally specify `spacesPerLevel`, the number of spaces per
214 /// indentation level for this and all of its nested objects. If
215 /// `level` is negative, suppress indentation of the first line. If
216 /// `spacesPerLevel` is negative, format the entire output on one line,
217 /// suppressing all but the initial indentation (as governed by
218 /// `level`). If `stream` is not valid on entry, this operation has no
219 /// effect.
220 bsl::ostream& print(bsl::ostream& stream,
221 int level = 0,
222 int spacesPerLevel = 4) const;
223};
224
225 // ============================
226 // class ThresholdAggregateUtil
227 // ============================
228
229/// This class provides helper methods for working with
230/// `ball::ThresholdAggregate` objects.
231///
232/// See @ref ball_thresholdaggregate
234 public:
235 // CLASS METHODS
236
237 /// Return an unsigned integer holding a packed representation of the
238 /// specified `thresholds`.
239 static unsigned pack(ThresholdAggregate thresholds);
240
241 /// Return a threshold aggregate having the value equivalent to one that
242 /// was converted into the specified `packed` representation.
243 ///
244 /// \pre The behavior is undefined unless the value for `packed` originated from a call to
245 /// `pack` in the same process instance.
246 static ThresholdAggregate unpack(unsigned packed);
247};
248
249// FREE OPERATORS
250
251/// Return `true` if the specified `lhs` and `rhs` threshold aggregates have
252/// the same value, and `false` otherwise. Two threshold aggregates have
253/// the same value if all four of their respective threshold levels are the
254/// same.
255bool operator==(const ThresholdAggregate& lhs, const ThresholdAggregate& rhs);
256
257/// Return `true` if the specified `lhs` and `rhs` threshold aggregates do
258/// not have the same value, and `false` otherwise. Two threshold
259/// aggregates do not have the same value if any of their four respective
260/// threshold levels differ.
261bool operator!=(const ThresholdAggregate& lhs, const ThresholdAggregate& rhs);
262
263/// Write the value of the specified threshold `aggregate` to the specified
264/// output `stream` and return a reference to the modifiable `stream`.
265bsl::ostream& operator<<(bsl::ostream& stream,
266 const ThresholdAggregate& aggregate);
267
268// ============================================================================
269// INLINE DEFINITIONS
270// ============================================================================
271
272 // ------------------------
273 // class ThresholdAggregate
274 // ------------------------
275
276// ACCESSORS USED INLINE BY OTHER METHODS
277inline
279{
280 return d_recordLevel;
281}
282
283inline
285{
286 return d_passLevel;
287}
288
289inline
291{
292 return d_triggerLevel;
293}
294
295inline
297{
298 return d_triggerAllLevel;
299}
300
301// CLASS METHODS
302inline
304 int passLevel,
305 int triggerLevel,
306 int triggerAllLevel)
307{
308 enum { k_BITS_PER_CHAR = 8 };
309
311 >> k_BITS_PER_CHAR);
312}
313
314inline
316{
317 return maxLevel(aggregate.recordLevel(), aggregate.passLevel(),
318 aggregate.triggerLevel(), aggregate.triggerAllLevel());
319}
320
321// CREATORS
322inline
324: d_recordLevel(0)
325, d_passLevel(0)
326, d_triggerLevel(0)
327, d_triggerAllLevel(0)
328{
329}
330
331inline
333 int passLevel,
334 int triggerLevel,
335 int triggerAllLevel)
336: d_recordLevel(static_cast<unsigned char>(recordLevel))
337, d_passLevel(static_cast<unsigned char>(passLevel))
338, d_triggerLevel(static_cast<unsigned char>(triggerLevel))
339, d_triggerAllLevel(static_cast<unsigned char>(triggerAllLevel))
340{
341}
342
343inline
345: d_recordLevel(original.d_recordLevel)
346, d_passLevel(original.d_passLevel)
347, d_triggerLevel(original.d_triggerLevel)
348, d_triggerAllLevel(original.d_triggerAllLevel)
349{
350}
351
352// MANIPULATORS
353inline
355{
356 d_recordLevel = static_cast<unsigned char>(recordLevel);
357}
358
359inline
361{
362 d_passLevel = static_cast<unsigned char>(passLevel);
363}
364
365inline
367{
368 d_triggerLevel = static_cast<unsigned char>(triggerLevel);
369}
370
371inline
373{
374 d_triggerAllLevel = static_cast<unsigned char>(triggerAllLevel);
375}
376
377 // ----------------------------
378 // class ThresholdAggregateUtil
379 // ----------------------------
380
381// CLASS METHODS
382inline
384{
385 unsigned char x[4] = {
386 static_cast<unsigned char>(thresholds.recordLevel()),
387 static_cast<unsigned char>(thresholds.passLevel()),
388 static_cast<unsigned char>(thresholds.triggerLevel()),
389 static_cast<unsigned char>(thresholds.triggerAllLevel())
390 };
391 unsigned packed;
392 memcpy(&packed, x, sizeof(x));
393 return packed;
394}
395
396inline
398{
399 unsigned char x[4];
400 memcpy(x, &packed, sizeof(x));
401 return ThresholdAggregate(x[0], x[1], x[2], x[3]);
402}
403
404} // close package namespace
405
406// FREE OPERATORS
407inline
408bool ball::operator==(const ThresholdAggregate& lhs,
409 const ThresholdAggregate& rhs)
410{
411 return lhs.d_recordLevel == rhs.d_recordLevel
412 && lhs.d_passLevel == rhs.d_passLevel
413 && lhs.d_triggerLevel == rhs.d_triggerLevel
414 && lhs.d_triggerAllLevel == rhs.d_triggerAllLevel;
415}
416
417inline
418bool ball::operator!=(const ThresholdAggregate& lhs,
419 const ThresholdAggregate& rhs)
420{
421 return !(lhs == rhs);
422}
423
424inline
425bsl::ostream& ball::operator<<(bsl::ostream& stream,
426 const ThresholdAggregate& aggregate)
427{
428 return aggregate.print(stream, 0, -1);
429}
430
431
432
433#endif
434
435// ----------------------------------------------------------------------------
436// Copyright 2015 Bloomberg Finance L.P.
437//
438// Licensed under the Apache License, Version 2.0 (the "License");
439// you may not use this file except in compliance with the License.
440// You may obtain a copy of the License at
441//
442// http://www.apache.org/licenses/LICENSE-2.0
443//
444// Unless required by applicable law or agreed to in writing, software
445// distributed under the License is distributed on an "AS IS" BASIS,
446// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
447// See the License for the specific language governing permissions and
448// limitations under the License.
449// ----------------------------- END-OF-FILE ----------------------------------
450
451/** @} */
452/** @} */
453/** @} */
Definition ball_thresholdaggregate.h:233
static ThresholdAggregate unpack(unsigned packed)
Definition ball_thresholdaggregate.h:397
static unsigned pack(ThresholdAggregate thresholds)
Definition ball_thresholdaggregate.h:383
Definition ball_thresholdaggregate.h:101
int triggerLevel() const
Return the trigger level of this threshold aggregate.
Definition ball_thresholdaggregate.h:290
int recordLevel() const
Return the record level of this threshold aggregate.
Definition ball_thresholdaggregate.h:278
~ThresholdAggregate()=default
Destroy this object.
void setRecordLevel(int recordLevel)
Definition ball_thresholdaggregate.h:354
void setTriggerAllLevel(int triggerAllLevel)
Definition ball_thresholdaggregate.h:372
int passLevel() const
Return the pass level of this threshold aggregate.
Definition ball_thresholdaggregate.h:284
void setTriggerLevel(int triggerLevel)
Definition ball_thresholdaggregate.h:366
friend bool operator==(const ThresholdAggregate &, const ThresholdAggregate &)
void setPassLevel(int passLevel)
Definition ball_thresholdaggregate.h:360
static int hash(const ThresholdAggregate &aggregate, int size)
ThresholdAggregate & operator=(const ThresholdAggregate &rhs)
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
static int maxLevel(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
ThresholdAggregate()
Create a threshold aggregate whose threshold levels are all 0.
Definition ball_thresholdaggregate.h:323
int setLevels(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
static bool areValidThresholdLevels(int recordLevel, int passLevel, int triggerLevel, int triggerAllLevel)
Definition ball_thresholdaggregate.h:303
int triggerAllLevel() const
Return the trigger-all level of this threshold aggregate.
Definition ball_thresholdaggregate.h:296
#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)
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