BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_timetableloader.h
Go to the documentation of this file.
1/// @file bdlt_timetableloader.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_timetableloader.h -*-C++-*-
8#ifndef INCLUDED_BDLT_TIMETABLELOADER
9#define INCLUDED_BDLT_TIMETABLELOADER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_timetableloader bdlt_timetableloader
15/// @brief Provide a protocol (or pure interface) for loading timetables.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_timetableloader
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_timetableloader-purpose"> Purpose</a>
25/// * <a href="#bdlt_timetableloader-classes"> Classes </a>
26/// * <a href="#bdlt_timetableloader-description"> Description </a>
27/// * <a href="#bdlt_timetableloader-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlt_timetableloader-usage"> Usage </a>
29/// * <a href="#bdlt_timetableloader-example-1-implementing-the-bdlt-timetableloader-protocol"> Example 1: Implementing the bdlt::TimetableLoader Protocol </a>
30///
31/// # Purpose {#bdlt_timetableloader-purpose}
32/// Provide a protocol (or pure interface) for loading timetables.
33///
34/// # Classes {#bdlt_timetableloader-classes}
35///
36/// - bdlt::TimetableLoader: pure interface for loading timetables
37///
38/// @see bdlt_timetablecache, bdlt_timetable
39///
40/// # Description {#bdlt_timetableloader-description}
41/// This component provides a protocol, `bdlt::TimetableLoader`,
42/// for loading timetables from a specific source. Each repository of timetable
43/// information can be supported by a distinct implementation of the
44/// `TimetableLoader` protocol. The protocol's primary method, `load`, loads a
45/// timetable into a `bdlt::Timetable` object. The timetable to load is
46/// identified by name, which is specified by a null-terminated C-style string
47/// (i.e., `const char *`).
48///
49/// ## Thread Safety {#bdlt_timetableloader-thread-safety}
50///
51///
52/// Unless otherwise documented, a single timetable loader object is not safe
53/// for concurrent access by multiple threads. Classes derived from
54/// `bdlt::TimetableLoader` that are specifically designed for concurrent access
55/// must be documented as such. Unless specifically documented otherwise,
56/// separate objects of classes derived from `bdlt::TimetableLoader` may safely
57/// be used in separate threads.
58///
59/// ## Usage {#bdlt_timetableloader-usage}
60///
61///
62/// This section illustrates intended use of this component.
63///
64/// ### Example 1: Implementing the bdlt::TimetableLoader Protocol {#bdlt_timetableloader-example-1-implementing-the-bdlt-timetableloader-protocol}
65///
66///
67/// This example demonstrates an elided concrete implementation of the
68/// `bdlt::TimetableLoader` protocol that interprets timetable information
69/// contained in ASCII strings that are formatted using JSON. Note that, in
70/// general, an implementation of `bdlt::TimetableLoader` must obtain timetable
71/// information from *some* data source. Our elided implementation leaves it
72/// unspecified as to where the JSON strings are obtained (i.e., whether from a
73/// file system, a database, a local or remote service, etc.).
74///
75/// First, we show the JSON format that our timetable loader accepts:
76/// @code
77/// {
78/// "firstDate": "YYYY-MM-DD",
79/// "lastDate": "YYYY-MM-DD",
80/// "initialTransitionCode": code,
81/// "transitions":
82/// [
83/// {
84/// "datetime": "YYYY-MM-DDThh:mm:ss",
85/// "code": code
86/// }
87/// ]
88/// }
89/// @endcode
90/// Note that "YYYY-MM-DD" is an ISO 8601 representation for the value of a
91/// `bdlt::Date` object, "YYYY-MM-DDThh:mm:ss" is an ISO 8601 representation for
92/// the value of a `bdlt::Datetime` object, and `code` is an integer greater
93/// than or equal to -1 (with -1 used to specify
94/// `bdlt::Timetable::k_UNSET_TRANSITION_CODE`). We assume that the four JSON
95/// attributes, "firstDate", "lastDate", "initialTransitionCode", and
96/// "transitions", must occur in the JSON string in the order in which they
97/// appear in the above display, but only "firstDate" and "lastDate" are
98/// *required* attributes.
99///
100/// Then, we define the interface of our implementation:
101/// @code
102/// /// This class provides a concrete implementation of the
103/// /// `bdlt::TimetableLoader` protocol (an abstract interface) for loading
104/// /// a timetable. This elided implementation obtains timetable
105/// /// information from ASCII strings formatted using JSON. The source of
106/// /// the strings is unspecified.
107/// class MyTimetableLoader : public bdlt::TimetableLoader {
108///
109/// public:
110/// // CREATORS
111///
112/// /// Create a `MyTimetableLoader` object.
113/// MyTimetableLoader();
114///
115/// /// Destroy this object.
116/// virtual ~MyTimetableLoader();
117///
118/// // MANIPULATORS
119///
120/// /// Load, into the specified `result`, the timetable identified by
121/// /// the specified `timetableName`. Return 0 on success, and a
122/// /// non-zero value otherwise. If the timetable corresponding to
123/// /// `timetableName` is not found, 1 is returned with no effect on
124/// /// `*result`. If a non-zero value other than 1 is returned
125/// /// (indicating a different error), `*result` is valid, but its
126/// /// value is undefined.
127/// virtual int load(bdlt::Timetable *result, const char *timetableName);
128/// };
129/// @endcode
130/// Next, we implement the creators, trivially, as `MyTimetableLoader` does not
131/// contain any instance data members:
132/// @code
133/// // CREATORS
134/// inline
135/// MyTimetableLoader::MyTimetableLoader()
136/// {
137/// }
138///
139/// inline
140/// MyTimetableLoader::~MyTimetableLoader()
141/// {
142/// }
143/// @endcode
144/// Then, we implement the `load` function:
145/// @code
146/// // MANIPULATORS
147/// int MyTimetableLoader::load(bdlt::Timetable *result,
148/// const char *timetableName)
149/// {
150/// @endcode
151/// Next, we look up the timetable identified by `timetableName` and load the
152/// corresponding text into a `bsl::string` object, `json` (as stated earlier,
153/// we do not specify in this example from where the timetable information is
154/// obtained):
155/// @code
156/// // Obtain the information for the timetable identified by
157/// // 'timetableName' from an unspecified data source and load it into the
158/// // 'json' string.
159///
160/// bsl::string json;
161///
162/// // Since a JSON parser is not available to 'bdlt', this example assumes
163/// // that 'json' is populated with the following specific data:
164/// //..
165/// // {
166/// // "firstDate": "2018-01-01",
167/// // "lastDate": "2018-12-31",
168/// // "initialTransitionCode": 1,
169/// // "transitions":
170/// // [
171/// // {
172/// // "datetime": "2018-05-28T09:00:00",
173/// // "code": 2
174/// // },
175/// // {
176/// // "datetime": "2018-05-28T18:00:00",
177/// // "code": 3
178/// // }
179/// // ]
180/// // }
181/// //..
182/// // Similarly, we hard-wire the value of a status flag, 'rc', to
183/// // indicate that this string was successfully retrieved from the data
184/// // source.
185///
186/// int rc = 0; // success obtaining timetable information
187///
188/// if (rc != 0) {
189/// return 1; // RETURN
190/// }
191/// @endcode
192/// Note that the non-zero value 1 is returned only in the case where the
193/// timetable information corresponding to `timetableName` cannot be found (per
194/// the contract for the `load` method).
195///
196/// Then, we parse the "firstDate" and "lastDate" attributes from the `json`
197/// string, loading the results into like-named variables:
198/// @code
199/// // Parse the "firstDate" and "lastDate" JSON attributes and load the
200/// // results into 'firstDate' and 'lastDate', respectively. It is an
201/// // error if either of the "firstDate" or "lastDate" attributes are
202/// // missing, or if they are out of order.
203///
204/// bdlt::Date firstDate;
205/// bdlt::Date lastDate;
206///
207/// // For the purposes of this Usage, we hard-wire the first and last
208/// // dates that are hypothetically parsed from the 'json' string, and
209/// // set the 'rc' status flag indicating that parsing succeeded.
210///
211/// firstDate.setYearMonthDay(2018, 1, 1);
212/// lastDate.setYearMonthDay( 2018, 12, 31);
213/// rc = 0; // success parsing "firstDate" and "lastDate" attributes
214///
215/// if (rc != 0 || firstDate > lastDate) {
216/// return 2; // RETURN
217/// }
218///
219/// result->reset();
220///
221/// result->setValidRange(firstDate, lastDate);
222/// @endcode
223/// Next, we parse the "initialTransitionCode" attribute from `json`:
224/// @code
225/// // For the purposes of this Usage, we hard-wire a boolean flag
226/// // indicating that the "initialTransitionCode" attribute was
227/// // hypothetically detected in the 'json' string.
228///
229/// bool isInitialTransitionCodePresent = true;
230///
231/// if (isInitialTransitionCodePresent) {
232///
233/// // For the purposes of this Usage, we hard-wire the initial
234/// // transition code that is hypothetically parsed from the 'json'
235/// // string, and set the 'rc' status flag indicating that parsing
236/// // succeeded.
237///
238/// int code = 1;
239///
240/// rc = 0; // success parsing "initialTransitionCode" attribute
241///
242/// if (rc != 0) {
243/// return 3; // RETURN
244/// }
245///
246/// result->setInitialTransitionCode(code);
247/// }
248/// @endcode
249/// Now, we parse the "transitions" attribute from `json` and load the result
250/// into a `bsl::vector<bsl::pair<bdlt::Datetime, int> >` object, `transitions`:
251/// @code
252/// // For the purposes of this Usage, we hard-wire a boolean flag
253/// // indicating that the "transitions" attribute was hypothetically
254/// // detected in the 'json' string.
255///
256/// bool isTransitionsPresent = true;
257///
258/// if (isTransitionsPresent) {
259///
260/// // Parse the "transitions" JSON attribute and load 'transitions'
261/// // with the result.
262///
263/// bsl::vector<bsl::pair<bdlt::Datetime, int> > transitions;
264///
265/// // For the purposes of this Usage, we hard-wire the transitions
266/// // that are hypothetically parsed from the 'json' string, and set
267/// // the 'rc' status flag indicating that parsing succeeded.
268///
269/// transitions.push_back(bsl::pair<bdlt::Datetime, int>(
270/// bdlt::Datetime(2018, 5, 28, 9), 2));
271/// transitions.push_back(bsl::pair<bdlt::Datetime, int>(
272/// bdlt::Datetime(2018, 5, 28, 18), 3));
273///
274/// rc = 0; // success parsing "transitions" attribute
275///
276/// if (rc != 0) {
277/// return 4; // RETURN
278/// }
279///
280/// bsl::vector<bsl::pair<bdlt::Datetime, int> >::const_iterator it =
281/// transitions.begin();
282///
283/// while (it != transitions.end()) {
284/// const bdlt::Date& date = it->first.date();
285///
286/// if (date < firstDate || date > lastDate || -1 > it->second) {
287/// return 5; // RETURN
288/// }
289///
290/// result->addTransition(
291/// it->first,
292/// it->second >= 0
293/// ? it->second
294/// : bdlt::Timetable::k_UNSET_TRANSITION_CODE);
295///
296/// ++it;
297/// }
298/// }
299/// @endcode
300/// Our timetable loader imposes the requirement that the dates specified in the
301/// "transitions" JSON attribute must be within the range
302/// `[firstDate .. lastDate]` and the transition codes are non-negative or
303/// unset.
304///
305/// Finally, we return 0 indicating success:
306/// @code
307/// return 0;
308/// }
309/// @endcode
310/// @}
311/** @} */
312/** @} */
313
314/** @addtogroup bdl
315 * @{
316 */
317/** @addtogroup bdlt
318 * @{
319 */
320/** @addtogroup bdlt_timetableloader
321 * @{
322 */
323
324#include <bdlscm_version.h>
325
326
327namespace bdlt {
328
329class Timetable;
330
331 // =====================
332 // class TimetableLoader
333 // =====================
334
335/// This class defines a protocol used to load timetables from a specific
336/// source. Each repository of timetable information can be supported by a
337/// distinct implementation of this protocol.
338///
339/// See @ref bdlt_timetableloader
341
342 public:
343 // CREATORS
344
345 /// Destroy this object.
347
348 // MANIPULATORS
349
350 /// Load, into the specified `result`, the timetable identified by the
351 /// specified `timetableName`. Return 0 on success, and a non-zero
352 /// value otherwise. If the timetable corresponding to `timetableName`
353 /// is not found, 1 is returned with no effect on `*result`. If a
354 /// non-zero value other than 1 is returned (indicating a different
355 /// error), `*result` is valid, but its value is undefined.
356 virtual int load(Timetable *result, const char *timetableName) = 0;
357};
358
359} // close package namespace
360
361
362#endif
363
364// ----------------------------------------------------------------------------
365// Copyright 2018 Bloomberg Finance L.P.
366//
367// Licensed under the Apache License, Version 2.0 (the "License");
368// you may not use this file except in compliance with the License.
369// You may obtain a copy of the License at
370//
371// http://www.apache.org/licenses/LICENSE-2.0
372//
373// Unless required by applicable law or agreed to in writing, software
374// distributed under the License is distributed on an "AS IS" BASIS,
375// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
376// See the License for the specific language governing permissions and
377// limitations under the License.
378// ----------------------------- END-OF-FILE ----------------------------------
379
380/** @} */
381/** @} */
382/** @} */
Definition bdlt_timetableloader.h:340
virtual ~TimetableLoader()
Destroy this object.
virtual int load(Timetable *result, const char *timetableName)=0
Definition bdlt_timetable.h:667
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112