QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
daycounter.hpp
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
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
35
44 class DayCounter {
45 protected:
47 class Impl {
48 public:
49 virtual ~Impl() = default;
50 virtual std::string name() const = 0;
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_;
65 explicit DayCounter(ext::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
66
67 public:
72 DayCounter() = default;
74
75
76 bool empty() const;
78
82 std::string name() const;
85 const Date&) const;
87 Time yearFraction(const Date&, const Date&,
88 const Date& refPeriodStart = Date(),
89 const Date& refPeriodEnd = Date()) const;
91 };
92
93 // comparison based on name
94
99 bool operator==(const DayCounter&,
100 const DayCounter&);
101
103 bool operator!=(const DayCounter&,
104 const DayCounter&);
105
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
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:191
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:196
STL namespace.