BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_scopedattributes.h
Go to the documentation of this file.
1/// @file ball_scopedattributes.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_scopedattributes.h -*-C++-*-
8#ifndef INCLUDED_BALL_SCOPEDATTRIBUTES
9#define INCLUDED_BALL_SCOPEDATTRIBUTES
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_scopedattributes ball_scopedattributes
15/// @brief Provide a class to add and remove attributes automatically.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_scopedattributes
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_scopedattributes-purpose"> Purpose</a>
25/// * <a href="#ball_scopedattributes-classes"> Classes </a>
26/// * <a href="#ball_scopedattributes-description"> Description </a>
27/// * <a href="#ball_scopedattributes-usage"> Usage </a>
28/// * <a href="#ball_scopedattributes-example-1-installing-a-ball-attributecontainer"> Example 1: Installing a ball::AttributeContainer </a>
29///
30/// # Purpose {#ball_scopedattributes-purpose}
31/// Provide a class to add and remove attributes automatically.
32///
33/// # Classes {#ball_scopedattributes-classes}
34///
35/// - ball::ScopedAttributes: helper for safely managing attributes
36///
37/// @see ball_attributecontext, ball_attribute
38///
39/// # Description {#ball_scopedattributes-description}
40/// This component provides a type, `ball::ScopedAttributes`,
41/// that serves as a "scoped helper" to safely manage `ball::AttributeContainer`
42/// objects for the current attribute context. A `ball::ScopedAttributes`
43/// object, upon construction, will install a `ball::AttributeContainer` object
44/// in the current attribute context and, more importantly, automatically
45/// remove that `ball::AttributeContainer` object from the current attribute
46/// context upon destruction.
47///
48/// This component is used to help associating an attributes (name-value pairs)
49/// with the current thread context for use when writing log records for the
50/// current thread. This context information can both be written to the log
51/// itself, and used as input when evaluating whether a particular log should be
52/// written. For more information on how to use this feature, please see the
53/// package level documentation and usage examples for "Log Attributes" and
54/// "Rule-Based Logging".
55///
56/// Note that the `ball::AttributeContainer` supplied at construction must
57/// remain valid and *unmodified* for the lifetime of this object.
58///
59/// ## Usage {#ball_scopedattributes-usage}
60///
61///
62/// This section illustrates intended use of this component.
63///
64/// ### Example 1: Installing a ball::AttributeContainer {#ball_scopedattributes-example-1-installing-a-ball-attributecontainer}
65///
66///
67/// In the following code fragment, we will use a `ball::ScopedAttributes` to
68/// install a `ball::AttributeContainer` in the current context.
69///
70/// We first create the current attribute context and two attributes:
71/// @code
72/// ball::AttributeContext *context = ball::AttributeContext::getContext();
73///
74/// ball::Attribute a1("uuid", 4044457);
75/// ball::Attribute a2("name", "Gang Chen");
76/// assert(false == context->hasAttribute(a1));
77/// assert(false == context->hasAttribute(a2));
78/// @endcode
79/// Now we create an `AttributeSet` and add the two attributes to this set,
80/// then we use a 'ball::ScopedAttributes to install these attributes in the
81/// current thread's attribute context.
82///
83/// Note that we use the `AttributeSet` implementation of the
84/// `ball::AttributeContainer` protocol defined in the component documentation
85/// for @ref ball_attributecontainer (the `ball` package provides a similar class
86/// in the @ref ball_defaultattributecontainer component).
87/// @code
88/// {
89/// AttributeSet attributes;
90/// attributes.insert(a1);
91/// attributes.insert(a2);
92/// ball::ScopedAttributes attributeGuard(&attributes);
93/// assert(true == context->hasAttribute(a1));
94/// assert(true == context->hasAttribute(a2));
95/// @endcode
96/// When `attributeGuard` goes out of scope and is destroyed, `attributes` are
97/// removed from the current thread's attribute context, which prevents the
98/// attribute context from referring to an invalid memory address (on the
99/// stack).
100/// @code
101/// }
102///
103/// assert(!context->hasAttribute(a1));
104/// assert(!context->hasAttribute(a2));
105/// @endcode
106/// @}
107/** @} */
108/** @} */
109
110/** @addtogroup bal
111 * @{
112 */
113/** @addtogroup ball
114 * @{
115 */
116/** @addtogroup ball_scopedattributes
117 * @{
118 */
119
120#include <balscm_version.h>
121
125
126
127namespace ball {
128
129 // ======================
130 // class ScopedAttributes
131 // ======================
132
133/// This class installs a `AttributeContainer` object in the current attribute
134/// context on construction, and removes it on destruction. Note that the
135/// `AttributeContainer` supplied at construction must remain valid and
136/// **unmodified** for the lifetime of this object.
137///
138/// See @ref ball_scopedattributes
140
141 // DATA
142 const AttributeContext::iterator d_it; // refers to attributes
143
144 // NOT IMPLEMENTED
146 ScopedAttributes& operator=(const ScopedAttributes&);
147
148 public:
149 // CREATORS
150
151 /// Create a `ScopedAttributes` object having the specified `attributes`.
152 /// Note that `attributes` must remain valid and **unmodified** for the
153 /// lifetime of this object.
154 ScopedAttributes(const AttributeContainer *attributes);
155
156 /// Remove the associated attributes from the current attribute context.
158};
159
160// ============================================================================
161// INLINE DEFINITIONS
162// ============================================================================
163
164 // ----------------------
165 // class ScopedAttributes
166 // ----------------------
167
168// CREATORS
169inline
170ScopedAttributes::ScopedAttributes(const AttributeContainer *attributes)
171: d_it(AttributeContext::getContext()->addAttributes(attributes))
172{
173}
174
175inline
180
181} // close package namespace
182
183
184#endif
185
186// ----------------------------------------------------------------------------
187// Copyright 2015 Bloomberg Finance L.P.
188//
189// Licensed under the Apache License, Version 2.0 (the "License");
190// you may not use this file except in compliance with the License.
191// You may obtain a copy of the License at
192//
193// http://www.apache.org/licenses/LICENSE-2.0
194//
195// Unless required by applicable law or agreed to in writing, software
196// distributed under the License is distributed on an "AS IS" BASIS,
197// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
198// See the License for the specific language governing permissions and
199// limitations under the License.
200// ----------------------------- END-OF-FILE ----------------------------------
201
202/** @} */
203/** @} */
204/** @} */
Definition ball_attributecontainerlist.h:168
Definition ball_attributecontainer.h:426
Definition ball_attributecontext.h:520
static AttributeContext * getContext()
void removeAttributes(iterator element)
Definition ball_attributecontext.h:811
Definition ball_scopedattributes.h:139
~ScopedAttributes()
Remove the associated attributes from the current attribute context.
Definition ball_scopedattributes.h:176
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214