scclib
Stable Cloud Computing C++ Library
rwloopbuf.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_UTIL_RWLOOPBUF_H
32 #define _SCC_UTIL_RWLOOPBUF_H
33 
34 #include <vector>
35 #include <string>
36 #include <cstring>
37 #include <util/iobase.h>
38 
39 namespace scc::util
40 {
41 
56 class RwLoopBuffer : public Reader, public Writer, public std::vector<char>
57 {
58  size_t m_idx;
59 public:
60  RwLoopBuffer();
61  virtual ~RwLoopBuffer() {}
62 
63  size_t idx() const { return m_idx; }
64 
66  RwLoopBuffer(const std::string& b)
67  {
68  set(b);
69  }
70 
72  RwLoopBuffer(const std::vector<char>& v)
73  {
74  set(v);
75  }
76 
78  RwLoopBuffer& operator=(const std::string& b)
79  {
80  set(b.data(), b.size());
81  return *this;
82  }
83 
85  void clear()
86  {
87  m_idx = 0;
88  std::vector<char>::clear();
89  }
90 
92  void set(const void* loc, size_t len)
93  {
94  clear();
95  write(loc, len);
96  }
97 
99  void set(const std::vector<char>& v)
100  {
101  set(v.data(), v.size());
102  }
103 
105  void set(const std::string& v)
106  {
107  set(v.data(), v.size());
108  }
109 
111  size_t read(void* loc, size_t len)
112  {
113  if (m_idx == size())
114  {
115  return 0;
116  }
117  int rd = len < size()-m_idx ? len : size()-m_idx;
118  memcpy(loc, (char*)data()+m_idx, rd);
119  m_idx += rd;
120  return rd;
121  }
122 
124  size_t const read_loc()
125  {
126  return m_idx;
127  }
128 
130  size_t write(const void* loc, size_t len)
131  {
132  insert(end(), (char*)loc, (char*)loc+len);
133  return len;
134  }
135 
137  size_t const write_loc()
138  {
139  return size();
140  }
141 
143  std::string str()
144  {
145  if (size() == 0)
146  {
147  return "";
148  }
149  return std::string((char*)data()+m_idx, (char*)data()+size());
150  }
151 
153  std::vector<char> vec() const
154  {
155  if (size() == 0)
156  {
157  return std::vector<char>();
158  }
159  return std::vector<char>((char*)data()+m_idx, (char*)data()+size());
160  }
161 };
162 
165 }
166 
167 #endif
Loopback read/write stream buffer.
Definition: rwloopbuf.h:57
std::string str()
Read as a string.
Definition: rwloopbuf.h:143
size_t const read_loc()
Return the current read location.
Definition: rwloopbuf.h:124
void clear()
Empty and reset.
Definition: rwloopbuf.h:85
std::vector< char > vec() const
Read as a vector.
Definition: rwloopbuf.h:153
void set(const std::string &v)
Clear and set for reading.
Definition: rwloopbuf.h:105
RwLoopBuffer(const std::vector< char > &v)
Create as a vector.
Definition: rwloopbuf.h:72
void set(const std::vector< char > &v)
Clear and set for reading.
Definition: rwloopbuf.h:99
RwLoopBuffer & operator=(const std::string &b)
Set to a string.
Definition: rwloopbuf.h:78
RwLoopBuffer()
rw loop buffer
Definition: iostream.cc:911
RwLoopBuffer(const std::string &b)
Create as a string.
Definition: rwloopbuf.h:66
size_t write(const void *loc, size_t len)
Writer write.
Definition: rwloopbuf.h:130
size_t const write_loc()
Return the current write location.
Definition: rwloopbuf.h:137
size_t read(void *loc, size_t len)
Reader read.
Definition: rwloopbuf.h:111
void set(const void *loc, size_t len)
Clear and set for reading.
Definition: rwloopbuf.h:92
Input/output stream base reader/writer interface classes.
Interface class for objects which can be read.
Definition: iobase.h:67
Interface class for objects which can be written.
Definition: iobase.h:86