|
BDE 4.14.0 Production release
|
Provide a protocol (or pure interface) for loading timetables.
This component provides a protocol, bdlt::TimetableLoader, for loading timetables from a specific source. Each repository of timetable information can be supported by a distinct implementation of the TimetableLoader protocol. The protocol's primary method, load, loads a timetable into a bdlt::Timetable object. The timetable 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 timetable loader object is not safe for concurrent access by multiple threads. Classes derived from bdlt::TimetableLoader that are specifically designed for concurrent access must be documented as such. Unless specifically documented otherwise, separate objects of classes derived from bdlt::TimetableLoader 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::TimetableLoader protocol that interprets timetable information contained in ASCII strings that are formatted using JSON. Note that, in general, an implementation of bdlt::TimetableLoader must obtain timetable 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 timetable loader accepts:
Note that "YYYY-MM-DD" is an ISO 8601 representation for the value of a bdlt::Date object, "YYYY-MM-DDThh:mm:ss" is an ISO 8601 representation for the value of a bdlt::Datetime object, and code is an integer greater than or equal to -1 (with -1 used to specify bdlt::Timetable::k_UNSET_TRANSITION_CODE). We assume that the four JSON attributes, "firstDate", "lastDate", "initialTransitionCode", and "transitions", 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 MyTimetableLoader does not contain any instance data members:
Then, we implement the load function:
Next, we look up the timetable identified by timetableName and load the corresponding text into a bsl::string object, json (as stated earlier, we do not specify in this example from where the timetable information is obtained):
Note that the non-zero value 1 is returned only in the case where the timetable information corresponding to timetableName 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 "initialTransitionCode" attribute from json:
Now, we parse the "transitions" attribute from json and load the result into a bsl::vector<bsl::pair<bdlt::Datetime, int> > object, transitions:
Our timetable loader imposes the requirement that the dates specified in the "transitions" JSON attribute must be within the range [firstDate .. lastDate] and the transition codes are non-negative or unset.
Finally, we return 0 indicating success: