mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
io_string.h
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef MOD_SERVLET_IO_STRING_H
9 #define MOD_SERVLET_IO_STRING_H
10 
11 #include <servlet/lib/io.h>
12 
13 namespace servlet
14 {
15 
25 template<typename CharT, typename Traits = std::char_traits<CharT>>
27 {
28 public:
32  typedef std::basic_string<CharT, Traits> string_type;
36  typedef std::experimental::basic_string_view<CharT, Traits> string_view_type;
37 
43  string_sink(std::size_t max_size = std::string::npos) : _max_size{max_size} {}
44 
52  std::streamsize write(const CharT* s, std::streamsize n)
53  {
54  if (_max_size <= _chars_written) return 0;
55  if (_chars_written + n > _max_size)
56  {
57  std::streamsize ln = _max_size - _chars_written;
58  _buffer.append(s, ln);
59  _chars_written = _max_size;
60  return ln;
61  }
62  _buffer.append(s, n);
63  _chars_written += n;
64  return n;
65  }
69  void flush() { _flushed = true; }
70 
74  void reset() { _buffer.clear(); _flushed = false; _chars_written = 0; }
79  string_view_type view() const { return string_view_type{_buffer}; }
84  string_type& str() { return _buffer; }
89  bool was_flushed() const { return _flushed; }
95  std::size_t characters_written() const { return _chars_written; }
96 
97 private:
98  string_type _buffer;
99  std::size_t _max_size;
100  std::size_t _chars_written = 0;
101  bool _flushed = false;
102 };
103 
107 template<typename CharT, typename Traits = std::char_traits<CharT>>
108 using basic_inplace_ostream = basic_outstream<string_sink<CharT, Traits>, non_buffered, CharT, Traits>;
112 typedef basic_inplace_ostream<char> inplace_ostream;
113 
114 } // end of servlet namespace
115 
116 #endif //MOD_SERVLET_IO_STRING_H
string_type & str()
Returns the reference to the destination std::string
Definition: io_string.h:84
std::streamsize write(const CharT *s, std::streamsize n)
Writes first n character from the array s into destination string
Definition: io_string.h:52
std::basic_string< CharT, Traits > string_type
Type of the underlying string.
Definition: io_string.h:32
string_view_type view() const
Returns string_view to the destination std::string
Definition: io_string.h:79
bool was_flushed() const
Return flag which indicates whether this sink was flushed since last reset.
Definition: io_string.h:89
Definitions for custom implementation of std::istream and std::ostream objects.
string_sink(std::size_t max_size=std::string::npos)
Constructs object with optional argument to specify maximum size of the destination string_type...
Definition: io_string.h:43
Implementation of sink to std::string Besides to having similar functionality to std::basic_ostringst...
Definition: io_string.h:26
std::experimental::basic_string_view< CharT, Traits > string_view_type
string_view for string_type
Definition: io_string.h:36
void reset()
Resets the sink.
Definition: io_string.h:74
std::size_t characters_written() const
Return the number of character which were written into this sink since last reset.
Definition: io_string.h:95
void flush()
Sets flushed flag to true
Definition: io_string.h:69