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