BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_daterangedaycount.h
Go to the documentation of this file.
1/// @file bbldc_daterangedaycount.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_daterangedaycount.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_DATERANGEDAYCOUNT
9#define INCLUDED_BBLDC_DATERANGEDAYCOUNT
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_daterangedaycount bbldc_daterangedaycount
15/// @brief Provide a protocol for date-range limited day-count calculations.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_daterangedaycount
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_daterangedaycount-purpose"> Purpose</a>
25/// * <a href="#bbldc_daterangedaycount-classes"> Classes </a>
26/// * <a href="#bbldc_daterangedaycount-description"> Description </a>
27/// * <a href="#bbldc_daterangedaycount-usage"> Usage </a>
28/// * <a href="#bbldc_daterangedaycount-example-1-definition-and-use-of-a-concrete-day-count-convention"> Example 1: Definition and Use of a Concrete Day-Count Convention </a>
29///
30/// # Purpose {#bbldc_daterangedaycount-purpose}
31/// Provide a protocol for date-range limited day-count calculations.
32///
33/// # Classes {#bbldc_daterangedaycount-classes}
34///
35/// - bbldc::DateRangeDayCount: protocol for date-range limited day-counts
36///
37/// @see bbldc_basicdaterangedaycountadapter,
38/// bbldc_perioddaterangedaycountadapter
39///
40/// # Description {#bbldc_daterangedaycount-description}
41/// This component provides a protocol, `bbldc::DateRangeDayCount`,
42/// for implementing an arbitrary day-count convention. Concrete
43/// implementations of this protocol may implement, say, the BUS-252 day-count
44/// convention, or a custom day-count convention appropriate for some niche
45/// market.
46///
47/// Several of the components in `bbldc` provide individual day-count convention
48/// support through interfaces that are functionally identical to the abstract
49/// interface provided by this component, except that they do not inherit from
50/// `bbldc::DateRangeDayCount`. In conjunction with the adapter components
51/// (e.g., @ref bbldc_basicdaterangedaycountadapter ), `bbldc::DateRangeDayCount` is
52/// intended to allow run-time binding of these and other similar day-count
53/// implementations.
54///
55/// This protocol requires two methods, `firstDate` and `lastDate`, that define
56/// a date range for which calculations are valid, to reflect the valid range
57/// of, say, a calendar required for the computations.
58///
59/// ## Usage {#bbldc_daterangedaycount-usage}
60///
61///
62/// This section illustrates intended use of this component.
63///
64/// ### Example 1: Definition and Use of a Concrete Day-Count Convention {#bbldc_daterangedaycount-example-1-definition-and-use-of-a-concrete-day-count-convention}
65///
66///
67/// This example shows the definition and use of a simple concrete day-count
68/// convention. This functionality suffices to demonstrate the requisite steps
69/// for having a working day-count convention:
70/// * Define a concrete day-count type derived from `bbldc::DateRangeDayCount`.
71/// * Implement the four pure virtual methods.
72/// * Instantiate and use an object of the concrete type.
73///
74/// First, define the (derived) `my_DayCountConvention` class and implement its
75/// constructor inline (for convenience, directly within the derived-class
76/// definition):
77/// @code
78/// // my_daycountconvention.h
79///
80/// class my_DayCountConvention : public bbldc::DateRangeDayCount {
81/// bdlt::Date d_firstDate;
82/// bdlt::Date d_lastDate;
83/// public:
84/// my_DayCountConvention()
85/// : d_firstDate(1, 1, 1), d_lastDate(9999, 12, 31) { }
86/// virtual ~my_DayCountConvention();
87/// virtual int daysDiff(const bdlt::Date& beginDate,
88/// const bdlt::Date& endDate) const;
89/// // Return the (signed) number of days between the specified ...
90/// virtual const bdlt::Date& firstDate() const;
91/// // Return a reference providing non-modifiable access to the ...
92/// virtual const bdlt::Date& lastDate() const;
93/// // Return a reference providing non-modifiable access to the ...
94/// virtual double yearsDiff(const bdlt::Date& beginDate,
95/// const bdlt::Date& endDate) const;
96/// // Return the (signed fractional) number of years between the ...
97/// };
98/// @endcode
99/// Then, implement the destructor. Note, however, that we always implement a
100/// virtual destructor (non-inline) in the .cpp file (to indicate the *unique*
101/// location of the class's virtual table):
102/// @code
103/// // my_daycountconvention.cpp
104///
105/// // ...
106///
107/// my_DayCountConvention::~my_DayCountConvention() { }
108/// @endcode
109/// Next, we implement the (virtual) `daysDiff`, `firstDate`, `lastDate`, and
110/// `yearsDiff` methods, which incorporate the "policy" of what it means for
111/// this day-count convention to calculate day count, year fraction, and the
112/// valid range of the convention instance:
113/// @code
114/// int my_DayCountConvention::daysDiff(const bdlt::Date& beginDate,
115/// const bdlt::Date& endDate) const
116/// {
117/// return endDate - beginDate;
118/// }
119///
120/// const bdlt::Date& my_DayCountConvention::firstDate() const
121/// {
122/// return d_firstDate;
123/// }
124///
125/// const bdlt::Date& my_DayCountConvention::lastDate() const
126/// {
127/// return d_lastDate;
128/// }
129///
130/// double my_DayCountConvention::yearsDiff(const bdlt::Date& beginDate,
131/// const bdlt::Date& endDate) const
132/// {
133/// return static_cast<double>(endDate - beginDate) / 365.0;
134/// }
135/// @endcode
136/// Then, create two `bdlt::Date` variables, `d1` and `d2`, to use with the
137/// `my_DayCountConvention` object and its day-count convention methods:
138/// @code
139/// const bdlt::Date d1(2003, 10, 19);
140/// const bdlt::Date d2(2003, 12, 31);
141/// @endcode
142/// Next, we obtain a `bbldc::DateRangeDayCount` reference from an instantiated
143/// `my_DayCountConvention`:
144/// @code
145/// my_DayCountConvention myDcc;
146/// const bbldc::DateRangeDayCount& dcc = myDcc;
147/// @endcode
148/// Now, we compute the day count between the two dates:
149/// @code
150/// const int daysDiff = dcc.daysDiff(d1, d2);
151/// assert(73 == daysDiff);
152/// @endcode
153/// Finally, we compute the year fraction between the two dates:
154/// @code
155/// const double yearsDiff = dcc.yearsDiff(d1, d2);
156/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
157/// assert(0.1999 < yearsDiff && 0.2001 > yearsDiff);
158/// @endcode
159/// @}
160/** @} */
161/** @} */
162
163/** @addtogroup bbl
164 * @{
165 */
166/** @addtogroup bbldc
167 * @{
168 */
169/** @addtogroup bbldc_daterangedaycount
170 * @{
171 */
172
173#include <bblscm_version.h>
174
175#include <bdlt_date.h>
176
177
178namespace bbldc {
179
180 // =======================
181 // class DateRangeDayCount
182 // =======================
183
184/// This `class` provides a protocol for determining values based on dates
185/// according to derived implementations of specific day-count conventions.
186/// The methods `firstDate` and `lastDate` define a date range for which
187/// calculations are valid, to reflect the valid range of, say, a calendar
188/// required for the computations.
189///
190/// See @ref bbldc_daterangedaycount
192
193 public:
194 // CREATORS
195
196 /// Destroy this object.
198
199 // ACCESSORS
200
201 /// Return the (signed) number of days between the specified `beginDate`
202 /// and `endDate`. If `beginDate <= endDate`, then the result is
203 /// non-negative. The behavior is undefined unless
204 /// `firstDate() <= beginDate <= lastDate()` and
205 /// `firstDate() <= endDate <= lastDate()`. Note that reversing the
206 /// order of `beginDate` and `endDate` negates the result.
207 virtual int daysDiff(const bdlt::Date& beginDate,
208 const bdlt::Date& endDate) const = 0;
209
210 /// Return a reference providing non-modifiable access to the earliest
211 /// date in the valid range of this day-count convention.
212 virtual const bdlt::Date& firstDate() const = 0;
213
214 /// Return a reference providing non-modifiable access to the latest
215 /// date in the valid range of this day-count convention.
216 virtual const bdlt::Date& lastDate() const = 0;
217
218 /// Return the (signed fractional) number of years between the specified
219 /// `beginDate` and `endDate`. If `beginDate <= endDate`, then the
220 /// result is non-negative. The behavior is undefined unless
221 /// `firstDate() <= beginDate <= lastDate()` and
222 /// `firstDate() <= endDate <= lastDate()`. Note that reversing the
223 /// order of `beginDate` and `endDate` negates the result; specifically,
224 /// `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for all dates `b`
225 /// and `e`.
226 virtual double yearsDiff(const bdlt::Date& beginDate,
227 const bdlt::Date& endDate) const = 0;
228};
229
230} // close package namespace
231
232
233#endif
234
235// ----------------------------------------------------------------------------
236// Copyright 2015 Bloomberg Finance L.P.
237//
238// Licensed under the Apache License, Version 2.0 (the "License");
239// you may not use this file except in compliance with the License.
240// You may obtain a copy of the License at
241//
242// http://www.apache.org/licenses/LICENSE-2.0
243//
244// Unless required by applicable law or agreed to in writing, software
245// distributed under the License is distributed on an "AS IS" BASIS,
246// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
247// See the License for the specific language governing permissions and
248// limitations under the License.
249// ----------------------------- END-OF-FILE ----------------------------------
250
251/** @} */
252/** @} */
253/** @} */
Definition bbldc_daterangedaycount.h:191
virtual ~DateRangeDayCount()
Destroy this object.
virtual const bdlt::Date & lastDate() const =0
virtual double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const =0
virtual int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const =0
virtual const bdlt::Date & firstDate() const =0
Definition bdlt_date.h:294
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicactual360.h:107