BDE 4.14.0 Production release
|
Provide a protocol (or pure interface) for loading calendars.
This component provides a protocol, bdlt::CalendarLoader
, for loading calendars from a specific source. Each repository of calendar information can be supported by a distinct implementation of the CalendarLoader
protocol. The protocol's primary method, load
, loads a calendar into a bdlt::PackedCalendar
object. The calendar to load is identified by name, which is specified by a null-terminated C-style string (i.e., const char *
).
Unless otherwise documented, a single calendar loader object is not safe for concurrent access by multiple threads. Classes derived from bdlt::CalendarLoader
that are specifically designed for concurrent access must be documented as such. Unless specifically documented otherwise, separate objects of classes derived from bdlt::CalendarLoader
may safely be used in separate threads.
This section illustrates intended use of this component.
This example demonstrates an elided concrete implementation of the bdlt::CalendarLoader
protocol that interprets calendar information contained in ASCII strings that are formatted using JSON. Note that, in general, an implementation of bdlt::CalendarLoader
must obtain calendar information from some data source. Our elided implementation leaves it unspecified as to where the JSON strings are obtained (i.e., whether from a file system, a database, a local or remote service, etc.).
First, we show the JSON format that our calendar loader accepts. For simplicity, we omit support for holiday codes and weekend-days transitions:
Note that "YYYY-MM-DD" is an ISO 8601 representation for the value of a bdlt::Date
object and wd
is an integer in the range [1 .. 7]
. The range used for specifying weekend days corresponds directly to the bdlt::DayOfWeek::Enum
enumeration, [e_SUN = 1 .. e_SAT]
(see bdlt_dayofweek ). We assume that the four JSON attributes, "firstDate", "lastDate", "weekendDays", and "holidays", must occur in the JSON string in the order in which they appear in the above display, but only "firstDate" and "lastDate" are required attributes.
Then, we define the interface of our implementation:
Next, we implement the creators, trivially, as MyCalendarLoader
does not contain any instance data members:
Then, we implement the load
function:
Next, we look up the calendar identified by calendarName
and load the corresponding text into a bsl::string
object, json
(as stated earlier, we do not specify in this example from where the calendar information is obtained):
Note that the non-zero value 1 is returned only in the case where the calendar information corresponding to calendarName
cannot be found (per the contract for the load
method).
Then, we parse the "firstDate" and "lastDate" attributes from the json
string, loading the results into like-named variables:
Next, we parse the "weekendDays" attribute from json
and load the result into a bdlt::DayOfWeekSet
object, dayOfWeekSet
:
Now, we parse the "holidays" attribute from json
and load the result into a bsl::vector<bdlt::Date>
object, holidays
:
Note that the addHoliday
method can extend the range of the calendar. Our calendar loader instead imposes the requirement that the dates specified in the "holidays" JSON attribute must be within the range [firstDate .. lastDate]
.
Finally, we return 0 indicating success: