| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Faster than ostringstream--returns its string by ref
 | 
					
						
							| 
									
										
										
										
											2014-03-22 16:26:08 +02:00
										 |  |  | #include <ostream>
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  | #include "c11log/details/stack_buf.h"
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace c11log | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace details | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-29 13:04:42 +03:00
										 |  |  | class stack_devicebuf:public std::streambuf | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     using Base = std::streambuf; | 
					
						
							| 
									
										
										
										
											2014-03-29 13:04:42 +03:00
										 |  |  |     stack_devicebuf() = default; | 
					
						
							|  |  |  |     ~stack_devicebuf() = default; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-29 13:04:42 +03:00
										 |  |  |     stack_devicebuf(const stack_devicebuf& other) = delete; | 
					
						
							|  |  |  |     stack_devicebuf(stack_devicebuf&& other) = delete; | 
					
						
							|  |  |  |     stack_devicebuf& operator=(const stack_devicebuf&) = delete; | 
					
						
							|  |  |  |     stack_devicebuf& operator=(stack_devicebuf&&) = delete; | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     bufpair_t buf() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |         return _stackbuf.get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::size_t size() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _stackbuf.size(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |     void clear() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |         _stackbuf.clear(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2014-03-23 01:48:37 +02:00
										 |  |  |     // copy the give buffer into the accumulated fast buffer
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     std::streamsize xsputn(const char_type* s, std::streamsize count) override | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |         _stackbuf.append(s, static_cast<unsigned int>(count)); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         return count; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int_type overflow(int_type ch) override | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-23 01:48:37 +02:00
										 |  |  |         if (traits_type::not_eof(ch)) | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |             char c = traits_type::to_char_type(ch); | 
					
						
							|  |  |  |             xsputn(&c, 1); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-03-23 01:48:37 +02:00
										 |  |  |         return ch; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     stack_buf<192> _stackbuf; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  | class stack_oss:public std::ostream | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     stack_oss():std::ostream(&_dev) {} | 
					
						
							|  |  |  |     ~stack_oss() = default; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     stack_oss(const stack_oss& other) = delete; | 
					
						
							|  |  |  |     stack_oss(stack_oss&& other) = delete; | 
					
						
							|  |  |  |     stack_oss& operator=(const stack_oss& other) = delete; | 
					
						
							| 
									
										
										
										
											2014-03-22 16:26:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     bufpair_t buf() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _dev.buf(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:05:09 +03:00
										 |  |  |     std::size_t size() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _dev.size(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |     void clear() | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-03-22 16:37:48 +02:00
										 |  |  |         _dev.clear(); | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2014-03-29 13:04:42 +03:00
										 |  |  |     stack_devicebuf _dev; | 
					
						
							| 
									
										
										
										
											2014-03-22 14:11:17 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |