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

Detailed Description

Outline

Purpose

Provide a broadcast observer that forwards to other observers.

Classes

See also
ball_record, ball_context, ball_observer, ball_loggermanager

Description

This component provides a concrete implementation of the ball::Observer protocol for receiving and processing log records:

,-----------------------.
`-----------------------'
| ctor
| registerObserver
| deregisterObserver
| deregisterAllObservers
| findObserver
| numRegisteredObservers
| visitObservers
V
,--------------.
( ball::Observer )
`--------------'
dtor
publish
releaseRecords
Definition ball_broadcastobserver.h:226

ball::BroadcastObserver is a concrete class derived from ball::Observer that processes the log records it receives through its publish method by forwarding them to other concrete observers. ball::BroadcastObserver maintains a registry of named observers to which it forwards log records. Clients of ball::BroadcastObserver register observers using the registerObserver method and unregister observers with the deregisterObserver method. Once registered, an observer receives all log records that its associated broadcast observer receives.

Thread Safety

ball::BroadcastObserver is thread-safe, meaning that multiple threads may share the same instance, or may have their own instances (see bsldoc_glossary ).

Usage

In this section we show intended use of this component.

Example 1: Delayed Observer Configuration

In this example, we will show how ball::BroadcastObserver can be used to implement delayed observer configuration.

First, we define an elided custom observer that conforms to the ball::Observer protocol and supports a configure method:

class ConfigurableObserver : public ball::Observer {
// DATA
bool d_configureFlag; // configuration completion flag
public:
// CREATORS
ConfigurableObserver() : d_configureFlag(false)
{
}
// MANIPULATORS
// Configure this observer.
void configure() {
d_configureFlag = true;
}
using Observer::publish; // avoid hiding base class method
/// Publish the specified `record` with the specified `context`.
const ball::Context& context)
{
// Do not publish any records until configuration has been done.
if (!d_configureFlag) {
return; // RETURN
}
// Publish the record.
// ...
}
// ACCESSORS
bool isConfigured() const
{
return d_configureFlag;
}
};
Definition ball_context.h:295
Definition ball_observer.h:235
virtual void publish(const Record &record, const Context &context)
Definition bslstl_sharedptr.h:1830

Then, we create a shared pointer to a ConfigurableObserver object and register it with a broadcast observer:

myObserverPtr(new ConfigurableObserver());
ball::BroadcastObserver broadcastObserver;
int rc = broadcastObserver.registerObserver(myObserver, "observer");
assert(0 == rc);
int registerObserver(const bsl::shared_ptr< Observer > &observer, const bsl::string_view &observerName)

Finally, we can retrieve the registered observer and configure it:

rc = broadcastObserver.findObserver(&tmpObserverPtr, "observer");
assert(0 == rc);
assert(myObserverPtr == tmpObserverPtr);
assert(false == tmpObserverPtr->isConfigured());
tmpObserverPtr->configure();
assert(true == tmpObserverPtr->isConfigured());
bsl::shared_ptr< Observer > findObserver(const bsl::string_view &observerName)

Note that there is an alternative way to obtain a shared pointer to the registered observer:

broadcastObserver.findObserver("observer");
assert(oPtr.ptr());
bslstl::SharedPtrUtil::dynamicCast(&anotherObserverPtr, oPtr);
assert(myObserverPtr == anotherObserverPtr);
element_type * ptr() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_sharedptr.h:5688
static void dynamicCast(bsl::shared_ptr< TARGET > *target, const bsl::shared_ptr< SOURCE > &source)
Definition bslstl_sharedptr.h:6111