Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
to_string.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilities/to_string.hpp
20 \brief string conversion utilities
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <ql/time/date.hpp>
27#include <ql/time/period.hpp>
28
29#include <set>
30#include <sstream>
31
32namespace ore {
33namespace data {
34
35//! Convert QuantLib::Date to std::string
36/*!
37 Returns date as a string in YYYY-MM-DD format, which matches QuantLib::io::iso_date()
38 However that function can have issues with locale so we have a local snprintf() based version.
39
40 If date == Date() returns 1900-01-01 so the above format is preserved.
41
42 \ingroup utilities
43*/
44std::string to_string(const QuantLib::Date& date);
45
46//! Convert bool to std::string
47/*! Returns "true" for true and "false" for false
48 \ingroup utilities
49*/
50std::string to_string(bool aBool);
51
52//! Convert QuantLib::Period to std::string
53/*!
54 Returns Period as a string as up to QuantLib 1.25, e.g. 13M is written as 1Y1M etc.
55
56 \ingroup utilities
57*/
58std::string to_string(const QuantLib::Period& period);
59
60//! Convert vector to std::string
61/*!
62 Returns a vector into a single string, with elemenst separated by Period as a string as up to QuantLib 1.25, e.g. 13M is written as 1Y1M etc.
63
64 \ingroup utilities
65*/
66template <class T> std::string to_string(const std::vector<T>& vec, const std::string& sep = ",") {
67 std::ostringstream oss;
68 for (std::size_t i = 0; i < vec.size(); ++i) {
69 oss << vec[i];
70 if (i < vec.size() - 1)
71 oss << sep;
72 }
73 return oss.str();
74}
75
76//! Convert set to std::string
77/*!
78 \ingroup utilities
79*/
80template <class T> std::string to_string(const std::set<T>& set, const std::string& sep = ",") {
81 std::ostringstream oss;
82 std::size_t count = 1;
83 for (auto s: set) {
84 oss << s;
85 if (count < set.size())
86 oss << sep;
87 count++;
88 }
89 return oss.str();
90}
91
92//! Convert type to std::string
93/*!
94 Utility to give to_string() interface to classes and enums that have ostream<< operators defined.
95 \ingroup utilities
96*/
97template <class T> std::string to_string(const T& t) {
98 std::ostringstream oss;
99 oss << t;
100 return oss.str();
101}
102} // namespace data
103} // namespace ore
@ data
Definition: log.hpp:77
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
std::size_t count