scclib
Stable Cloud Computing C++ Library
iobase.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_IOBASE_H
32 #define _SCC_UTIL_IOBASE_H
33 
34 #include <memory>
35 
36 namespace scc::util
37 {
38 
66 struct Reader
67 {
68  virtual ~Reader() {}
70  virtual size_t read(void*, size_t) = 0;
71 };
72 
75 struct PipelineReader : public Reader
76 {
77  virtual ~PipelineReader() {}
79  virtual void read_reset(const std::shared_ptr<Reader>&) = 0;
80  virtual std::shared_ptr<Reader> read_shared() const = 0;
81 };
82 
85 struct Writer
86 {
87  virtual ~Writer() {}
89  virtual size_t write(const void*, size_t) = 0;
90 };
91 
94 struct PipelineWriter : public Writer
95 {
96  virtual ~PipelineWriter() {}
98  virtual void write_reset(const std::shared_ptr<Writer>&) = 0;
99  virtual std::shared_ptr<Writer> write_shared() const = 0;
100 };
101 
105 {
106  std::shared_ptr<Reader> reader;
107 
108  FlowThroughPipelineReader() : reader(nullptr) {}
109  FlowThroughPipelineReader(const std::shared_ptr<Reader>& r) : reader(r) {}
110  virtual ~FlowThroughPipelineReader() {}
111  virtual size_t read(void* loc, size_t len)
112  {
113  if (reader) return reader->read(loc, len);
114  else return 0;
115  }
116  virtual void read_reset(const std::shared_ptr<Reader>& r)
117  {
118  reader = r;
119  }
120  virtual std::shared_ptr<Reader> read_shared() const { return reader; };
121 };
122 
126 {
127  std::shared_ptr<Writer> writer;
128 
129  FlowThroughPipelineWriter() : writer(nullptr) {}
130  FlowThroughPipelineWriter(const std::shared_ptr<Writer>& w) : writer(w) {}
131  virtual ~FlowThroughPipelineWriter() {}
132  virtual size_t write(const void* loc, size_t len)
133  {
134  if (writer) return writer->write(loc, len);
135  else return 0;
136  }
137  virtual void write_reset(const std::shared_ptr<Writer>& w)
138  {
139  writer = w;
140  }
141  virtual std::shared_ptr<Writer> write_shared() const { return writer; };
142 };
145 }
146 
147 #endif
Pipeline reader which flows through all data.
Definition: iobase.h:105
virtual void read_reset(const std::shared_ptr< Reader > &r)
Reset interface.
Definition: iobase.h:116
virtual size_t read(void *loc, size_t len)
Read interface.
Definition: iobase.h:111
Pipeline writer which flows through all data.
Definition: iobase.h:126
virtual size_t write(const void *loc, size_t len)
Write interface.
Definition: iobase.h:132
virtual void write_reset(const std::shared_ptr< Writer > &w)
Reset interface.
Definition: iobase.h:137
Pipeline reader to carry out processing in a pipeline (chain of readers).
Definition: iobase.h:76
virtual void read_reset(const std::shared_ptr< Reader > &)=0
Reset interface.
Pipeline writer to carry out processing in a pipeline (chain of writers).
Definition: iobase.h:95
virtual void write_reset(const std::shared_ptr< Writer > &)=0
Reset interface.
Interface class for objects which can be read.
Definition: iobase.h:67
virtual size_t read(void *, size_t)=0
Read interface.
Interface class for objects which can be written.
Definition: iobase.h:86
virtual size_t write(const void *, size_t)=0
Write interface.