BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_defaulttimetablecache.h
Go to the documentation of this file.
1/// @file bdlt_defaulttimetablecache.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_defaulttimetablecache.h -*-C++-*-
8#ifndef INCLUDED_BDLT_DEFAULTTIMETABLECACHE
9#define INCLUDED_BDLT_DEFAULTTIMETABLECACHE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_defaulttimetablecache bdlt_defaulttimetablecache
15/// @brief Provide a process-wide default `bdlt::TimetableCache` object.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_defaulttimetablecache
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_defaulttimetablecache-purpose"> Purpose</a>
25/// * <a href="#bdlt_defaulttimetablecache-classes"> Classes </a>
26/// * <a href="#bdlt_defaulttimetablecache-description"> Description </a>
27/// * <a href="#bdlt_defaulttimetablecache-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlt_defaulttimetablecache-usage"> Usage </a>
29/// * <a href="#bdlt_defaulttimetablecache-example-1-using-bdlt-defaulttimetablecache"> Example 1: Using bdlt::DefaultTimetableCache </a>
30///
31/// # Purpose {#bdlt_defaulttimetablecache-purpose}
32/// Provide a process-wide default `bdlt::TimetableCache` object.
33///
34/// # Classes {#bdlt_defaulttimetablecache-classes}
35///
36/// - bdlt::DefaultTimetableCache: namespace managing a default timetable cache
37///
38/// @see bdlt_timetablecache, bdlt_timetableloader
39///
40/// # Description {#bdlt_defaulttimetablecache-description}
41/// This component provides a namespace,
42/// `bdlt::DefaultTimetableCache`, for utility functions that initialize,
43/// provide access to, and ultimately destroy, a default `bdlt::TimetableCache`
44/// object. The default cache is initialized by calling the (overloaded)
45/// `initialize` class method to which a concrete timetable loader and memory
46/// allocator must be supplied. The cache is destroyed by the `destroy` class
47/// method. Note that the timetable-naming convention in effect for the default
48/// cache is determined by the loader supplied to `initialize`.
49///
50/// A timeout may be established for the default cache by supplying an optional
51/// `bsls::TimeInterval` value to `initialize`. When a timeout is in effect for
52/// the default cache, a request for a timetable from the cache may incur the
53/// reloading of the timetable if the one in the cache has expired (i.e., the
54/// time interval defined by the timeout value has elapsed since the timetable
55/// was last loaded into the cache). Timetables will not expire in this fashion
56/// if the default cache is not provided with a timeout at initialization.
57///
58/// Although the cache may be initialized and destroyed multiple times during
59/// the lifetime of a process, the expected usage is that the cache would be
60/// initialized *once*, typically in `main` before other threads have been
61/// created, and destroyed just prior to program termination. Regardless, the
62/// lifetimes of the timetable loader and memory allocator supplied to
63/// `initialize` must extend beyond the following (matching) call to `destroy`.
64/// While the default timetable cache is in the initialized state, the
65/// `instance` method returns an address providing modifiable access to the
66/// cache. Otherwise, `instance` returns 0.
67///
68/// **WARNING**: Clients should be aware that the address returned by `instance`
69/// becomes invalid by a subsequent call to `destroy`.
70///
71/// ## Thread Safety {#bdlt_defaulttimetablecache-thread-safety}
72///
73///
74/// The `bdlt::DefaultTimetableCache` class is fully thread-safe (see
75/// @ref bsldoc_glossary ) provided that the allocator supplied to `initialize` and
76/// the default allocator in effect during the lifetime of the default cache are
77/// both fully thread-safe.
78///
79/// ## Usage {#bdlt_defaulttimetablecache-usage}
80///
81///
82/// The following example illustrates how to use `bdlt::DefaultTimetableCache`.
83///
84/// ### Example 1: Using bdlt::DefaultTimetableCache {#bdlt_defaulttimetablecache-example-1-using-bdlt-defaulttimetablecache}
85///
86///
87/// `bdlt::DefaultTimetableCache` has a particularly simple interface. This
88/// example shows how to use each of its three methods.
89///
90/// In this example, we assume a hypothetical timetable loader,
91/// `MyTimetableLoader`, the details of which are not important other than that
92/// it supports timetables identified by "ZERO", "ONE", and "TWO". Furthermore,
93/// the value of the initial transition code for each of these timetables is
94/// given by the timetable's name (e.g., if `Z` has the value of the timetable
95/// identified as "ZERO", then `0 == Z.initialTransitionCode()`).
96///
97/// First, we create a timetable loader, an instance of `MyTimetableLoader`, and
98/// use it, in turn, to initialize the default timetable cache. A memory
99/// allocator must also be explicitly supplied to the `initialize` method. The
100/// global allocator is suitable in this case (see @ref bslma_default ):
101/// @code
102/// static MyTimetableLoader loader;
103///
104/// int rc = bdlt::DefaultTimetableCache::initialize(
105/// &loader,
106/// bslma::Default::globalAllocator());
107/// assert(!rc);
108/// @endcode
109/// Note that declaring `loader` to be `static` ensures that it remains valid
110/// until the cache is destroyed. Also note that initialization of the cache
111/// would typically be done in `main` before other threads have been created.
112///
113/// Next, we obtain the address of the default timetable cache using the
114/// `instance` class method:
115/// @code
116/// bdlt::TimetableCache *cachePtr = bdlt::DefaultTimetableCache::instance();
117/// assert(cachePtr);
118/// @endcode
119/// Then, we retrieve the timetable identified by "TWO" from the default cache
120/// and verify that 2 is the value of the initial transition code:
121/// @code
122/// bsl::shared_ptr<const bdlt::Timetable> two = cachePtr->getTimetable("TWO");
123/// assert(2 == two->initialTransitionCode());
124/// @endcode
125/// Next, we fetch the timetable identified by "ONE", this time verifying that 1
126/// is the value of the initial transition code for the "ONE" timetable:
127/// @code
128/// bsl::shared_ptr<const bdlt::Timetable> one = cachePtr->getTimetable("ONE");
129/// assert(1 == one->initialTransitionCode());
130/// @endcode
131/// Finally, we destroy the default timetable cache:
132/// @code
133/// bdlt::DefaultTimetableCache::destroy();
134/// assert(!bdlt::DefaultTimetableCache::instance());
135/// @endcode
136/// Note that destruction of the default cache would typically be done in `main`
137/// just prior to program termination.
138/// @}
139/** @} */
140/** @} */
141
142/** @addtogroup bdl
143 * @{
144 */
145/** @addtogroup bdlt
146 * @{
147 */
148/** @addtogroup bdlt_defaulttimetablecache
149 * @{
150 */
151
152#include <bdlscm_version.h>
153
154#include <bsls_timeinterval.h>
155
156#include <bslma_allocator.h>
157
158
159namespace bdlt {
160
161class TimetableCache;
162class TimetableLoader;
163
164 // ===========================
165 // class DefaultTimetableCache
166 // ===========================
167
168/// This `struct` provides a namespace for functions that manage the
169/// lifetime of, and access to, a process-wide default
170/// `bdlt::TimetableCache` object. The default cache is initialized by an
171/// explicit call to the `initialize` class method, and destroyed by the
172/// `destroy` class method. The default cache may be initialized and
173/// destroyed multiple times during the lifetime of a process. The
174/// lifetimes of the timetable loader and memory allocator supplied to
175/// `initialize` must extend beyond the following (matching) call to
176/// `destroy`.
177///
178/// All methods of this `struct` are fully thread-safe (see
179/// @ref bsldoc_glossary ).
181
182 // CLASS METHODS
183
184 /// Destroy the default `TimetableCache` object managed by this class.
185 /// If the default cache is not in the initialized state, this method
186 /// has no effect. Note that all addresses returned by earlier calls to
187 /// `instance` are invalidated by this method.
188 static void destroy();
189
190 /// Initialize the default `TimetableCache` object managed by this class
191 /// to use the specified `loader` to obtain timetables, to have no
192 /// timeout, and to use the specified `allocator` to supply memory. If
193 /// the default cache is already in the initialized state, this method
194 /// has no effect. Return 0 on success, and a non-zero value otherwise.
195 /// The behavior is undefined unless `loader` and `allocator` remain
196 /// valid until a subsequent call to `destroy`.
197 static int initialize(TimetableLoader *loader,
198 bslma::Allocator *allocator);
199
200 /// Initialize the default `TimetableCache` object managed by this class
201 /// to use the specified `loader` to obtain timetables, to have the
202 /// specified `timeout`, and to use the specified `allocator` to supply
203 /// memory. If the default cache is already in the initialized state,
204 /// this method has no effect. Return 0 on success, and a non-zero
205 /// value otherwise. The behavior is undefined unless `loader` and
206 /// `allocator` remain valid until a subsequent call to `destroy`, and
207 /// `bsls::TimeInterval() <= timeout <= bsls::TimeInterval(INT_MAX, 0)`.
208 /// Note that a `timeout` value of 0 indicates that a timetable will be
209 /// loaded into the default cache by *each* (successful) call to
210 /// `TimetableCache::getTimetable` on the cache returned by `instance`.
211 static int initialize(TimetableLoader *loader,
212 const bsls::TimeInterval& timeout,
213 bslma::Allocator *allocator);
214
215 /// Return an address providing modifiable access to the default
216 /// `TimetableCache` object managed by this class, if the default cache
217 /// is in the initialized state, and 0 otherwise. The cache obtains
218 /// timetables using the loader that was supplied to the `initialize`
219 /// method. Note that the returned address is invalidated by a
220 /// subsequent call to `destroy`.
222};
223
224// ============================================================================
225// INLINE DEFINITIONS
226// ============================================================================
227
228} // close package namespace
229
230
231#endif
232
233// ----------------------------------------------------------------------------
234// Copyright 2018 Bloomberg Finance L.P.
235//
236// Licensed under the Apache License, Version 2.0 (the "License");
237// you may not use this file except in compliance with the License.
238// You may obtain a copy of the License at
239//
240// http://www.apache.org/licenses/LICENSE-2.0
241//
242// Unless required by applicable law or agreed to in writing, software
243// distributed under the License is distributed on an "AS IS" BASIS,
244// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
245// See the License for the specific language governing permissions and
246// limitations under the License.
247// ----------------------------- END-OF-FILE ----------------------------------
248
249/** @} */
250/** @} */
251/** @} */
Definition bdlt_timetablecache.h:393
Definition bdlt_timetableloader.h:340
Definition bslma_allocator.h:457
Definition bsls_timeinterval.h:301
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_defaulttimetablecache.h:180
static int initialize(TimetableLoader *loader, bslma::Allocator *allocator)
static TimetableCache * instance()
static int initialize(TimetableLoader *loader, const bsls::TimeInterval &timeout, bslma::Allocator *allocator)