Bond
 
Loading...
Searching...
No Matches
string_stream.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include <stdint.h>
9#include <stdio.h>
10#include <string>
11#include <vector>
12
13namespace bond
14{
15
16namespace detail
17{
18
19template<uint16_t Size, typename Allocator = std::allocator<char> >
20class basic_string_stream
21{
22public:
23 basic_string_stream()
24 {
25 buffer.reserve(Size);
26 buffer.push_back('\0');
27 }
28
29 explicit basic_string_stream(const Allocator& allocator)
30 : buffer(allocator)
31 {
32 buffer.reserve(Size);
33 buffer.push_back('\0');
34 }
35
36 basic_string_stream& operator<<(const char* str)
37 {
38 while (*str)
39 {
40 write(*str++);
41 }
42
43 return *this;
44 }
45
46 template<typename T, typename A>
47 basic_string_stream& operator<<(const std::basic_string<char, T, A>& str)
48 {
49 write(str.begin(), str.end());
50 return *this;
51 }
52
53 basic_string_stream& operator<<(char value)
54 {
55 write(value);
56 return *this;
57 }
58
59 template <typename T>
60 basic_string_stream& operator<<(const T& value)
61 {
62 return *this << std::to_string(value);
63 }
64
65 std::string str() const
66 {
67 return content();
68 }
69
70 const char* content() const
71 {
72 return &buffer[0];
73 }
74
75private:
76 void write(char ch)
77 {
78 buffer.back() = ch;
79 buffer.push_back('\0');
80 }
81
82 template<typename I>
83 void write(I begin, I end)
84 {
85 for ( ; begin != end; ++begin)
86 {
87 write(*begin);
88 }
89 }
90
91 std::vector<char, Allocator> buffer;
92};
93
94typedef basic_string_stream<1024> string_stream;
95
96}
97
98}
namespace bond
Definition: apply.h:17