1 /* 2 * Copyright (C) 2018-2019 Codership Oy <info@codership.com> 3 * 4 * This file is part of wsrep-lib. 5 * 6 * Wsrep-lib is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * Wsrep-lib is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef WSREP_BUFFER_HPP 21 #define WSREP_BUFFER_HPP 22 23 #include <cstddef> 24 #include <vector> 25 26 namespace wsrep 27 { 28 class const_buffer 29 { 30 public: const_buffer()31 const_buffer() 32 : ptr_() 33 , size_() 34 { } 35 const_buffer(const void * ptr,size_t size)36 const_buffer(const void* ptr, size_t size) 37 : ptr_(ptr) 38 , size_(size) 39 { } 40 const_buffer(const const_buffer & b)41 const_buffer(const const_buffer& b) 42 : ptr_(b.ptr()) 43 , size_(b.size()) 44 { } 45 ptr() const46 const void* ptr() const { return ptr_; } data() const47 const char* data() const { return static_cast<const char*>(ptr_); } size() const48 size_t size() const { return size_; } 49 operator =(const const_buffer & b)50 const_buffer& operator=(const const_buffer& b) 51 { 52 ptr_ = b.ptr(); 53 size_ = b.size(); 54 return *this; 55 } 56 private: 57 const void* ptr_; 58 size_t size_; 59 }; 60 61 62 class mutable_buffer 63 { 64 public: mutable_buffer()65 mutable_buffer() 66 : buffer_() 67 { } 68 mutable_buffer(const mutable_buffer & b)69 mutable_buffer(const mutable_buffer& b) 70 : buffer_(b.buffer_) 71 { } 72 resize(size_t s)73 void resize(size_t s) { buffer_.resize(s); } 74 clear()75 void clear() 76 { 77 // using swap to ensure deallocation 78 std::vector<char>().swap(buffer_); 79 } 80 push_back(const char * begin,const char * end)81 void push_back(const char* begin, const char* end) 82 { 83 buffer_.insert(buffer_.end(), begin, end); 84 } 85 push_back(const C & c)86 template <class C> void push_back(const C& c) 87 { 88 std::copy(c.begin(), c.end(), std::back_inserter(buffer_)); 89 } 90 size() const91 size_t size() const { return buffer_.size(); } 92 93 /** 94 * Return pointer to underlying data array. The returned pointer 95 * may or may not be null in case of empty buffer, it is up to 96 * user to check the size of the array before dereferencing the 97 * pointer. 98 * 99 * @return Pointer to underlying data array. 100 */ data()101 char* data() { return buffer_.data(); } 102 103 /** 104 * Return const pointer to underlying data array. The returned pointer 105 * may or may not be null in case of empty buffer, it is up to 106 * user to check the size of the array before dereferencing the 107 * pointer. 108 * 109 * @return Const pointer to underlying data array. 110 */ data() const111 const char* data() const { return buffer_.data(); } 112 operator =(const mutable_buffer & other)113 mutable_buffer& operator= (const mutable_buffer& other) 114 { 115 buffer_ = other.buffer_; 116 return *this; 117 } 118 operator ==(const mutable_buffer & other) const119 bool operator==(const mutable_buffer& other) const 120 { 121 return buffer_ == other.buffer_; 122 } 123 private: 124 std::vector<char> buffer_; 125 }; 126 } 127 128 #endif // WSREP_BUFFER_HPP 129