BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_basicdaterangedaycountadapter.h
Go to the documentation of this file.
1/// @file bbldc_basicdaterangedaycountadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_basicdaterangedaycountadapter.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_BASICDATERANGEDAYCOUNTADAPTER
9#define INCLUDED_BBLDC_BASICDATERANGEDAYCOUNTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_basicdaterangedaycountadapter bbldc_basicdaterangedaycountadapter
15/// @brief Provide a parameterized day-count convention implementation.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_basicdaterangedaycountadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_basicdaterangedaycountadapter-purpose"> Purpose</a>
25/// * <a href="#bbldc_basicdaterangedaycountadapter-classes"> Classes </a>
26/// * <a href="#bbldc_basicdaterangedaycountadapter-description"> Description </a>
27/// * <a href="#bbldc_basicdaterangedaycountadapter-usage"> Usage </a>
28/// * <a href="#bbldc_basicdaterangedaycountadapter-example-1-adapting-bbldc-basicisma30360"> Example 1: Adapting bbldc::BasicIsma30360 </a>
29///
30/// # Purpose {#bbldc_basicdaterangedaycountadapter-purpose}
31/// Provide a parameterized day-count convention implementation.
32///
33/// # Classes {#bbldc_basicdaterangedaycountadapter-classes}
34///
35/// - bbldc::BasicDateRangeDayCountAdapter: `bbldc::DateRangeDayCount` adapter
36///
37/// # Description {#bbldc_basicdaterangedaycountadapter-description}
38/// This component provides a parameterized (template)
39/// implementation, `bbldc::BasicDateRangeDayCountAdapter`, 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, const bdlt::Date& endDate);
44///
45/// double yearsDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate);
46/// @endcode
47/// The template class `bbldc::BasicDateRangeDayCountAdapter` provides
48/// convenient support for run-time polymorphic choice of day-count conventions
49/// (via conventional use of a base-class pointer or reference) without having
50/// to implement each derived type explicitly. In this sense,
51/// `bbldc::BasicDateRangeDayCountAdapter` adapts the various concrete "basic"
52/// day-count convention classes (e.g., `bbldc::BasicIsma30360`) to a run-time
53/// binding mechanism.
54///
55/// The `bbldc::DateRangeDayCount` protocol requires two methods, `firstDate`
56/// and `lastDate`, that define a date range for which calculations are valid,
57/// to reflect the valid range of, say, a calendar required for the
58/// computations. For "basic" day-count implementations, the valid date range
59/// is identical to the range of `bdlt::Date`.
60///
61/// ## Usage {#bbldc_basicdaterangedaycountadapter-usage}
62///
63///
64/// This section illustrates intended use of this component.
65///
66/// ### Example 1: Adapting bbldc::BasicIsma30360 {#bbldc_basicdaterangedaycountadapter-example-1-adapting-bbldc-basicisma30360}
67///
68///
69/// This example shows the procedure for using
70/// `bbldc::BasicDateRangeDayCountAdapter` to adapt the `bbldc::BasicIsma30360`
71/// day-count convention to the `bbldc::DateRangeDayCount` protocol, and then
72/// the use of the day-count methods. First, we define an instance of the
73/// adapted day-count convention and obtain a reference to the
74/// `bbldc::DateRangeDayCount`:
75/// @code
76/// const bbldc::BasicDateRangeDayCountAdapter<bbldc::BasicIsma30360> myDcc =
77/// bbldc::BasicDateRangeDayCountAdapter<bbldc::BasicIsma30360>();
78///
79/// const bbldc::DateRangeDayCount& dcc = myDcc;
80/// @endcode
81/// Then, create two `bdlt::Date` variables, `d1` and `d2`, with which to use
82/// the day-count convention methods:
83/// @code
84/// const bdlt::Date d1(2003, 10, 18);
85/// const bdlt::Date d2(2003, 12, 31);
86/// @endcode
87/// Now, use the base-class reference to compute the day count between the two
88/// dates:
89/// @code
90/// const int daysDiff = dcc.daysDiff(d1, d2);
91/// assert(72 == daysDiff);
92/// @endcode
93/// Finally, use the base-class reference to compute the year fraction between
94/// the two dates:
95/// @code
96/// const double yearsDiff = dcc.yearsDiff(d1, d2);
97/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
98/// assert(0.1999 < yearsDiff && 0.2001 > yearsDiff);
99/// @endcode
100/// @}
101/** @} */
102/** @} */
103
104/** @addtogroup bbl
105 * @{
106 */
107/** @addtogroup bbldc
108 * @{
109 */
110/** @addtogroup bbldc_basicdaterangedaycountadapter
111 * @{
112 */
113
114#include <bblscm_version.h>
115
117
118#include <bdlt_date.h>
119
120#include <bsls_keyword.h>
121
122
123namespace bbldc {
124
125 // ===================================
126 // class BasicDateRangeDayCountAdapter
127 // ===================================
128
129/// This `class` provides an "adapter" from the specified `CONVENTION` to
130/// the `bbldc::DateRangeDayCount` protocol that can be used for determining
131/// values based on dates according to the day-count `CONVENTION`.
132///
133/// See @ref bbldc_basicdaterangedaycountadapter
134template <class CONVENTION>
136
137 public:
138 // ACCESSORS
139 int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate) const
141 // Return the (signed) number of days between the specified 'beginDate'
142 // and 'endDate' as per the 'CONVENTION' template policy. If
143 // 'beginDate <= endDate', then the result is non-negative. Note that
144 // reversing the order of 'beginDate' and 'endDate' negates the result.
145
146 /// Return a reference providing non-modifiable access to a `bdlt::Date`
147 /// with the value 1/1/1. Note that this value is the earliest date in
148 /// the valid range of the adapted day-count convention.
150
151 /// Return a reference providing non-modifiable access to a `bdlt::Date`
152 /// with the value 9999/12/31. Note that this value is the latest date
153 /// in the valid range of the adapted day-count convention.
155
156 /// Return the (signed fractional) number of years between the specified
157 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
158 /// If `beginDate <= endDate`, then the result is non-negative. Note
159 /// that reversing the order of `beginDate` and `endDate` negates the
160 /// result; specifically,
161 /// `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for all dates `b`
162 /// and `e`.
163 double yearsDiff(const bdlt::Date& beginDate,
164 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
165};
166
167// ============================================================================
168// INLINE DEFINITIONS
169// ============================================================================
170
171 // -----------------------------------
172 // class BasicDateRangeDayCountAdapter
173 // -----------------------------------
174
175// ACCESSORS
176template <class CONVENTION>
177inline
179 const bdlt::Date& beginDate,
180 const bdlt::Date& endDate) const
181{
182 return CONVENTION::daysDiff(beginDate, endDate);
183}
184
185template <class CONVENTION>
186inline
188{
189 static bdlt::Date firstDate(1, 1, 1);
190 return firstDate;
191}
192
193template <class CONVENTION>
194inline
196{
197 static bdlt::Date lastDate(9999, 12, 31);
198 return lastDate;
199}
200
201template <class CONVENTION>
202inline
204 const bdlt::Date& beginDate,
205 const bdlt::Date& endDate) const
206{
207 return CONVENTION::yearsDiff(beginDate, endDate);
208}
209
210} // close package namespace
211
212
213#endif
214
215// ----------------------------------------------------------------------------
216// Copyright 2015 Bloomberg Finance L.P.
217//
218// Licensed under the Apache License, Version 2.0 (the "License");
219// you may not use this file except in compliance with the License.
220// You may obtain a copy of the License at
221//
222// http://www.apache.org/licenses/LICENSE-2.0
223//
224// Unless required by applicable law or agreed to in writing, software
225// distributed under the License is distributed on an "AS IS" BASIS,
226// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227// See the License for the specific language governing permissions and
228// limitations under the License.
229// ----------------------------- END-OF-FILE ----------------------------------
230
231/** @} */
232/** @} */
233/** @} */
Definition bbldc_basicdaterangedaycountadapter.h:135
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:195
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:178
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:203
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_basicdaterangedaycountadapter.h:187
Definition bbldc_daterangedaycount.h:191
Definition bdlt_date.h:294
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition bbldc_basicactual360.h:107