| 23 | namespace sonic_json { |
| 24 | |
| 25 | class WriteBuffer { |
| 26 | public: |
| 27 | WriteBuffer() : stack_() {} |
| 28 | WriteBuffer(size_t cap) : stack_(cap) {} |
| 29 | WriteBuffer(const WriteBuffer&) = delete; |
| 30 | WriteBuffer(WriteBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} |
| 31 | ~WriteBuffer() = default; |
| 32 | WriteBuffer& operator=(const WriteBuffer&) = delete; |
| 33 | WriteBuffer& operator=(WriteBuffer&& rhs) { |
| 34 | stack_ = std::move(rhs.stack_); |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @brief Return the context in the buffer. |
| 40 | * @return a null-terminate string. |
| 41 | * @note a '\0' will be added in the ending, so, this function is not |
| 42 | * thread-safe. |
| 43 | */ |
| 44 | sonic_force_inline const char* ToString() const { |
| 45 | stack_.Grow(1); |
| 46 | *(stack_.template End<char>()) = '\0'; |
| 47 | return stack_.Begin<char>(); |
| 48 | } |
| 49 | |
| 50 | sonic_force_inline size_t Size() const { return stack_.Size(); } |
| 51 | sonic_force_inline size_t Capacity() const { return stack_.Capacity(); } |
| 52 | sonic_force_inline bool Empty() const { return stack_.Empty(); } |
| 53 | |
| 54 | /** |
| 55 | * @brief Increase the capacity of buffer if new_cap is greater than the |
| 56 | * current capacity(). Otherwise, do nothing. |
| 57 | */ |
| 58 | sonic_force_inline void Reserve(size_t new_cap) { stack_.Reserve(new_cap); } |
| 59 | |
| 60 | /** |
| 61 | * @brief Erases all contexts in the buffer. |
| 62 | */ |
| 63 | sonic_force_inline void Clear() { stack_.Clear(); } |
| 64 | |
| 65 | /** |
| 66 | * @brief Push a value into buffer |
| 67 | * @param v the pushed value, as char, int... |
| 68 | */ |
| 69 | template <typename T> |
| 70 | sonic_force_inline void Push(T v) { |
| 71 | stack_.template Push<T>(v); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief Push a string into the buffer. |
| 76 | * @param s the beginning of string |
| 77 | * @param n the string size |
| 78 | */ |
| 79 | sonic_force_inline void Push(const char* s, size_t n) { stack_.Push(s, n); } |
| 80 | sonic_force_inline void PushUnsafe(const char* s, size_t n) { |
| 81 | stack_.PushUnsafe(s, n); |
| 82 | } |
nothing calls this directly
no outgoing calls
no test coverage detected