BDE 4.14.0 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 /// Note that this value is the earliest date in the valid range of this
220 /// day-count convention adaptation.
222
223 /// Return a reference providing non-modifiable access to
224 /// `periodDate.back()` for the `periodDate` provided at construction.
225 /// Note that this value is the latest date in the valid range of this
226 /// day-count convention adaptation.
228
229 /// Return the (signed fractional) number of years between the specified
230 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
231 /// If `beginDate <= endDate`, then the result is non-negative. The
232 /// behavior is undefined unless, for the `periodDate` provided at
233 /// construction, `periodDate.front() <= beginDate <= periodDate.back()`
234 /// and `periodDate.front() <= endDate <= periodDate.back()`. Note that
235 /// reversing the order of `beginDate` and `endDate` negates the result;
236 /// specifically, `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for
237 /// all dates `b` and `e`.
238 double yearsDiff(const bdlt::Date& beginDate,
239 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
240
241 // Aspects
242
243 /// Return the allocator used by this adapter to supply memory.
245};
246
247// ============================================================================
248// INLINE DEFINITIONS
249// ============================================================================
250
251 // ------------------------------------
252 // class PeriodDateRangeDayCountAdapter
253 // ------------------------------------
254
255// PRIVATE ACCESSORS
256template <class CONVENTION>
258 const bsl::vector<bdlt::Date>& periodDate) const
259{
262
263 if (begin == end) {
264 return true; // RETURN
265 }
266
269
270 while (at != end) {
271 if (*prev >= *at) {
272 return false; // RETURN
273 }
274 prev = at++;
275 }
276
277 return true;
278}
279
280// CREATORS
281template <class CONVENTION>
282inline
284 const bsl::vector<bdlt::Date>& periodDate,
285 double periodYearDiff,
286 bslma::Allocator *basicAllocator)
287: d_periodDate(periodDate, basicAllocator)
288, d_periodYearDiff(periodYearDiff)
289{
290 BSLS_ASSERT(d_periodDate.size() >= 2);
291
292 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
293}
294
295template <class CONVENTION>
296inline
298 const std::vector<bdlt::Date>& periodDate,
299 double periodYearDiff,
300 bslma::Allocator *basicAllocator)
301: d_periodDate(periodDate.begin(), periodDate.end(), basicAllocator)
302, d_periodYearDiff(periodYearDiff)
303{
304 BSLS_ASSERT(d_periodDate.size() >= 2);
305
306 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
307}
308
309#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
310template <class CONVENTION>
311inline
313 const std::pmr::vector<bdlt::Date>& periodDate,
314 double periodYearDiff,
315 bslma::Allocator *basicAllocator)
316: d_periodDate(periodDate.begin(), periodDate.end(), basicAllocator)
317, d_periodYearDiff(periodYearDiff)
318{
319 BSLS_ASSERT(d_periodDate.size() >= 2);
320
321 BSLS_ASSERT_SAFE(isSortedAndUnique(d_periodDate));
322}
323#endif
324
325template <class CONVENTION>
326inline
330
331// ACCESSORS
332template <class CONVENTION>
333inline
335 const bdlt::Date& beginDate,
336 const bdlt::Date& endDate) const
337{
338 return CONVENTION::daysDiff(beginDate, endDate);
339}
340
341template <class CONVENTION>
342inline
344{
345 return d_periodDate.front();
346}
347
348template <class CONVENTION>
349inline
351{
352 return d_periodDate.back();
353}
354
355template <class CONVENTION>
356inline
358 const bdlt::Date& beginDate,
359 const bdlt::Date& endDate) const
360{
361 BSLS_ASSERT(d_periodDate.front() <= beginDate);
362 BSLS_ASSERT( beginDate <= d_periodDate.back());
363 BSLS_ASSERT(d_periodDate.front() <= endDate);
364 BSLS_ASSERT( endDate <= d_periodDate.back());
365
366 return CONVENTION::yearsDiff(beginDate,
367 endDate,
368 d_periodDate,
369 d_periodYearDiff);
370}
371
372 // Aspects
373
374template <class CONVENTION>
375inline
377{
378 return d_periodDate.get_allocator().mechanism();
379}
380
381} // close package namespace
382
383
384// TRAITS
385
386
387namespace bslma {
388
389template <class CONVENTION>
390struct UsesBslmaAllocator<bbldc::PeriodDateRangeDayCountAdapter<CONVENTION> >
391 : bsl::true_type {};
392
393} // close namespace bslma
394
395
396#endif
397
398// ----------------------------------------------------------------------------
399// Copyright 2015 Bloomberg Finance L.P.
400//
401// Licensed under the Apache License, Version 2.0 (the "License");
402// you may not use this file except in compliance with the License.
403// You may obtain a copy of the License at
404//
405// http://www.apache.org/licenses/LICENSE-2.0
406//
407// Unless required by applicable law or agreed to in writing, software
408// distributed under the License is distributed on an "AS IS" BASIS,
409// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
410// See the License for the specific language governing permissions and
411// limitations under the License.
412// ----------------------------- END-OF-FILE ----------------------------------
413
414/** @} */
415/** @} */
416/** @} */
Definition bbldc_daterangedaycount.h:191
Definition bbldc_perioddaterangedaycountadapter.h:159
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:343
~PeriodDateRangeDayCountAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition bbldc_perioddaterangedaycountadapter.h:327
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:357
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:350
bslma::Allocator * allocator() const
Return the allocator used by this adapter to supply memory.
Definition bbldc_perioddaterangedaycountadapter.h:376
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_perioddaterangedaycountadapter.h:334
Definition bdlt_date.h:294
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2511
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2519
Definition bslstl_vector.h:1025
VALUE_TYPE const * const_iterator
Definition bslstl_vector.h:1058
Definition bslma_allocator.h:457
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#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