BDE 4.39.x 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.
201 typedef bsl::pair<bsl::string, Collector> CollectorEntry;
202
203 /// This `typedef` is an alias for the type of the registry maintained by this object.
204 ///
205 /// \note Note that `vector` is used to preserve the order in
206 /// which collectors are registered.
208
209 // DATA
210 Registry d_collectors; // collector registry
211
212 mutable bslmt::ReaderWriterMutex d_rwMutex; // protects concurrent
213 // access to 'd_collectors'
214
215 private:
216 // NOT IMPLEMENTED
219
220 public:
221 // TRAITS
224
225 // CREATORS
226
227 /// Create a registry having no registered collectors. Optionally
228 /// specify an `allocator` (e.g., the address of a `bslma::Allocator`
229 /// object) to supply memory; otherwise, the default allocator is used.
231 explicit AttributeCollectorRegistry(const allocator_type& allocator);
232
233#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
234 /// Destroy this registry.
235 ~AttributeCollectorRegistry() = default;
236#endif
237
238 // MANIPULATORS
239
240 /// Add the specified `collector` with the specified `name` to this
241 /// registry. Return 0 if `collector` was successfully registered, and a non-zero value (with no effect) otherwise.
242 ///
243 /// \note Note that this method
244 /// will fail if a collector having `name` is already registered.
245 int addCollector(const Collector& collector, const bsl::string_view& name);
246
247 /// Remove all collectors from this registry.
248 void removeAll();
249
250 /// Remove the collector having the specified `name` from this registry.
251 /// Return 0 if the collector with `name` was successfully removed, and
252 /// a non-zero value (with no effect) otherwise.
254
255 // ACCESSORS
256
257 /// Invoke all registered collectors with the specified `visitor` functor.
258 ///
259 /// \note Note that collectors are invoked in the order in which
260 /// collectors are registered.
261 void collect(const Visitor& visitor) const;
262
263 /// Return the allocator used by this object to supply memory.
264 ///
265 /// \note Note that if no allocator was supplied at construction the default
266 /// allocator in effect at construction is used.
268
269 /// Return `true` if a collector having the specified `name` is in the
270 /// registry maintained by this object, and `false` otherwise.
271 ///
272 /// \note Note that this method is provided primarily for debugging purposes (i.e.,
273 /// its return value can be invalidated from another thread).
274 bool hasCollector(const bsl::string_view& name) const;
275
276 /// Return the number of collectors registered with this object.
277 ///
278 /// \note Note that this method is provided primarily for debugging purposes (i.e.,
279 /// its return value can be invalidated from another thread).
280 int numCollectors() const;
281};
282
283// ============================================================================
284// INLINE DEFINITIONS
285// ============================================================================
286
287 // --------------------------------
288 // class AttributeCollectorRegistry
289 // --------------------------------
290
291// CREATORS
292inline
297
298inline
300 const allocator_type& allocator)
301: d_collectors(allocator)
302{
303}
304
305// ACCESSORS
306inline
309{
310 return d_collectors.get_allocator();
311}
312
313} // close package namespace
314
315
316#endif
317
318// ----------------------------------------------------------------------------
319// Copyright 2020 Bloomberg Finance L.P.
320//
321// Licensed under the Apache License, Version 2.0 (the "License");
322// you may not use this file except in compliance with the License.
323// You may obtain a copy of the License at
324//
325// http://www.apache.org/licenses/LICENSE-2.0
326//
327// Unless required by applicable law or agreed to in writing, software
328// distributed under the License is distributed on an "AS IS" BASIS,
329// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
330// See the License for the specific language governing permissions and
331// limitations under the License.
332// ----------------------------- END-OF-FILE ----------------------------------
333
334/** @} */
335/** @} */
336/** @} */
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:293
allocator_type get_allocator() const
Definition ball_attributecollectorregistry.h:308
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:199
Definition bslma_bslallocator.h:588
Definition bslstl_stringview.h:471
Forward declaration.
Definition bslstl_function.h:946
Definition bslstl_pair.h:1280
Definition bslstl_vector.h:1120
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:4621
Definition bslmt_readerwritermutex.h:244
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition ball_administration.h:214
Definition bslma_usesbslmaallocator.h:344