BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bbldc_perioddaterangedaycountadapter.h
Go to the documentation of this file.
1/// @file bbldc_perioddaterangedaycountadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_perioddaterangedaycountadapter.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_PERIODDATERANGEDAYCOUNTADAPTER
9#define INCLUDED_BBLDC_PERIODDATERANGEDAYCOUNTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_perioddaterangedaycountadapter bbldc_perioddaterangedaycountadapter
15/// @brief Provide a parameterized day-count convention implementation.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_perioddaterangedaycountadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_perioddaterangedaycountadapter-purpose"> Purpose</a>
25/// * <a href="#bbldc_perioddaterangedaycountadapter-classes"> Classes </a>
26/// * <a href="#bbldc_perioddaterangedaycountadapter-description"> Description </a>
27/// * <a href="#bbldc_perioddaterangedaycountadapter-usage"> Usage </a>
28/// * <a href="#bbldc_perioddaterangedaycountadapter-example-1-adapting-bbldc-periodicmaactualactual"> Example 1: Adapting bbldc::PeriodIcmaActualActual </a>
29///
30/// # Purpose {#bbldc_perioddaterangedaycountadapter-purpose}
31/// Provide a parameterized day-count convention implementation.
32///
33/// # Classes {#bbldc_perioddaterangedaycountadapter-classes}
34///
35/// - bbldc::PeriodDateRangeDayCountAdapter: `bbldc::DateRangeDayCount` adapter
36///
37/// # Description {#bbldc_perioddaterangedaycountadapter-description}
38/// This component provides a parameterized (template)
39/// implementation, `bbldc::PeriodDateRangeDayCountAdapter`, 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,
46/// const bdlt::Date& endDate,
47/// const bsl::vector<bdlt::Date>& periodDate,
48/// double periodYearDiff);
49/// @endcode
50/// The template class `bbldc::PeriodDateRangeDayCountAdapter` provides
51/// convenient support for run-time polymorphic choice of day-count conventions
52/// (via conventional use of a base-class pointer or reference) without having
53/// to implement each derived type explicitly. In this sense,
54/// `bbldc::PeriodDateRangeDayCountAdapter` adapts the various concrete
55/// period-based day-count convention classes (e.g.,
56/// `bbldc::PeriodIcmaActualActual`) to a run-time binding mechanism.
57///
58/// The `bbldc::DateRangeDayCount` protocol requires two methods, `firstDate`
59/// and `lastDate`, that define a date range for which calculations are valid,
60/// to reflect the valid range of, say, a calendar required for the
61/// computations. For "period" day-count implementations, the valid date range
62/// is from the first to the last period date.
63///
64/// ## Usage {#bbldc_perioddaterangedaycountadapter-usage}
65///
66///
67/// This section illustrates intended use of this component.
68///
69/// ### Example 1: Adapting bbldc::PeriodIcmaActualActual {#bbldc_perioddaterangedaycountadapter-example-1-adapting-bbldc-periodicmaactualactual}
70///
71///
72/// This example shows the procedure for using
73/// `bbldc::PeriodDateRangeDayCountAdapter` to adapt the
74/// `bbldc::PeriodIcmaActualActual` day-count convention to the
75/// `bbldc::DateRangeDayCount` protocol, and then the use of the day-count
76/// methods. First, we create a schedule of period dates, `sched`,
77/// corresponding to a quarterly payment (`periodYearDiff == 0.25`):
78/// @code
79/// bsl::vector<bdlt::Date> sched;
80/// sched.push_back(bdlt::Date(2003, 10, 1));
81/// sched.push_back(bdlt::Date(2004, 1, 1));
82/// @endcode
83/// Then, we define an instance of the adapted day-count convention and obtain a
84/// reference to the `bbldc::DateRangeDayCount`:
85/// @code
86/// const bbldc::PeriodDateRangeDayCountAdapter<bbldc::PeriodIcmaActualActual>
87/// myDcc(sched,
88/// 0.25);
89/// const bbldc::DateRangeDayCount& dcc = myDcc;
90/// @endcode
91/// Next, create two `bdlt::Date` variables, `d1` and `d2`, with which to use
92/// the day-count convention methods:
93/// @code
94/// const bdlt::Date d1(2003, 10, 19);
95/// const bdlt::Date d2(2003, 12, 31);
96/// @endcode
97/// Now, use the base-class reference to compute the day count between the two
98/// dates:
99/// @code
100/// const int daysDiff = dcc.daysDiff(d1, d2);
101/// assert(73 == daysDiff);
102/// @endcode
103/// Finally, use the base-class reference to compute the year fraction between
104/// the two dates:
105/// @code
106/// const double yearsDiff = dcc.yearsDiff(d1, d2);
107/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
108/// assert(yearsDiff > 0.1983 && yearsDiff < 0.1985);
109/// @endcode
110/// @}
111/** @} */
112/** @} */
113
114/** @addtogroup bbl
115 * @{
116 */
117/** @addtogroup bbldc
118 * @{
119 */
120/** @addtogroup bbldc_perioddaterangedaycountadapter
121 * @{
122 */
123
124#include <bblscm_version.h>
125
127
128#include <bdlt_date.h>
129
130#include <bslma_allocator.h>
132
134
135#include <bsls_assert.h>
136#include <bsls_keyword.h>
137#include <bsls_libraryfeatures.h>
138#include <bsls_review.h>
139
140#include <bsl_iterator.h>
141#include <bsl_vector.h>
142
143#include <vector>
144
145
146namespace bbldc {
147
148 // ====================================
149 // class PeriodDateRangeDayCountAdapter
150 // ====================================
151
152/// This `class` provides an "adapter" from the specified `CONVENTION`, that
153/// requires a set of periods to compute the year fraction, to the
154/// `bbldc::DateRangeDayCount` protocol that can be used for determining
155/// values based on dates according to the day-count `CONVENTION`.
156///
157/// See @ref bbldc_perioddaterangedaycountadapter
158template <class CONVENTION>
160
161 // DATA
162 bsl::vector<bdlt::Date> d_periodDate; // period starting dates
163
164 double d_periodYearDiff; // years for each period
165
166 private:
167 // PRIVATE ACCESSORS
168
169 /// Return `true` if all values contained in the specified `periodDate`
170 /// are unique and sorted from minimum to maximum value, and `false`
171 /// otherwise.
172 bool isSortedAndUnique(const bsl::vector<bdlt::Date>& periodDate) const;
173
174 private:
175 // NOT IMPLEMENTED
179
180 public:
181 // CREATORS
183 const bsl::vector<bdlt::Date>& periodDate,
184 double periodYearDiff,
185 bslma::Allocator *basicAllocator = 0);
187 const std::vector<bdlt::Date>& periodDate,
188 double periodYearDiff,
189 bslma::Allocator *basicAllocator = 0);
190#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
192 const std::pmr::vector<bdlt::Date>& periodDate,
193 double periodYearDiff,
194 bslma::Allocator *basicAllocator = 0);
195#endif
196 // Create a day-count adapter that uses the specified 'periodDate' and
197 // 'periodYearDiff' during invocations of 'yearsDiff'. 'periodDate'
198 // provides the period starting dates and 'periodYearDiff' defines the
199 // duration of each period (e.g., 0.25 for quarterly periods).
200 // Optionally specify a 'basicAllocator' used to supply memory. If
201 // 'basicAllocator' is 0, the currently installed default allocator is
202 // used. The behavior is undefined unless 'periodDate.size() >= 2' and
203 // the values contained in 'periodDate' are unique and sorted from
204 // minimum to maximum.
205
206 /// Destroy this object.
208
209 // ACCESSORS
210 int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate) const
212 // Return the (signed) number of days between the specified 'beginDate'
213 // and 'endDate' as per the 'CONVENTION' template policy. If
214 // 'beginDate <= endDate', then the result is non-negative. Note that
215 // reversing the order of 'beginDate' and 'endDate' negates the result.
216
217 /// Return a reference providing non-modifiable access to
218 /// `periodDate.front()` for the `periodDate` provided at construction.
219 ///
220 /// \note Note that this value is the earliest date in the valid range of this
221 /// day-count convention adaptation.
223
224 /// Return a reference providing non-modifiable access to
225 /// `periodDate.back()` for the `periodDate` provided at construction.
226 ///
227 /// \note Note that this value is the latest date in the valid range of this
228 /// day-count convention adaptation.
230
231 /// Return the (signed fractional) number of years between the specified
232 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
233 /// If `beginDate <= endDate`, then the result is non-negative.
234 ///
235 /// \pre The behavior is undefined unless, for the `periodDate` provided at
236 /// construction, `periodDate.front() <= beginDate <= periodDate.back()`
237 /// and `periodDate.front() <= endDate <= periodDate.back()`.
238 ///
239 /// \note Note that reversing the order of `beginDate` and `endDate` negates the result;
240 /// specifically, `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for
241 /// all dates `b` and `e`.
242 double yearsDiff(const bdlt::Date& beginDate,
243 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
244
245 // Aspects
246
247 /// Return the allocator used by this adapter to supply memory.
249};
250
251// ============================================================================
252// INLINE DEFINITIONS
253// ============================================================================
254
255 // ------------------------------------
256 // class PeriodDateRangeDayCountAdapter
257 // ------------------------------------
258
259// PRIVATE ACCESSORS
260template <class CONVENTION>
262 const bsl::vector<bdlt::Date>& periodDate) const
263{
266
267 if (begin == end) {
268 return true; // RETURN
269 }
270
273
274 while (at != end) {
275 if (*prev >= *at) {
276 return false; // RETURN
277 }
278 prev = at++;
279 }
280
281 return true;
282}
283
284// CREATORS
285template <class CONVENTION>
286inline
288 const bsl::vector<bdlt::Date>& periodDate,
289 double periodYearDiff,
290 bslma::Allocator *basicAllocator)
291: d_periodDate(periodDate, basicAllocator)
292, d_periodYearDiff(periodYearDiff)
293{
294 BSLS_ASSERT(d_periodDate.size() >= 2);
295
296 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
297}
298
299template <class CONVENTION>
300inline
302 const std::vector<bdlt::Date>& periodDate,
303 double periodYearDiff,
304 bslma::Allocator *basicAllocator)
305: d_periodDate(periodDate.begin(), periodDate.end(), basicAllocator)
306, d_periodYearDiff(periodYearDiff)
307{
308 BSLS_ASSERT(d_periodDate.size() >= 2);
309
310 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
311}
312
313#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
314template <class CONVENTION>
315inline
317 const std::pmr::vector<bdlt::Date>& periodDate,
318 double periodYearDiff,
319 bslma::Allocator *basicAllocator)
320: d_periodDate(periodDate.begin(), periodDate.end(), basicAllocator)
321, d_periodYearDiff(periodYearDiff)
322{
323 BSLS_ASSERT(d_periodDate.size() >= 2);
324
325 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
326}
327#endif
328
329template <class CONVENTION>
330inline
334
335// ACCESSORS
336template <class CONVENTION>
337inline
339 const bdlt::Date& beginDate,
340 const bdlt::Date& endDate) const
341{
342 return CONVENTION::daysDiff(beginDate, endDate);
343}
344
345template <class CONVENTION>
346inline
348{
349 return d_periodDate.front();
350}
351
352template <class CONVENTION>
353inline
355{
356 return d_periodDate.back();
357}
358
359template <class CONVENTION>
360inline
362 const bdlt::Date& beginDate,
363 const bdlt::Date& endDate) const
364{
365 BSLS_ASSERT(d_periodDate.front() <= beginDate);
366 BSLS_ASSERT( beginDate <= d_periodDate.back());
367 BSLS_ASSERT(d_periodDate.front() <= endDate);
368 BSLS_ASSERT( endDate <= d_periodDate.back());
369
370 return CONVENTION::yearsDiff(beginDate,
371 endDate,
372 d_periodDate,
373 d_periodYearDiff);
374}
375
376 // Aspects
377
378template <class CONVENTION>
379inline
381{
382 return d_periodDate.get_allocator().mechanism();
383}
384
385} // close package namespace
386
387
388// TRAITS
389
390
391namespace bslma {
392
393template <class CONVENTION>
394struct UsesBslmaAllocator<bbldc::PeriodDateRangeDayCountAdapter<CONVENTION> >
395 : bsl::true_type {};
396
397} // close namespace bslma
398
399
400#endif
401
402// ----------------------------------------------------------------------------
403// Copyright 2015 Bloomberg Finance L.P.
404//
405// Licensed under the Apache License, Version 2.0 (the "License");
406// you may not use this file except in compliance with the License.
407// You may obtain a copy of the License at
408//
409// http://www.apache.org/licenses/LICENSE-2.0
410//
411// Unless required by applicable law or agreed to in writing, software
412// distributed under the License is distributed on an "AS IS" BASIS,
413// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
414// See the License for the specific language governing permissions and
415// limitations under the License.
416// ----------------------------- END-OF-FILE ----------------------------------
417
418/** @} */
419/** @} */
420/** @} */
Definition bbldc_daterangedaycount.h:191
Definition bbldc_perioddaterangedaycountadapter.h:159
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:347
~PeriodDateRangeDayCountAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition bbldc_perioddaterangedaycountadapter.h:331
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:361
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:354
bslma::Allocator * allocator() const
Return the allocator used by this adapter to supply memory.
Definition bbldc_perioddaterangedaycountadapter.h:380
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:338
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:3019
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2866
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2874
Definition bslstl_vector.h:1120
VALUE_TYPE const * const_iterator
Definition bslstl_vector.h:1153
Definition bslma_allocator.h:545
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#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