drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
OStringStream.h
1
14
15#pragma once
16#include <string>
17#include <sstream>
18#include <string_view>
19
20namespace drogon
21{
22namespace internal
23{
24template <typename T, typename = void>
25struct CanConvertToString : std::false_type
26{
27};
28
29template <typename T>
31 T,
32 std::void_t<decltype(std::to_string(std::declval<T>()))>> : std::true_type
33{
34};
35} // namespace internal
36
37class OStringStream
38{
39 public:
40 OStringStream() = default;
41
42 void reserve(size_t size)
43 {
44 buffer_.reserve(size);
45 }
46
47 template <typename T>
48 OStringStream &operator<<(T &&value)
49 {
51 {
52 buffer_.append(std::to_string(std::forward<T>(value)));
53 return *this;
54 }
55 else
56 {
57 std::stringstream ss;
58 ss << std::forward<T>(value);
59 buffer_.append(ss.str());
60 return *this;
61 }
62 }
63
64 template <int N>
65 OStringStream &operator<<(const char (&buf)[N])
66 {
67 buffer_.append(buf, N - 1);
68 return *this;
69 }
70
71 OStringStream &operator<<(const std::string_view &str)
72 {
73 buffer_.append(str.data(), str.length());
74 return *this;
75 }
76
77 OStringStream &operator<<(std::string_view &&str)
78 {
79 buffer_.append(str.data(), str.length());
80 return *this;
81 }
82
83 OStringStream &operator<<(const std::string &str)
84 {
85 buffer_.append(str);
86 return *this;
87 }
88
89 OStringStream &operator<<(std::string &&str)
90 {
91 buffer_.append(std::move(str));
92 return *this;
93 }
94
95 OStringStream &operator<<(const double &d)
96 {
97 std::stringstream ss;
98 ss << d;
99 buffer_.append(ss.str());
100 return *this;
101 }
102
103 OStringStream &operator<<(const float &f)
104 {
105 std::stringstream ss;
106 ss << f;
107 buffer_.append(ss.str());
108 return *this;
109 }
110
111 OStringStream &operator<<(double &&d)
112 {
113 std::stringstream ss;
114 ss << d;
115 buffer_.append(ss.str());
116 return *this;
117 }
118
119 OStringStream &operator<<(float &&f)
120 {
121 std::stringstream ss;
122 ss << f;
123 buffer_.append(ss.str());
124 return *this;
125 }
126
127 std::string &str()
128 {
129 return buffer_;
130 }
131
132 const std::string &str() const
133 {
134 return buffer_;
135 }
136
137 private:
138 std::string buffer_;
139};
140} // namespace drogon
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
STL namespace.
Definition OStringStream.h:26