* Java-like Object */
| 17 | * Java-like Object |
| 18 | */ |
| 19 | class SharedBuffer { |
| 20 | public: |
| 21 | SharedBuffer(); |
| 22 | |
| 23 | SharedBuffer(const SharedBuffer &o); |
| 24 | |
| 25 | SharedBuffer &operator=(const SharedBuffer &o); |
| 26 | |
| 27 | ~SharedBuffer(); |
| 28 | |
| 29 | [[nodiscard]] size_t size() const noexcept; |
| 30 | |
| 31 | [[nodiscard]] void *get() const noexcept; |
| 32 | |
| 33 | [[nodiscard]] SharedBuffer copy() const; |
| 34 | |
| 35 | template<class T> |
| 36 | [[nodiscard]] const T *at(size_t s) const noexcept { |
| 37 | if (s + sizeof(T) > size()) { |
| 38 | return nullptr; |
| 39 | } else { |
| 40 | return (const T *) (((const char *) get()) + s); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | template<class T> |
| 45 | [[nodiscard]] T *at(size_t s) noexcept { |
| 46 | if (s + sizeof(T) > size()) { |
| 47 | return nullptr; |
| 48 | } else { |
| 49 | return (T *) (((char *) get()) + s); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | [[nodiscard]] bool ensureCapacity(size_t size, std::nothrow_t) noexcept; |
| 54 | |
| 55 | inline void ensureCapacity(size_t size) { |
| 56 | if (size != 0 && !ensureCapacity(size, std::nothrow)) { |
| 57 | throw std::bad_alloc(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | [[nodiscard]] std::vector<uint8_t> toVector() const; |
| 62 | |
| 63 | [[nodiscard]] bool resetSize(size_t size, bool keepContent = true) noexcept; |
| 64 | |
| 65 | void reset() noexcept; |
| 66 | |
| 67 | private: |
| 68 | std::shared_ptr<SharedBufferImpl> pImpl; |
| 69 | }; |
| 70 | |
| 71 | #endif //RPCPROTOCOL_SHAREDBUFFER_H |