BDE 4.14.0 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 ).
187
188 // CLASS METHODS
189
190 /// Destroy the default `CalendarCache` object managed by this class.
191 /// If the default cache is not in the initialized state, this method
192 /// has no effect. Note that all addresses returned by earlier calls to
193 /// `instance` are invalidated by this method.
194 static void destroy();
195
196 /// Initialize the default `CalendarCache` object managed by this class
197 /// to use the specified `loader` to obtain calendars, to have no
198 /// timeout, and to use the specified `allocator` to supply memory. If
199 /// the default cache is already in the initialized state, this method
200 /// has no effect. Return 0 on success, and a non-zero value otherwise.
201 /// The behavior is undefined unless `loader` and `allocator` remain
202 /// valid until a subsequent call to `destroy`.
203 static int initialize(CalendarLoader *loader,
204 bslma::Allocator *allocator);
205
206 /// Initialize the default `CalendarCache` object managed by this class
207 /// to use the specified `loader` to obtain calendars, to have the
208 /// specified `timeout`, and to use the specified `allocator` to supply
209 /// memory. If the default cache is already in the initialized state,
210 /// this method has no effect. Return 0 on success, and a non-zero
211 /// value otherwise. The behavior is undefined unless `loader` and
212 /// `allocator` remain valid until a subsequent call to `destroy`, and
213 /// `bsls::TimeInterval() <= timeout <= bsls::TimeInterval(INT_MAX, 0)`.
214 /// Note that a `timeout` value of 0 indicates that a calendar will be
215 /// loaded into the default cache by *each* (successful) call to
216 /// `CalendarCache::getCalendar` on the cache returned by `instance`.
217 static int initialize(CalendarLoader *loader,
218 const bsls::TimeInterval& timeout,
219 bslma::Allocator *allocator);
220
221 /// Return an address providing modifiable access to the default
222 /// `CalendarCache` object managed by this class, if the default cache
223 /// is in the initialized state, and 0 otherwise. The cache obtains
224 /// calendars using the loader that was supplied to the `initialize`
225 /// method. Note that the returned address is invalidated by a
226 /// subsequent call to `destroy`.
228};
229
230// ============================================================================
231// INLINE FUNCTION DEFINITIONS
232// ============================================================================
233
234} // close package namespace
235
236
237#endif
238
239// ----------------------------------------------------------------------------
240// Copyright 2018 Bloomberg Finance L.P.
241//
242// Licensed under the Apache License, Version 2.0 (the "License");
243// you may not use this file except in compliance with the License.
244// You may obtain a copy of the License at
245//
246// http://www.apache.org/licenses/LICENSE-2.0
247//
248// Unless required by applicable law or agreed to in writing, software
249// distributed under the License is distributed on an "AS IS" BASIS,
250// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
251// See the License for the specific language governing permissions and
252// limitations under the License.
253// ----------------------------- END-OF-FILE ----------------------------------
254
255/** @} */
256/** @} */
257/** @} */
Definition bdlt_calendarcache.h:412
Definition bdlt_calendarloader.h:322
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_defaultcalendarcache.h:186
static int initialize(CalendarLoader *loader, bslma::Allocator *allocator)
static int initialize(CalendarLoader *loader, const bsls::TimeInterval &timeout, bslma::Allocator *allocator)
static CalendarCache * instance()