QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
daycounter.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file daycounter.hpp
22 \brief day counter class
23*/
24
25#ifndef quantlib_day_counter_hpp
26#define quantlib_day_counter_hpp
27
28#include <ql/errors.hpp>
29#include <ql/time/date.hpp>
30#include <utility>
31
32namespace QuantLib {
33
34 //! day counter class
35 /*! This class provides methods for determining the length of a time
36 period according to given market convention, both as a number
37 of days and as a year fraction.
38
39 The Bridge pattern is used to provide the base behavior of the
40 day counter.
41
42 \ingroup datetime
43 */
44 class DayCounter {
45 protected:
46 //! abstract base class for day counter implementations
47 class Impl {
48 public:
49 virtual ~Impl() = default;
50 virtual std::string name() const = 0;
51 //! to be overloaded by more complex day counters
52 virtual Date::serial_type dayCount(const Date& d1,
53 const Date& d2) const {
54 return (d2-d1);
55 }
56 virtual Time yearFraction(const Date& d1,
57 const Date& d2,
58 const Date& refPeriodStart,
59 const Date& refPeriodEnd) const = 0;
60 };
61 ext::shared_ptr<Impl> impl_;
62 /*! This constructor can be invoked by derived classes which
63 define a given implementation.
64 */
65 explicit DayCounter(ext::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
66
67 public:
68 /*! The default constructor returns a day counter with a null
69 implementation, which is therefore unusable except as a
70 placeholder.
71 */
72 DayCounter() = default;
73 //! \name DayCounter interface
74 //@{
75 //! Returns whether or not the day counter is initialized
76 bool empty() const;
77 //! Returns the name of the day counter.
78 /*! \warning This method is used for output and comparison between
79 day counters. It is <b>not</b> meant to be used for writing
80 switch-on-type code.
81 */
82 std::string name() const;
83 //! Returns the number of days between two dates.
85 const Date&) const;
86 //! Returns the period between two dates as a fraction of year.
87 Time yearFraction(const Date&, const Date&,
88 const Date& refPeriodStart = Date(),
89 const Date& refPeriodEnd = Date()) const;
90 //@}
91 };
92
93 // comparison based on name
94
95 /*! Returns <tt>true</tt> iff the two day counters belong to the same
96 derived class.
97 \relates DayCounter
98 */
99 bool operator==(const DayCounter&,
100 const DayCounter&);
101
102 /*! \relates DayCounter */
103 bool operator!=(const DayCounter&,
104 const DayCounter&);
105
106 /*! \relates DayCounter */
107 std::ostream& operator<<(std::ostream&,
108 const DayCounter&);
109
110
111 // inline definitions
112
113 inline bool DayCounter::empty() const {
114 return !impl_;
115 }
116
117 inline std::string DayCounter::name() const {
118 QL_REQUIRE(impl_, "no day counter implementation provided");
119 return impl_->name();
120 }
121
123 const Date& d2) const {
124 QL_REQUIRE(impl_, "no day counter implementation provided");
125 return impl_->dayCount(d1,d2);
126 }
127
128 inline Time DayCounter::yearFraction(const Date& d1, const Date& d2,
129 const Date& refPeriodStart, const Date& refPeriodEnd) const {
130 QL_REQUIRE(impl_, "no day counter implementation provided");
131 return impl_->yearFraction(d1,d2,refPeriodStart,refPeriodEnd);
132 }
133
134
135 inline bool operator==(const DayCounter& d1, const DayCounter& d2) {
136 return (d1.empty() && d2.empty())
137 || (!d1.empty() && !d2.empty() && d1.name() == d2.name());
138 }
139
140 inline bool operator!=(const DayCounter& d1, const DayCounter& d2) {
141 return !(d1 == d2);
142 }
143
144 inline std::ostream& operator<<(std::ostream& out, const DayCounter &d) {
145 return out << d.name();
146 }
147
148}
149
150#endif
Concrete date class.
Definition: date.hpp:125
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
abstract base class for day counter implementations
Definition: daycounter.hpp:47
virtual ~Impl()=default
virtual Date::serial_type dayCount(const Date &d1, const Date &d2) const
to be overloaded by more complex day counters
Definition: daycounter.hpp:52
virtual std::string name() const =0
virtual Time yearFraction(const Date &d1, const Date &d2, const Date &refPeriodStart, const Date &refPeriodEnd) const =0
day counter class
Definition: daycounter.hpp:44
Date::serial_type dayCount(const Date &, const Date &) const
Returns the number of days between two dates.
Definition: daycounter.hpp:122
std::string name() const
Returns the name of the day counter.
Definition: daycounter.hpp:117
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
bool empty() const
Returns whether or not the day counter is initialized.
Definition: daycounter.hpp:113
ext::shared_ptr< Impl > impl_
Definition: daycounter.hpp:61
DayCounter(ext::shared_ptr< Impl > impl)
Definition: daycounter.hpp:65
date- and time-related classes, typedefs and enumerations
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:231
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:236
STL namespace.