QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
dataformatters.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) 2005, 2009 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file dataformatters.hpp
21 \brief output manipulators
22*/
23
24#ifndef quantlib_data_formatters_hpp
25#define quantlib_data_formatters_hpp
26
27#include <ql/utilities/null.hpp>
28#include <ostream>
29
30namespace QuantLib {
31
32 namespace detail {
33
34 template <typename T> struct null_checker {
35 explicit null_checker(T value) : value(value) {}
37 };
38 template <typename T>
39 std::ostream& operator<<(std::ostream&, const null_checker<T>&);
40
42 explicit ordinal_holder(Size n) : n(n) {}
44 };
45 std::ostream& operator<<(std::ostream&, const ordinal_holder&);
46
47 template <typename T> struct power_of_two_holder {
48 explicit power_of_two_holder(T n) : n(n) {}
50 };
51 template <typename T>
52 std::ostream& operator<<(std::ostream&,
54
58 };
59 std::ostream& operator<<(std::ostream&, const percent_holder&);
60
61 template <typename InputIterator> struct sequence_holder {
62 sequence_holder(InputIterator begin, InputIterator end)
63 : begin(begin), end(end) {}
64 InputIterator begin, end;
65 };
66 template <typename I>
67 std::ostream& operator<<(std::ostream&, const sequence_holder<I>&);
68
69 }
70
71
72 namespace io {
73
74 /*! \defgroup manips Output manipulators
75
76 Helper functions for creating formatted output.
77
78 @{
79 */
80
81 //! check for nulls before output
82 template <typename T>
84
85 //! outputs naturals as 1st, 2nd, 3rd...
87
88 //! output integers as powers of two
89 template <typename T>
91
92 //! output reals as percentages
94
95 //! output rates and spreads as percentages
97
98 //! output volatilities as percentages
100
101 //! output STL-compliant containers as space-separated sequences
102 template <class Container>
104 sequence(const Container& c);
105
106 /*! @} */
107
108
109 // inline definitions
110
111 template <typename T>
113 return detail::null_checker<T>(x);
114 }
115
118 }
119
120 template <typename T>
123 }
124
126 return detail::percent_holder(x);
127 }
128
131 }
132
135 }
136
137 template <class Container>
139 sequence(const Container& c) {
141 c.begin(), c.end());
142 }
143
144 }
145
146 namespace detail {
147
148 template <typename T>
149 inline std::ostream& operator<<(std::ostream& out,
150 const null_checker<T>& checker) {
151 if (checker.value == Null<T>())
152 return out << "null";
153 else
154 return out << checker.value;
155 }
156
157 template <typename T>
158 inline std::ostream& operator<<(std::ostream& out,
159 const power_of_two_holder<T>& holder) {
160 if (holder.n == Null<T>())
161 return out << "null";
162
163 T n = holder.n;
164 Integer power = 0;
165 if (n != 0) {
166 while (!(n & 1UL)) {
167 power++;
168 n >>= 1;
169 }
170 }
171 return out << n << "*2^" << power;
172 }
173
174 template <typename I>
175 inline std::ostream& operator<<(std::ostream& out,
176 const sequence_holder<I>& holder) {
177 out << "( ";
178 for (I i = holder.begin; i != holder.end; ++i)
179 out << *i << " ";
180 out << ")";
181 return out;
182 }
183
184 }
185
186}
187
188
189#endif
template class providing a null value for a given type.
Definition: null.hpp:76
detail::sequence_holder< typename Container::const_iterator > sequence(const Container &c)
output STL-compliant containers as space-separated sequences
detail::percent_holder volatility(Volatility)
output volatilities as percentages
detail::percent_holder rate(Rate)
output rates and spreads as percentages
detail::null_checker< T > checknull(T)
check for nulls before output
detail::power_of_two_holder< T > power_of_two(T)
output integers as powers of two
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
detail::percent_holder percent(Real)
output reals as percentages
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
std::ostream & operator<<(std::ostream &out, const short_date_holder &holder)
Definition: date.cpp:894
Definition: any.hpp:35
null values
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v
sequence_holder(InputIterator begin, InputIterator end)