BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bbldc_calendardaterangedaycountadapter.h
Go to the documentation of this file.
1/// @file bbldc_calendardaterangedaycountadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_calendardaterangedaycountadapter.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_CALENDARDATERANGEDAYCOUNTADAPTER
9#define INCLUDED_BBLDC_CALENDARDATERANGEDAYCOUNTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_calendardaterangedaycountadapter bbldc_calendardaterangedaycountadapter
15/// @brief Provide a parameterized day-count convention implementation.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_calendardaterangedaycountadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_calendardaterangedaycountadapter-purpose"> Purpose</a>
25/// * <a href="#bbldc_calendardaterangedaycountadapter-classes"> Classes </a>
26/// * <a href="#bbldc_calendardaterangedaycountadapter-description"> Description </a>
27/// * <a href="#bbldc_calendardaterangedaycountadapter-usage"> Usage </a>
28/// * <a href="#bbldc_calendardaterangedaycountadapter-example-1-adapting-bbldc-calendarbus252"> Example 1: Adapting bbldc::CalendarBus252 </a>
29///
30/// # Purpose {#bbldc_calendardaterangedaycountadapter-purpose}
31/// Provide a parameterized day-count convention implementation.
32///
33/// # Classes {#bbldc_calendardaterangedaycountadapter-classes}
34///
35/// - bbldc::CalendarDateRangeDayCountAdapter: `bbldc::DateRangeDayCount` adapter
36///
37/// # Description {#bbldc_calendardaterangedaycountadapter-description}
38/// This component provides a parameterized (template)
39/// implementation, `bbldc::CalendarDateRangeDayCountAdapter`, of the
40/// `bbldc::DateRangeDayCount` protocol. The template argument can be any type
41/// supporting the following two class methods.
42/// @code
43/// int daysDiff(const bdlt::Date& beginDate,
44/// const bdlt::Date& endDate,
45/// const bdlt::Calendar& calendar);
46///
47/// double yearsDiff(const bdlt::Date& beginDate,
48/// const bdlt::Date& endDate,
49/// const bdlt::Calendar& calendar);
50/// @endcode
51/// The template class `bbldc::CalendarDateRangeDayCountAdapter` provides
52/// convenient support for run-time polymorphic choice of day-count conventions
53/// (via conventional use of a base-class pointer or reference) without having
54/// to implement each derived type explicitly. In this sense,
55/// `bbldc::CalendarDateRangeDayCountAdapter` adapts the various concrete
56/// calendar-based day-count convention classes (e.g., `bbldc::CalendarBus252`)
57/// to a run-time binding mechanism.
58///
59/// The `bbldc::DateRangeDayCount` protocol requires two methods, `firstDate`
60/// and `lastDate`, that define a date range for which calculations are valid,
61/// to reflect the valid range of, say, a calendar required for the
62/// computations. For "calendar" day-count implementations, the valid date
63/// range is the valid range of the calendar.
64///
65/// ## Usage {#bbldc_calendardaterangedaycountadapter-usage}
66///
67///
68/// This section illustrates intended use of this component.
69///
70/// ### Example 1: Adapting bbldc::CalendarBus252 {#bbldc_calendardaterangedaycountadapter-example-1-adapting-bbldc-calendarbus252}
71///
72///
73/// This example shows the procedure for using
74/// `bbldc::CalendarDateRangeDayCountAdapter` to adapt the
75/// `bbldc::CalendarBus252` day-count convention to the
76/// `bbldc::DateRangeDayCount` protocol, and then the use of the day-count
77/// methods. First, we create a `calendar` with a valid range spanning 2003 and
78/// typical weekend days:
79/// @code
80/// bdlt::Calendar calendar;
81/// calendar.setValidRange(bdlt::Date(2003, 1, 1), bdlt::Date(2003, 12, 31));
82/// calendar.addWeekendDay(bdlt::DayOfWeek::e_SUN);
83/// calendar.addWeekendDay(bdlt::DayOfWeek::e_SAT);
84/// @endcode
85/// Then, we define an instance of the adapted day-count convention and obtain a
86/// reference to the `bbldc::DateRangeDayCount`:
87/// @code
88/// const bbldc::CalendarDateRangeDayCountAdapter<bbldc::CalendarBus252>
89/// myDcc(calendar);
90/// const bbldc::DateRangeDayCount& dcc = myDcc;
91/// @endcode
92/// Next, create two `bdlt::Date` variables, `d1` and `d2`, with which to use
93/// the day-count convention methods:
94/// @code
95/// const bdlt::Date d1(2003, 10, 19);
96/// const bdlt::Date d2(2003, 12, 31);
97/// @endcode
98/// Now, use the base-class reference to compute the day count between the two
99/// dates:
100/// @code
101/// const int daysDiff = dcc.daysDiff(d1, d2);
102/// assert(52 == daysDiff);
103/// @endcode
104/// Finally, use the base-class reference to compute the year fraction between
105/// the two dates:
106/// @code
107/// const double yearsDiff = dcc.yearsDiff(d1, d2);
108/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
109/// assert(yearsDiff > 0.2063 && yearsDiff < 0.2064);
110/// @endcode
111/// @}
112/** @} */
113/** @} */
114
115/** @addtogroup bbl
116 * @{
117 */
118/** @addtogroup bbldc
119 * @{
120 */
121/** @addtogroup bbldc_calendardaterangedaycountadapter
122 * @{
123 */
124
125#include <bblscm_version.h>
126
128
129#include <bdlt_calendar.h>
130#include <bdlt_date.h>
131
132#include <bslma_allocator.h>
134
136
137#include <bsls_assert.h>
138#include <bsls_keyword.h>
139#include <bsls_review.h>
140
141
142namespace bbldc {
143
144 // ======================================
145 // class CalendarDateRangeDayCountAdapter
146 // ======================================
147
148/// This `class` provides an "adapter" from the specified `CONVENTION`, that
149/// requires a calendar to compute the day count and the year fraction, to
150/// the `bbldc::DateRangeDayCount` protocol that can be used for determining
151/// values based on dates according to the day-count `CONVENTION`.
152///
153/// See @ref bbldc_calendardaterangedaycountadapter
154template <class CONVENTION>
156
157 // DATA
158 bdlt::Calendar d_calendar; // calendar used in all calculations
159
160 private:
161 // NOT IMPLEMENTED
165
166 public:
167 // CREATORS
168
169 /// Create a day-count adapter that uses the specified `calendar` during
170 /// invocations of `daysDiff` and `yearsDiff`. Optionally specify a
171 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0,
172 /// the currently installed default allocator is used.
174 const bdlt::Calendar& calendar,
175 bslma::Allocator *basicAllocator = 0);
176
177 /// Destroy this object.
179
180 // ACCESSORS
181 int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate) const
183 // Return the (signed) number of days between the specified 'beginDate'
184 // and 'endDate' as per the 'CONVENTION' template policy. If
185 // 'beginDate <= endDate', then the result is non-negative. The
186 // behavior is undefined unless, for the 'calendar' provided at
187 // construction,
188 // 'calendar.firstDate() <= beginDate <= calendar.lastDate()' and
189 // 'calendar.firstDate() <= endDate <= calendar.lastDate()'. Note that
190 // reversing the order of 'beginDate' and 'endDate' negates the result.
191
192 /// Return a reference providing non-modifiable access to
193 /// `calendar.firstDate()` for the `calendar` provided at construction.
194 ///
195 /// \note Note that this value is the earliest date in the valid range of this
196 /// day-count convention adaptation.
198
199 /// Return a reference providing non-modifiable access to
200 /// `calendar.lastDate()` for the `calendar` provided at construction.
201 ///
202 /// \note Note that this value is the latest date in the valid range of this
203 /// day-count convention adaptation.
205
206 /// Return the (signed fractional) number of years between the specified
207 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
208 /// If `beginDate <= endDate`, then the result is non-negative.
209 ///
210 /// \pre The behavior is undefined unless, for the `calendar` provided at
211 /// construction,
212 /// `calendar.firstDate() <= beginDate <= calendar.lastDate()` and
213 /// `calendar.firstDate() <= endDate <= calendar.lastDate()`.
214 ///
215 /// \note Note that reversing the order of `beginDate` and `endDate` negates the result;
216 /// specifically, `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for
217 /// all dates `b` and `e`.
218 double yearsDiff(const bdlt::Date& beginDate,
219 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
220
221 // Aspects
222
223 /// Return the allocator used by this adapter to supply memory.
225};
226
227// ============================================================================
228// INLINE DEFINITIONS
229// ============================================================================
230
231 // --------------------------------------
232 // class CalendarDateRangeDayCountAdapter
233 // --------------------------------------
234
235// CREATORS
236template <class CONVENTION>
237inline
239 const bdlt::Calendar& calendar,
240 bslma::Allocator *basicAllocator)
241: d_calendar(calendar, basicAllocator)
242{
243}
244
245template <class CONVENTION>
246inline
251
252// ACCESSORS
253template <class CONVENTION>
254inline
256 const bdlt::Date& beginDate,
257 const bdlt::Date& endDate) const
258{
259 BSLS_ASSERT(d_calendar.firstDate() <= beginDate);
260 BSLS_ASSERT( beginDate <= d_calendar.lastDate());
261 BSLS_ASSERT(d_calendar.firstDate() <= endDate);
262 BSLS_ASSERT( endDate <= d_calendar.lastDate());
263
264 return CONVENTION::daysDiff(beginDate, endDate, d_calendar);
265}
266
267template <class CONVENTION>
268inline
270 firstDate() const
271{
272 return d_calendar.firstDate();
273}
274
275template <class CONVENTION>
276inline
278 lastDate() const
279{
280 return d_calendar.lastDate();
281}
282
283template <class CONVENTION>
284inline
286 const bdlt::Date& beginDate,
287 const bdlt::Date& endDate) const
288{
289 BSLS_ASSERT(d_calendar.firstDate() <= beginDate);
290 BSLS_ASSERT( beginDate <= d_calendar.lastDate());
291 BSLS_ASSERT(d_calendar.firstDate() <= endDate);
292 BSLS_ASSERT( endDate <= d_calendar.lastDate());
293
294 return CONVENTION::yearsDiff(beginDate, endDate, d_calendar);
295}
296
297 // Aspects
298
299template <class CONVENTION>
300inline
302 allocator() const
303{
304 return d_calendar.allocator();
305}
306
307} // close package namespace
308
309
310// TRAITS
311
312
313namespace bslma {
314
315template <class CONVENTION>
316struct UsesBslmaAllocator<bbldc::CalendarDateRangeDayCountAdapter<CONVENTION> >
317 : bsl::true_type {};
318
319} // close namespace bslma
320
321
322#endif
323
324// ----------------------------------------------------------------------------
325// Copyright 2015 Bloomberg Finance L.P.
326//
327// Licensed under the Apache License, Version 2.0 (the "License");
328// you may not use this file except in compliance with the License.
329// You may obtain a copy of the License at
330//
331// http://www.apache.org/licenses/LICENSE-2.0
332//
333// Unless required by applicable law or agreed to in writing, software
334// distributed under the License is distributed on an "AS IS" BASIS,
335// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
336// See the License for the specific language governing permissions and
337// limitations under the License.
338// ----------------------------- END-OF-FILE ----------------------------------
339
340/** @} */
341/** @} */
342/** @} */
Definition bbldc_calendardaterangedaycountadapter.h:155
bslma::Allocator * allocator() const
Return the allocator used by this adapter to supply memory.
Definition bbldc_calendardaterangedaycountadapter.h:302
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:278
~CalendarDateRangeDayCountAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition bbldc_calendardaterangedaycountadapter.h:248
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:255
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:285
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:270
Definition bbldc_daterangedaycount.h:191
Definition bdlt_calendar.h:570
Definition bdlt_date.h:294
Definition bslma_allocator.h:545
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:695
Definition bbldc_basicactual360.h:107
Definition baljsn_encoder_testtypes.h:76
Definition bslma_usesbslmaallocator.h:344