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

Detailed Description

Outline

Purpose

Provide a mechanism for mapping control messages to callbacks.

Classes

Description

The balb::ControlManager mechanism provided by this component maps control messages to callback functions on the basis of message prefixes.

Callback Function Requirements

Functions registered as callbacks for messages must be invokable as void(*)(const bsl::string&, bsl::istream&). (This signature is balb::ControlManager::ControlHandler). When the function is invoked, the first argument is the message prefix, and the second is a stream on the remainder of the message.

Thread Safety

This component is thread-safe and thread-enabled: it is safe to access and manipulate multiple distinct instances from different threads, and it is safe to access and manipulate a single shared instance from different threads.

Usage

This section illustrates intended use of this component.

Example 1: Creating an ECHO Message Handler

First define a trivial callback to be invoked when an "ECHO" message is received:

void onEcho(const bsl::string& prefix, bsl::istream& stream)
{
bsl::cout << "onEcho: \"" << prefix;
while (stream.good()) {
stream >> word;
bsl::cout << ' ' << word;
}
bsl::cout << '\"' << bsl::endl;
}
Definition bslstl_string.h:1281

Now create a balb::ControlManager object and register a handler for "ECHO". Also register a handler for HELP to observe the auto-generated documentation for ECHO:

manager.registerHandler("ECHO", "<text>",
"Print specified text to the standard output",
&onEcho);
manager.registerHandler("HELP", "",
"Print documentation",
&manager, &bsl::cout, bsl::string(
"The following commands are accepted by the test driver:")));
manager.dispatchMessage("ECHO repeat this text");
manager.dispatchMessage("echo matching is case-insensitive");
manager.dispatchMessage("HELP");
Definition balb_controlmanager.h:142
void printUsageHelper(bsl::ostream *stream, const bsl::string_view &preamble) const
Definition balb_controlmanager.h:400
int dispatchMessage(const bsl::string_view &message) const
int registerHandler(const bsl::string_view &prefix, const bsl::string_view &arguments, const bsl::string_view &description, const ControlHandler &handler)
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1830