| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:05:21 +03:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | #include <array>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:05:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 18:00:10 +03:00
										 |  |  | // Fast memory storage on the stack when possible or in std::vector
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | namespace c11log | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace details | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:03:33 +03:00
										 |  |  | template<unsigned short STACK_SIZE> | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | class stack_buf | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     using bufpair_t = std::pair<const char*, std::size_t>; | 
					
						
							|  |  |  |     using iterator = char const*; | 
					
						
							|  |  |  |     static constexpr unsigned short stack_size = STACK_SIZE; | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |     stack_buf() :_v(), _stack_size(0) {} | 
					
						
							|  |  |  |     ~stack_buf() = default; | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     stack_buf& operator=(const stack_buf& other) = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     stack_buf(const stack_buf& other):stack_buf(other, delegate_copy_move {}) | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |     {} | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     stack_buf(stack_buf&& other):stack_buf(other, delegate_copy_move {}) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |         other.clear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void append(const char* buf, std::size_t buf_size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         //If we are aleady using _v, forget about the stack
 | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |         if (vector_used()) | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |         { | 
					
						
							|  |  |  |             _v.insert(_v.end(), buf, buf + buf_size); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //Try use the stack
 | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (_stack_size + buf_size <= STACK_SIZE) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2014-05-11 02:59:05 +03:00
										 |  |  |                 std::memcpy(&_stack_array[_stack_size], buf, buf_size); | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |                 _stack_size += buf_size; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             //Not enough stack space. Copy all to _v
 | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |                 _v.reserve(_stack_size + buf_size); | 
					
						
							|  |  |  |                 _v.insert(_v.end(), _stack_array.begin(), _stack_array.begin() + _stack_size); | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |                 _v.insert(_v.end(), buf, buf + buf_size); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |     void clear() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _stack_size = 0; | 
					
						
							|  |  |  |         _v.clear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bufpair_t get() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |         if (vector_used()) | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |             return bufpair_t(_v.data(), _v.size()); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             return bufpair_t(_stack_array.data(), _stack_size); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     iterator begin() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return get().first; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     iterator end() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         bufpair_t bpair = get(); | 
					
						
							|  |  |  |         return bpair.first + bpair.second; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |     std::size_t size() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |         return get().second; | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |     struct delegate_copy_move {}; | 
					
						
							|  |  |  |     template<class T1> | 
					
						
							|  |  |  |     stack_buf(T1&& other, delegate_copy_move) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         _stack_size = other._stack_size; | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |         if (other.vector_used()) | 
					
						
							| 
									
										
										
										
											2014-05-11 02:59:05 +03:00
										 |  |  |             _v = std::forward<T1>(other)._v; | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2014-05-11 03:07:21 +03:00
										 |  |  |             std::copy_n(other._stack_array.begin(), other._stack_size, _stack_array.begin()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inline bool vector_used() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return !(_v.empty()); | 
					
						
							| 
									
										
										
										
											2014-05-11 02:56:27 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 16:33:55 +03:00
										 |  |  |     std::vector<char> _v; | 
					
						
							|  |  |  |     std::array<char, STACK_SIZE> _stack_array; | 
					
						
							|  |  |  |     std::size_t _stack_size; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-09 18:00:10 +03:00
										 |  |  | } //namespace c11log { namespace details {
 |