BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_observeradapter.h
Go to the documentation of this file.
1/// @file ball_observeradapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_observeradapter.h -*-C++-*-
8#ifndef INCLUDED_BALL_OBSERVERADAPTER
9#define INCLUDED_BALL_OBSERVERADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_observeradapter ball_observeradapter
15/// @brief Provide a helper for implementing the `ball::Observer` protocol.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_observeradapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_observeradapter-purpose"> Purpose</a>
25/// * <a href="#ball_observeradapter-classes"> Classes </a>
26/// * <a href="#ball_observeradapter-description"> Description </a>
27/// * <a href="#ball_observeradapter-usage"> Usage </a>
28/// * <a href="#ball_observeradapter-example-1-concrete-observer-derived-from-ball-observeradapter"> Example 1: Concrete Observer Derived From ball::ObserverAdapter </a>
29///
30/// # Purpose {#ball_observeradapter-purpose}
31/// Provide a helper for implementing the `ball::Observer` protocol.
32///
33/// # Classes {#ball_observeradapter-classes}
34///
35/// - ball::ObserverAdapter: a helper for implementing `ball::Observer`
36///
37/// @see ball_observer, ball_record, ball_context
38///
39/// # Description {#ball_observeradapter-description}
40/// This component provides a single class `ball::ObserverAdapter`
41/// that aids in the implementation of the `ball::Observer` protocol by allowing
42/// clients to implement that protocol by implementing a single method
43/// signature: `publish(const ball::Record&, const ball::Context&)`. A primary
44/// goal of this component is to simplify the transition for older
45/// implementations of the `ball::Observer` protocol (that accept
46/// const-references to `ball::Record` objects) to the updated protocol (that
47/// accepts shared-pointers to `ball::Record` objects). `ball::ObserverAdapter`
48/// inherits from `ball::Observer`, and implements the (newer) overload of the
49/// `publish` method in (accepting a shared-pointer to a record) by calling the
50/// overload of the `publish` method (accepting a reference to a record).
51///
52/// ## Usage {#ball_observeradapter-usage}
53///
54///
55/// This section illustrates intended use of this component.
56///
57/// ### Example 1: Concrete Observer Derived From ball::ObserverAdapter {#ball_observeradapter-example-1-concrete-observer-derived-from-ball-observeradapter}
58///
59///
60/// The following code fragments illustrate the essentials of defining and using
61/// a concrete observer inherited from `ball::ObserverAdapter`.
62///
63/// First define a concrete observer `MyOstreamObserver` derived from
64/// `ball::ObserverAdapter` that declares a single publish method accepting a
65/// const-reference to a `ball::Record` object:
66/// @code
67/// class MyOstreamObserver : public ball::ObserverAdapter {
68/// bsl::ostream *d_stream;
69///
70/// public:
71/// explicit MyOstreamObserver(bsl::ostream *stream) : d_stream(stream) { }
72/// virtual ~MyOstreamObserver();
73/// using Observer::publish; // avoid hiding base class method
74/// virtual void publish(const ball::Record& record,
75/// const ball::Context& context);
76/// };
77/// @endcode
78/// Then, we implement the public methods of `MyOstreamObserver`, including the
79/// `publish` method. This implementation of `publish` simply prints out the
80/// content of the record it receives to the stream supplied at construction.
81/// @code
82/// MyOstreamObserver::~MyOstreamObserver()
83/// {
84/// }
85///
86/// void MyOstreamObserver::publish(const ball::Record& record,
87/// const ball::Context&)
88/// {
89/// const ball::RecordAttributes& fixedFields = record.fixedFields();
90///
91/// *d_stream << fixedFields.timestamp() << ' '
92/// << fixedFields.processID() << ' '
93/// << fixedFields.threadID() << ' '
94/// << fixedFields.fileName() << ' '
95/// << fixedFields.lineNumber() << ' '
96/// << fixedFields.category() << ' '
97/// << fixedFields.message() << ' ';
98///
99/// const ball::UserFields& customFields = record.customFields();
100/// const int numCustomFields = customFields.length();
101/// for (int i = 0; i < numCustomFields; ++i) {
102/// *d_stream << customFields[i] << ' ';
103/// }
104///
105/// *d_stream << '\n' << bsl::flush;
106/// }
107/// @endcode
108/// Now, we defined a function `main` in which we create a `MyOstreamObserver`
109/// object and assign the address of this object to a `ball::ObserverAdapter`
110/// pointer:
111/// @code
112/// int main(bool verbose)
113/// {
114/// bsl::ostringstream out;
115/// MyOstreamObserver myObserver(&out);
116/// ball::ObserverAdapter *adapter = &myObserver;
117/// @endcode
118/// Finally, publish three messages by calling `publish` method accepting a
119/// shared-pointer, provided by `ball::ObserverAdapter`, that in turn will call
120/// the `publish` method defined in `MyOstreamObserver`:
121/// @code
122/// bdlt::Datetime now;
123/// ball::RecordAttributes fixedFields;
124/// ball::UserFields customFields;
125///
126/// const int NUM_MESSAGES = 3;
127/// for (int n = 0; n < NUM_MESSAGES; ++n) {
128/// fixedFields.setTimestamp(bdlt::CurrentTime::utc());
129///
130/// bsl::shared_ptr<const ball::Record> handle;
131/// handle.createInplace(bslma::Default::allocator(),
132/// fixedFields,
133/// customFields);
134/// adapter->publish(handle,
135/// ball::Context(ball::Transmission::e_TRIGGER,
136/// n,
137/// NUM_MESSAGES));
138/// }
139/// if (verbose) {
140/// bsl::cout << out.str() << bsl::endl;
141/// }
142/// return 0;
143/// }
144/// @endcode
145/// The above code fragments print to `stdout` like this:
146/// @code
147/// Publish a sequence of three messages.
148/// 22FEB2012_00:12:12.000 201 31 0
149/// 22FEB2012_00:12:12.000 202 32 0
150/// 22FEB2012_00:12:12.000 203 33 0
151/// @endcode
152/// @}
153/** @} */
154/** @} */
155
156/** @addtogroup bal
157 * @{
158 */
159/** @addtogroup ball
160 * @{
161 */
162/** @addtogroup ball_observeradapter
163 * @{
164 */
165
166#include <balscm_version.h>
167
168#include <ball_observer.h>
169
170#include <bsls_keyword.h>
171
172#include <bsl_memory.h>
173
174
175namespace ball {
176
177class Record;
178class Context;
179
180 // =====================
181 // class ObserverAdapter
182 // =====================
183
184/// This class aids in the implementation of the `Observer` protocol by
185/// allowing clients to implement that protocol by implementing a single
186/// method signature: `publish(const Record&, const Context&)`.
187///
188/// See @ref ball_observeradapter
189class ObserverAdapter : public Observer {
190
191 public:
192 // CREATORS
193
194 /// Destroy this observer.
196
197 // MANIPULATORS
198
199 /// Process the record referred by the specified log shared pointer
200 /// `record`. Note that classes that derive from `ObserverAdapter`
201 /// should **not** implement this method.
202 void publish(const Record& record,
203 const Context& context) BSLS_KEYWORD_OVERRIDE = 0;
204
205 /// Process the specified log `record` having the specified publishing
206 /// `context`.
207 void publish(const bsl::shared_ptr<const Record>& record,
208 const Context& context)
210
211 /// Discard any shared reference to a `Record` object that was supplied
212 /// to the `publish` method and is held by this observer. Note that
213 /// classes that derive from `ObserverAdapter` should *not* implement
214 /// this method. Also note that this operation should be called if
215 /// resources underlying the previously provided shared-pointers must be
216 /// released.
218};
219
220// ============================================================================
221// INLINE DEFINITIONS
222// ============================================================================
223
224 // ---------------------
225 // class ObserverAdapter
226 // ---------------------
227
228// MANIPULATORS
229inline
230void ObserverAdapter::publish(const bsl::shared_ptr<const Record>& record,
231 const Context& context)
232{
233 publish(*record, context);
234}
235
236inline
240
241} // close package namespace
242
243
244#endif
245
246// ----------------------------------------------------------------------------
247// Copyright 2015 Bloomberg Finance L.P.
248//
249// Licensed under the Apache License, Version 2.0 (the "License");
250// you may not use this file except in compliance with the License.
251// You may obtain a copy of the License at
252//
253// http://www.apache.org/licenses/LICENSE-2.0
254//
255// Unless required by applicable law or agreed to in writing, software
256// distributed under the License is distributed on an "AS IS" BASIS,
257// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
258// See the License for the specific language governing permissions and
259// limitations under the License.
260// ----------------------------- END-OF-FILE ----------------------------------
261
262/** @} */
263/** @} */
264/** @} */
Definition ball_context.h:295
Definition ball_observeradapter.h:189
void releaseRecords() BSLS_KEYWORD_OVERRIDE
Definition ball_observeradapter.h:237
void publish(const Record &record, const Context &context) BSLS_KEYWORD_OVERRIDE=0
~ObserverAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this observer.
Definition ball_observer.h:235
Definition ball_record.h:178
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition ball_administration.h:214
Definition bdlb_printmethods.h:283