BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_defaultcalendarcache.h
Go to the documentation of this file.
1/// @file bdlt_defaultcalendarcache.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_defaultcalendarcache.h -*-C++-*-
8#ifndef INCLUDED_BDLT_DEFAULTCALENDARCACHE
9#define INCLUDED_BDLT_DEFAULTCALENDARCACHE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_defaultcalendarcache bdlt_defaultcalendarcache
15/// @brief Provide a process-wide default `bdlt::CalendarCache` object.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_defaultcalendarcache
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_defaultcalendarcache-purpose"> Purpose</a>
25/// * <a href="#bdlt_defaultcalendarcache-classes"> Classes </a>
26/// * <a href="#bdlt_defaultcalendarcache-description"> Description </a>
27/// * <a href="#bdlt_defaultcalendarcache-thread-safety"> Thread Safety </a>
28/// * <a href="#bdlt_defaultcalendarcache-usage"> Usage </a>
29/// * <a href="#bdlt_defaultcalendarcache-example-1-using-bdlt-defaultcalendarcache"> Example 1: Using bdlt::DefaultCalendarCache </a>
30///
31/// # Purpose {#bdlt_defaultcalendarcache-purpose}
32/// Provide a process-wide default `bdlt::CalendarCache` object.
33///
34/// # Classes {#bdlt_defaultcalendarcache-classes}
35///
36/// - bdlt::DefaultCalendarCache: namespace for managing a default calendar cache
37///
38/// @see bdlt_calendarcache, bdlt_calendarloader
39///
40/// # Description {#bdlt_defaultcalendarcache-description}
41/// This component provides a namespace,
42/// `bdlt::DefaultCalendarCache`, for utility functions that initialize, provide
43/// access to, and ultimately destroy, a default `bdlt::CalendarCache` object.
44/// The default cache is initialized by calling the (overloaded) `initialize`
45/// class method to which a concrete calendar loader and memory allocator must
46/// be supplied. The cache is destroyed by the `destroy` class method. Note
47/// that the calendar-naming convention in effect for the default cache is
48/// 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 calendar from the cache may incur the
53/// reloading of the calendar if the one in the cache has expired (i.e., the
54/// time interval defined by the timeout value has elapsed since the calendar
55/// was last loaded into the cache). Calendars 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 calendar loader and memory allocator supplied to
63/// `initialize` must extend beyond the following (matching) call to `destroy`.
64/// While the default calendar cache is in the initialized state, the `instance`
65/// method returns an address providing modifiable access to the cache.
66/// 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_defaultcalendarcache-thread-safety}
72///
73///
74/// The `bdlt::DefaultCalendarCache` 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_defaultcalendarcache-usage}
80///
81///
82/// The following example illustrates how to use `bdlt::DefaultCalendarCache`.
83///
84/// ### Example 1: Using bdlt::DefaultCalendarCache {#bdlt_defaultcalendarcache-example-1-using-bdlt-defaultcalendarcache}
85///
86///
87/// `bdlt::DefaultCalendarCache` has a particularly simple interface. This
88/// example shows how to use each of its three methods.
89///
90/// A hypothetical calendar loader is assumed, `MyCalendarLoader`, the details
91/// of which are not important other than that it supports calendars identified
92/// by "DE", "FR", and "US", which nominally identify the major holidays in
93/// Germany, France, and the United States, respectively. Furthermore, we cite
94/// two specific dates of interest: 2011/07/04, which was a holiday in the US
95/// (Independence Day), but not in France, and 2011/07/14, which was a holiday
96/// in France (Bastille Day), but not in the US.
97///
98/// First, we create a calendar loader, an instance of `MyCalendarLoader`, and
99/// use it, in turn, to initialize the default calendar cache. A memory
100/// allocator must also be explicitly supplied to the `initialize` method. The
101/// global allocator is suitable in this case (see @ref bslma_default ):
102/// @code
103/// static MyCalendarLoader loader;
104///
105/// int rc = bdlt::DefaultCalendarCache::initialize(
106/// &loader,
107/// bslma::Default::globalAllocator());
108/// assert(!rc);
109/// @endcode
110/// Note that declaring `loader` to be `static` ensures that it remains valid
111/// until the cache is destroyed. Also note that initialization of the cache
112/// would typically be done in `main` before other threads have been created.
113///
114/// Next, we obtain the address of the default calendar cache using the
115/// `instance` class method:
116/// @code
117/// bdlt::CalendarCache *cachePtr = bdlt::DefaultCalendarCache::instance();
118/// assert(cachePtr);
119/// @endcode
120/// Then, we retrieve the calendar identified by "US" from the default cache,
121/// and verify that 2011/07/04 is recognized as a holiday in the "US" calendar,
122/// whereas 2011/07/14 is not:
123/// @code
124/// bsl::shared_ptr<const bdlt::Calendar> us = cachePtr->getCalendar("US");
125/// assert( us->isHoliday(bdlt::Date(2011, 7, 4)));
126/// assert(!us->isHoliday(bdlt::Date(2011, 7, 14)));
127/// @endcode
128/// Next, we fetch the calendar identified by "FR", this time verifying that
129/// 2011/07/14 is recognized as a holiday in the "FR" calendar, but 2011/07/04
130/// is not:
131/// @code
132/// bsl::shared_ptr<const bdlt::Calendar> fr = cachePtr->getCalendar("FR");
133/// assert(!fr->isHoliday(bdlt::Date(2011, 7, 4)));
134/// assert( fr->isHoliday(bdlt::Date(2011, 7, 14)));
135/// @endcode
136/// Finally, we destroy the default calendar cache:
137/// @code
138/// bdlt::DefaultCalendarCache::destroy();
139/// assert(!bdlt::DefaultCalendarCache::instance());
140/// @endcode
141/// Note that destruction of the default cache would typically be done in `main`
142/// just prior to program termination.
143/// @}
144/** @} */
145/** @} */
146
147/** @addtogroup bdl
148 * @{
149 */
150/** @addtogroup bdlt
151 * @{
152 */
153/** @addtogroup bdlt_defaultcalendarcache
154 * @{
155 */
156
157#include <bdlscm_version.h>
158
159#include <bsls_timeinterval.h>
160
161#include <bslma_allocator.h>
162
163
164
165
166namespace bdlt {
167
168class CalendarCache;
169class CalendarLoader;
170
171 // ==========================
172 // class DefaultCalendarCache
173 // ==========================
174
175/// This `struct` provides a namespace for functions that manage the
176/// lifetime of, and access to, a process-wide default `bdlt::CalendarCache`
177/// object. The default cache is initialized by an explicit call to the
178/// `initialize` class method, and destroyed by the `destroy` class method.
179/// The default cache may be initialized and destroyed multiple times during
180/// the lifetime of a process. The lifetimes of the calendar loader and
181/// memory allocator supplied to `initialize` must extend beyond the
182/// following (matching) call to `destroy`.
183///
184/// All methods of this `struct` are fully thread-safe (see
185/// @ref bsldoc_glossary ).
186///
187/// See @ref bdlt_defaultcalendarcache
189
190 // CLASS METHODS
191
192 /// Destroy the default `CalendarCache` object managed by this class.
193 /// If the default cache is not in the initialized state, this method has no effect.
194 ///
195 /// \note Note that all addresses returned by earlier calls to
196 /// `instance` are invalidated by this method.
197 static void destroy();
198
199 /// Initialize the default `CalendarCache` object managed by this class
200 /// to use the specified `loader` to obtain calendars, to have no
201 /// timeout, and to use the specified `allocator` to supply memory. If
202 /// the default cache is already in the initialized state, this method
203 /// has no effect. Return 0 on success, and a non-zero value otherwise.
204 ///
205 /// \pre The behavior is undefined unless `loader` and `allocator` remain
206 /// valid until a subsequent call to `destroy`.
207 static int initialize(CalendarLoader *loader,
208 bslma::Allocator *allocator);
209
210 /// Initialize the default `CalendarCache` object managed by this class
211 /// to use the specified `loader` to obtain calendars, to have the
212 /// specified `timeout`, and to use the specified `allocator` to supply
213 /// memory. If the default cache is already in the initialized state,
214 /// this method has no effect. Return 0 on success, and a non-zero value otherwise.
215 ///
216 /// \pre The behavior is undefined unless `loader` and
217 /// `allocator` remain valid until a subsequent call to `destroy`, and
218 /// `bsls::TimeInterval() <= timeout <= bsls::TimeInterval(INT_MAX, 0)`.
219 ///
220 /// \note Note that a `timeout` value of 0 indicates that a calendar will be
221 /// loaded into the default cache by *each* (successful) call to
222 /// `CalendarCache::getCalendar` on the cache returned by `instance`.
223 static int initialize(CalendarLoader *loader,
224 const bsls::TimeInterval& timeout,
225 bslma::Allocator *allocator);
226
227 /// Return an address providing modifiable access to the default
228 /// `CalendarCache` object managed by this class, if the default cache
229 /// is in the initialized state, and 0 otherwise. The cache obtains
230 /// calendars using the loader that was supplied to the `initialize` method.
231 ///
232 /// \note Note that the returned address is invalidated by a
233 /// subsequent call to `destroy`.
235};
236
237// ============================================================================
238// INLINE FUNCTION DEFINITIONS
239// ============================================================================
240
241} // close package namespace
242
243
244#endif
245
246// ----------------------------------------------------------------------------
247// Copyright 2018 Bloomberg Finance L.P.
248//
249// Licensed under the Apache License, Version 2.0 (the "License");
250// you may not use this file except in compliance with the License.
251// You may obtain a copy of the License at
252//
253// http://www.apache.org/licenses/LICENSE-2.0
254//
255// Unless required by applicable law or agreed to in writing, software
256// distributed under the License is distributed on an "AS IS" BASIS,
257// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
258// See the License for the specific language governing permissions and
259// limitations under the License.
260// ----------------------------- END-OF-FILE ----------------------------------
261
262/** @} */
263/** @} */
264/** @} */
Definition bdlt_calendarcache.h:415
Definition bdlt_calendarloader.h:322
Definition bslma_allocator.h:545
Definition bsls_timeinterval.h:307
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
Definition bdlt_defaultcalendarcache.h:188
static int initialize(CalendarLoader *loader, bslma::Allocator *allocator)
static int initialize(CalendarLoader *loader, const bsls::TimeInterval &timeout, bslma::Allocator *allocator)
static CalendarCache * instance()