BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_attributecollectorregistry

Detailed Description

Outline

Purpose

Provide a registry for attribute collector functors.

Classes

See also
ball_record, ball_loggermanager

Description

This component provides a mechanism, ball::AttributeCollectorRegistry, that allows clients to register ball::Attribute collection functions, and to separately apply a visitor to all the attributes collected using those collection functions. A client can register a ball::Attribute collection function by calling addCollector and supplying a function matching the AttributeCollectorRegistry::Collector signature. Clients can also apply a visitor to collected ball::Attribute objects by calling collect and supplying a function matching the ball::AttributeCollectorRegistry::Visitor signature. This call to collect will use the supplied Visitor as the argument when calling each of the Collector functors registered with addCollector.

Thread Safety

ball::AttributeCollectorRegistry is fully thread-safe, meaning that all non-creator operations on an object can be safely invoked simultaneously from multiple threads.

Usage

This section illustrates intended use of this component.

Example 1: Collecting Attributes From Non-uniform Sources

In this example we will collect a set of application properties and perform some manipulation of the collected data. Note that this usage example uses lambdas and requires C++11. Lambdas can be replaced with named functions for C++03.

First, we define a few collector functions that will collect the application properties from various parts of an application and call the specified visitor functor for every collected attribute:

void userInfo(const bsl::function<void(const ball::Attribute &)>& visitor)
{
int uuid = 12345; // getUuid();
bsl::string userName = "proxy"; // getUserName();
visitor(ball::Attribute("myLib.uuid", uuid));
visitor(ball::Attribute("myLib.user", userName));
}
void threadInfo(
const bsl::function<void(const ball::Attribute &)>& visitor)
{
int threadId = 87654; // getThreadId();
visitor(ball::Attribute("myLib.threadId", threadId));
}
Definition ball_attribute.h:198
Definition bslstl_string.h:1281
Forward declaration.
Definition bslstl_function.h:934

Then, we register collector functions with the attribute collector registry:

int rc = registry.addCollector(&userInfo, "userInfoCollector");
assert(0 == rc);
assert(true == registry.hasCollector("userInfoCollector"));
rc = registry.addCollector(&threadInfo, "threadInfoCollector");
assert(0 == rc);
assert(true == registry.hasCollector("threadInfoCollector"));
assert(2 == registry.numCollectors());
Definition ball_attributecollectorregistry.h:182
int addCollector(const Collector &collector, const bsl::string_view &name)
bool hasCollector(const bsl::string_view &name) const

Next, we print every attribute gathered by all registered attribute collectors in the registry:

registry.collect([&output1](const ball::Attribute& attribute)
{
output1 << attribute.name() << "=" << attribute.value() << " ";
});
assert("myLib.uuid=12345 myLib.user=proxy myLib.threadId=87654 "
== output1.str());
void collect(const Visitor &visitor) const
const char * name() const
Return the name of this object.
Definition ball_attribute.h:643
const Value & value() const
Definition ball_attribute.h:649
Definition bslstl_stringstream.h:184
void str(const StringType &value)
Definition bslstl_stringstream.h:545

Finally, we remove one of the collectors and collect attributes again:

int rc = registry.removeCollector("threadInfoCollector");
assert(0 == rc);
assert(false == registry.hasCollector("threadInfoCollectory"));
assert(1 == registry.numCollectors());
registry.collect([&output2](const ball::Attribute& attribute)
{
output2 << attribute.name() << "=" << attribute.value() << " ";
});
assert("myLib.uuid=12345 myLib.user=proxy " == output2.str());
int removeCollector(const bsl::string_view &name)