12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #pragma once
- #include <vector>
- class MessageVector {
- public:
- struct header_t {
- size_t len_{};
- };
- size_t get_buff_size() const
- {
- return buffer_.size();
- }
- size_t get_buff_capacity() const
- {
- return buffer_.capacity();
- }
- void clear() {
- buffer_.clear();
- }
- void shrink(size_t size) {
- buffer_.reserve(size);
- }
- template<typename T, typename... Types>
- static size_t get_len(T value, Types... args)
- {
- if constexpr (std::is_convertible_v<T, std::size_t>) {
- return value ;
- }
- return 0;
- }
-
- template<typename... Types>
- bool put(Types&&... args){
- size_t total_len = (... + get_len(std::forward<Types>(args))) ;
- size_t buff_used = buffer_.size();
- buffer_.resize(buff_used + total_len + sizeof(header_t));
- header_t* p = (header_t*)(buffer_.data() + buff_used);
- p->len_ = total_len;
- char* pbuff = (char*)p + sizeof(header_t);
- auto func = [&pbuff](auto func, void* pdata, size_t len, auto&&... args) {
- memcpy(pbuff, pdata, len);
- pbuff += len;
- if constexpr (sizeof ... (args) > 0) {
- func(func, args...);
- }
- };
- func(func, std::forward<Types>(args)...);
-
- return true;
- }
- bool peek(size_t& it, const void*& pdata, size_t& len) const
- {
- size_t pos = it + sizeof(header_t);
- if(pos > buffer_.size()) {
- return false;
- }
- header_t* p = (header_t*)(buffer_.data() + it);
- if(pos + p->len_ > buffer_.size())
- {
- return false;
- }
- it = pos + p->len_;
- pdata = buffer_.data() + pos;
- len = p->len_;
- return true;
- }
- private:
- std::vector<char> buffer_;
- };
|