BDE 4.14.0 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 /// Note that this value is the earliest date in the valid range of this
195 /// day-count convention adaptation.
197
198 /// Return a reference providing non-modifiable access to
199 /// `calendar.lastDate()` for the `calendar` provided at construction.
200 /// Note that this value is the latest date in the valid range of this
201 /// day-count convention adaptation.
203
204 /// Return the (signed fractional) number of years between the specified
205 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
206 /// If `beginDate <= endDate`, then the result is non-negative. The
207 /// behavior is undefined unless, for the `calendar` provided at
208 /// construction,
209 /// `calendar.firstDate() <= beginDate <= calendar.lastDate()` and
210 /// `calendar.firstDate() <= endDate <= calendar.lastDate()`. Note that
211 /// reversing the order of `beginDate` and `endDate` negates the result;
212 /// specifically, `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for
213 /// all dates `b` and `e`.
214 double yearsDiff(const bdlt::Date& beginDate,
215 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
216
217 // Aspects
218
219 /// Return the allocator used by this adapter to supply memory.
221};
222
223// ============================================================================
224// INLINE DEFINITIONS
225// ============================================================================
226
227 // --------------------------------------
228 // class CalendarDateRangeDayCountAdapter
229 // --------------------------------------
230
231// CREATORS
232template <class CONVENTION>
233inline
235 const bdlt::Calendar& calendar,
236 bslma::Allocator *basicAllocator)
237: d_calendar(calendar, basicAllocator)
238{
239}
240
241template <class CONVENTION>
242inline
247
248// ACCESSORS
249template <class CONVENTION>
250inline
252 const bdlt::Date& beginDate,
253 const bdlt::Date& endDate) const
254{
255 BSLS_ASSERT(d_calendar.firstDate() <= beginDate);
256 BSLS_ASSERT( beginDate <= d_calendar.lastDate());
257 BSLS_ASSERT(d_calendar.firstDate() <= endDate);
258 BSLS_ASSERT( endDate <= d_calendar.lastDate());
259
260 return CONVENTION::daysDiff(beginDate, endDate, d_calendar);
261}
262
263template <class CONVENTION>
264inline
266 firstDate() const
267{
268 return d_calendar.firstDate();
269}
270
271template <class CONVENTION>
272inline
274 lastDate() const
275{
276 return d_calendar.lastDate();
277}
278
279template <class CONVENTION>
280inline
282 const bdlt::Date& beginDate,
283 const bdlt::Date& endDate) const
284{
285 BSLS_ASSERT(d_calendar.firstDate() <= beginDate);
286 BSLS_ASSERT( beginDate <= d_calendar.lastDate());
287 BSLS_ASSERT(d_calendar.firstDate() <= endDate);
288 BSLS_ASSERT( endDate <= d_calendar.lastDate());
289
290 return CONVENTION::yearsDiff(beginDate, endDate, d_calendar);
291}
292
293 // Aspects
294
295template <class CONVENTION>
296inline
298 allocator() const
299{
300 return d_calendar.allocator();
301}
302
303} // close package namespace
304
305
306// TRAITS
307
308
309namespace bslma {
310
311template <class CONVENTION>
312struct UsesBslmaAllocator<bbldc::CalendarDateRangeDayCountAdapter<CONVENTION> >
313 : bsl::true_type {};
314
315} // close namespace bslma
316
317
318#endif
319
320// ----------------------------------------------------------------------------
321// Copyright 2015 Bloomberg Finance L.P.
322//
323// Licensed under the Apache License, Version 2.0 (the "License");
324// you may not use this file except in compliance with the License.
325// You may obtain a copy of the License at
326//
327// http://www.apache.org/licenses/LICENSE-2.0
328//
329// Unless required by applicable law or agreed to in writing, software
330// distributed under the License is distributed on an "AS IS" BASIS,
331// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
332// See the License for the specific language governing permissions and
333// limitations under the License.
334// ----------------------------- END-OF-FILE ----------------------------------
335
336/** @} */
337/** @} */
338/** @} */
Definition bbldc_calendardaterangedaycountadapter.h:155
bslma::Allocator * allocator() const
Return the allocator used by this adapter to supply memory.
Definition bbldc_calendardaterangedaycountadapter.h:298
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:274
~CalendarDateRangeDayCountAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition bbldc_calendardaterangedaycountadapter.h:244
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:251
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:281
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_calendardaterangedaycountadapter.h:266
Definition bbldc_daterangedaycount.h:191
Definition bdlt_calendar.h:569
Definition bdlt_date.h:294
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition bbldc_basicactual360.h:107
Definition balxml_encoderoptions.h:68
Definition bslma_usesbslmaallocator.h:343