Electroneum
buffer.h
Go to the documentation of this file.
1 // Copyright (c) 2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <vector>
32 #include "misc_log_ex.h"
33 #include "span.h"
34 
35 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
36 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.buffer"
37 
38 //#define NET_BUFFER_LOG(x) MDEBUG(x)
39 #define NET_BUFFER_LOG(x) ((void)0)
40 
41 namespace epee
42 {
43 namespace net_utils
44 {
45 class buffer
46 {
47 public:
48  buffer(size_t reserve = 0): offset(0) { storage.reserve(reserve); }
49 
50  void append(const void *data, size_t sz);
51  void erase(size_t sz) { NET_BUFFER_LOG("erasing " << sz << "/" << size()); CHECK_AND_ASSERT_THROW_MES(offset + sz <= storage.size(), "erase: sz too large"); offset += sz; if (offset == storage.size()) { storage.resize(0); offset = 0; } }
52  epee::span<const uint8_t> span(size_t sz) const { CHECK_AND_ASSERT_THROW_MES(sz <= size(), "span is too large"); return epee::span<const uint8_t>(storage.data() + offset, sz); }
53  // carve must keep the data in scope till next call, other API calls (such as append, erase) can invalidate the carved buffer
54  epee::span<const uint8_t> carve(size_t sz) { CHECK_AND_ASSERT_THROW_MES(sz <= size(), "span is too large"); offset += sz; return epee::span<const uint8_t>(storage.data() + offset - sz, sz); }
55  size_t size() const { return storage.size() - offset; }
56 
57 private:
58  std::vector<uint8_t> storage;
59  size_t offset;
60 };
61 }
62 }
void append(const void *data, size_t sz)
Definition: buffer.cpp:40
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
void erase(size_t sz)
Definition: buffer.h:51
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
#define NET_BUFFER_LOG(x)
Definition: buffer.h:39
size_t size() const
Definition: buffer.h:55
epee::span< const uint8_t > carve(size_t sz)
Definition: buffer.h:54
buffer(size_t reserve=0)
Definition: buffer.h:48
epee::span< const uint8_t > span(size_t sz) const
Definition: buffer.h:52