Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component ball_attributecollectorregistry
[Package ball]

Provide a registry for attribute collector functors. More...

Namespaces

namespace  ball

Detailed Description

Outline
Purpose:
Provide a registry for attribute collector functors.
Classes:
ball::AttributeCollectorRegistry registry of attribute collector functors
See also:
Component ball_record, Component 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));
  }
Then, we register collector functions with the attribute collector registry:
  ball::AttributeCollectorRegistry 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());
Next, we print every attribute gathered by all registered attribute collectors in the registry:
  bsl::stringstream output1;

  registry.collect([&output1](const ball::Attribute& attribute)
      {
          output1 << attribute.name() << "=" << attribute.value() << " ";
      });

  assert("myLib.uuid=12345 myLib.user=proxy myLib.threadId=87654 "
         == output1.str());
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());

  bsl::stringstream output2;
  registry.collect([&output2](const ball::Attribute& attribute)
      {
          output2 << attribute.name() << "=" << attribute.value() << " ";
      });

  assert("myLib.uuid=12345 myLib.user=proxy " == output2.str());