BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_attributecollectorregistry.h
Go to the documentation of this file.
1/// @file ball_attributecollectorregistry.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_attributecollectorregistry.h -*-C++-*-
8#ifndef INCLUDED_BALL_ATTRIBUTECOLLECTORREGISTRY
9#define INCLUDED_BALL_ATTRIBUTECOLLECTORREGISTRY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_attributecollectorregistry ball_attributecollectorregistry
15/// @brief Provide a registry for attribute collector functors.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_attributecollectorregistry
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_attributecollectorregistry-purpose"> Purpose</a>
25/// * <a href="#ball_attributecollectorregistry-classes"> Classes </a>
26/// * <a href="#ball_attributecollectorregistry-description"> Description </a>
27/// * <a href="#ball_attributecollectorregistry-thread-safety"> Thread Safety </a>
28/// * <a href="#ball_attributecollectorregistry-usage"> Usage </a>
29/// * <a href="#ball_attributecollectorregistry-example-1-collecting-attributes-from-non-uniform-sources"> Example 1: Collecting Attributes From Non-uniform Sources </a>
30///
31/// # Purpose {#ball_attributecollectorregistry-purpose}
32/// Provide a registry for attribute collector functors.
33///
34/// # Classes {#ball_attributecollectorregistry-classes}
35///
36/// - ball::AttributeCollectorRegistry: registry of attribute collector functors
37///
38/// @see ball_record, ball_loggermanager
39///
40/// # Description {#ball_attributecollectorregistry-description}
41/// This component provides a mechanism,
42/// `ball::AttributeCollectorRegistry`, that allows clients to register
43/// `ball::Attribute` collection functions, and to separately apply a visitor to
44/// all the attributes collected using those collection functions. A client can
45/// register a `ball::Attribute` collection function by calling `addCollector`
46/// and supplying a function matching the
47/// `AttributeCollectorRegistry::Collector` signature. Clients can also apply a
48/// visitor to collected `ball::Attribute` objects by calling `collect` and
49/// supplying a function matching the
50/// `ball::AttributeCollectorRegistry::Visitor` signature. This call to
51/// `collect` will use the supplied `Visitor` as the argument when calling each
52/// of the `Collector` functors registered with `addCollector`.
53///
54/// ## Thread Safety {#ball_attributecollectorregistry-thread-safety}
55///
56///
57/// `ball::AttributeCollectorRegistry` is fully *thread-safe*, meaning that all
58/// non-creator operations on an object can be safely invoked simultaneously
59/// from multiple threads.
60///
61/// ## Usage {#ball_attributecollectorregistry-usage}
62///
63///
64/// This section illustrates intended use of this component.
65///
66/// ### Example 1: Collecting Attributes From Non-uniform Sources {#ball_attributecollectorregistry-example-1-collecting-attributes-from-non-uniform-sources}
67///
68///
69/// In this example we will collect a set of application properties and perform
70/// some manipulation of the collected data. Note that this usage example uses
71/// lambdas and requires C++11. Lambdas can be replaced with named functions
72/// for C++03.
73///
74/// First, we define a few collector functions that will collect the application
75/// properties from various parts of an application and call the specified
76/// visitor functor for every collected attribute:
77/// @code
78/// void userInfo(const bsl::function<void(const ball::Attribute &)>& visitor)
79/// {
80/// int uuid = 12345; // getUuid();
81/// bsl::string userName = "proxy"; // getUserName();
82///
83/// visitor(ball::Attribute("myLib.uuid", uuid));
84/// visitor(ball::Attribute("myLib.user", userName));
85/// }
86///
87/// void threadInfo(
88/// const bsl::function<void(const ball::Attribute &)>& visitor)
89/// {
90/// int threadId = 87654; // getThreadId();
91///
92/// visitor(ball::Attribute("myLib.threadId", threadId));
93/// }
94/// @endcode
95/// Then, we register collector functions with the attribute collector
96/// registry:
97/// @code
98/// ball::AttributeCollectorRegistry registry;
99///
100/// int rc = registry.addCollector(&userInfo, "userInfoCollector");
101/// assert(0 == rc);
102/// assert(true == registry.hasCollector("userInfoCollector"));
103/// rc = registry.addCollector(&threadInfo, "threadInfoCollector");
104/// assert(0 == rc);
105/// assert(true == registry.hasCollector("threadInfoCollector"));
106/// assert(2 == registry.numCollectors());
107/// @endcode
108/// Next, we print every attribute gathered by all registered attribute
109/// collectors in the registry:
110/// @code
111/// bsl::stringstream output1;
112///
113/// registry.collect([&output1](const ball::Attribute& attribute)
114/// {
115/// output1 << attribute.name() << "=" << attribute.value() << " ";
116/// });
117///
118/// assert("myLib.uuid=12345 myLib.user=proxy myLib.threadId=87654 "
119/// == output1.str());
120/// @endcode
121/// Finally, we remove one of the collectors and collect attributes again:
122/// @code
123/// int rc = registry.removeCollector("threadInfoCollector");
124/// assert(0 == rc);
125/// assert(false == registry.hasCollector("threadInfoCollectory"));
126/// assert(1 == registry.numCollectors());
127///
128/// bsl::stringstream output2;
129/// registry.collect([&output2](const ball::Attribute& attribute)
130/// {
131/// output2 << attribute.name() << "=" << attribute.value() << " ";
132/// });
133///
134/// assert("myLib.uuid=12345 myLib.user=proxy " == output2.str());
135/// @endcode
136/// @}
137/** @} */
138/** @} */
139
140/** @addtogroup bal
141 * @{
142 */
143/** @addtogroup ball
144 * @{
145 */
146/** @addtogroup ball_attributecollectorregistry
147 * @{
148 */
149
150#include <balscm_version.h>
151
152#include <ball_attribute.h>
153
154#include <bslma_allocator.h>
155#include <bslma_bslallocator.h>
157
159
161
163
164#include <bsl_functional.h>
165#include <bsl_memory.h>
166#include <bsl_string.h>
167#include <bsl_utility.h>
168#include <bsl_vector.h>
169
170
171namespace ball {
172
173 // ================================
174 // class AttributeCollectorRegistry
175 // ================================
176
177/// This component maintains a registry of named functors ("collectors")
178/// that are used to transform opaque user data into a set of
179/// `ball::Attribute` objects.
180///
181/// See @ref ball_attributecollectorregistry
183
184 public:
185 // TYPES
186
187 /// `Visitor` is the type of a user-supplied visit functor.
188 typedef bsl::function<void(const ball::Attribute&)> Visitor;
189
190 /// `Collector` is the type of a user-supplied attribute collector
191 /// functor.
192 typedef bsl::function<void(const Visitor&)> Collector;
193
194 /// This `typedef` is an alias for the allocator used by this object.
196
197 private:
198 // PRIVATE TYPES
199
200 /// This `typedef` is an alias for a single named attribute collector.
202
203 /// This `typedef` is an alias for the type of the registry maintained
204 /// by this object. Note that `vector` is used to preserve the order in
205 /// which collectors are registered.
207
208 // DATA
209 Registry d_collectors; // collector registry
210
211 mutable bslmt::ReaderWriterMutex d_rwMutex; // protects concurrent
212 // access to 'd_collectors'
213
214 // NOT IMPLEMENTED
217
218 public:
219 // TRAITS
222
223 // CREATORS
224
226 /// Create a registry having no registered collectors. Optionally
227 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
228 /// object) to supply memory; otherwise, the default allocator is used.
229 explicit AttributeCollectorRegistry(const allocator_type& allocator);
230
231#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
232 /// Destroy this registry.
233 ~AttributeCollectorRegistry() = default;
234#endif
235
236 // MANIPULATORS
237
238 /// Add the specified `collector` with the specified `name` to this
239 /// registry. Return 0 if `collector` was successfully registered, and
240 /// a non-zero value (with no effect) otherwise. Note that this method
241 /// will fail if a collector having `name` is already registered.
242 int addCollector(const Collector& collector, const bsl::string_view& name);
243
244 /// Remove all collectors from this registry.
245 void removeAll();
246
247 /// Remove the collector having the specified `name` from this registry.
248 /// Return 0 if the collector with `name` was successfully removed, and
249 /// a non-zero value (with no effect) otherwise.
251
252 // ACCESSORS
253
254 /// Invoke all registered collectors with the specified `visitor`
255 /// functor. Note that collectors are invoked in the order in which
256 /// collectors are registered.
257 void collect(const Visitor& visitor) const;
258
259 /// Return the allocator used by this object to supply memory. Note
260 /// that if no allocator was supplied at construction the default
261 /// allocator in effect at construction is used.
263
264 /// Return `true` if a collector having the specified `name` is in the
265 /// registry maintained by this object, and `false` otherwise. Note
266 /// that this method is provided primarily for debugging purposes (i.e.,
267 /// its return value can be invalidated from another thread).
268 bool hasCollector(const bsl::string_view& name) const;
269
270 /// Return the number of collectors registered with this object. Note
271 /// that this method is provided primarily for debugging purposes (i.e.,
272 /// its return value can be invalidated from another thread).
273 int numCollectors() const;
274};
275
276// ============================================================================
277// INLINE DEFINITIONS
278// ============================================================================
279
280 // --------------------------------
281 // class AttributeCollectorRegistry
282 // --------------------------------
283
284// CREATORS
285inline
290
291inline
293 const allocator_type& allocator)
294: d_collectors(allocator)
295{
296}
297
298// ACCESSORS
299inline
302{
303 return d_collectors.get_allocator();
304}
305
306} // close package namespace
307
308
309#endif
310
311// ----------------------------------------------------------------------------
312// Copyright 2020 Bloomberg Finance L.P.
313//
314// Licensed under the Apache License, Version 2.0 (the "License");
315// you may not use this file except in compliance with the License.
316// You may obtain a copy of the License at
317//
318// http://www.apache.org/licenses/LICENSE-2.0
319//
320// Unless required by applicable law or agreed to in writing, software
321// distributed under the License is distributed on an "AS IS" BASIS,
322// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
323// See the License for the specific language governing permissions and
324// limitations under the License.
325// ----------------------------- END-OF-FILE ----------------------------------
326
327/** @} */
328/** @} */
329/** @} */
Definition ball_attributecollectorregistry.h:182
int addCollector(const Collector &collector, const bsl::string_view &name)
bsl::function< void(const Visitor &)> Collector
Definition ball_attributecollectorregistry.h:192
bsl::function< void(const ball::Attribute &)> Visitor
Visitor is the type of a user-supplied visit functor.
Definition ball_attributecollectorregistry.h:188
BSLMF_NESTED_TRAIT_DECLARATION(AttributeCollectorRegistry, bslma::UsesBslmaAllocator)
AttributeCollectorRegistry()
Definition ball_attributecollectorregistry.h:286
allocator_type get_allocator() const
Definition ball_attributecollectorregistry.h:301
int removeCollector(const bsl::string_view &name)
void collect(const Visitor &visitor) const
bsl::allocator< char > allocator_type
This typedef is an alias for the allocator used by this object.
Definition ball_attributecollectorregistry.h:195
bool hasCollector(const bsl::string_view &name) const
void removeAll()
Remove all collectors from this registry.
Definition ball_attribute.h:198
Definition bslma_bslallocator.h:580
Definition bslstl_stringview.h:441
Forward declaration.
Definition bslstl_function.h:934
Definition bslstl_pair.h:1210
Definition bslstl_vector.h:1025
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:4019
Definition bslmt_readerwritermutex.h:244
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
Definition bslma_usesbslmaallocator.h:343