scclib
Stable Cloud Computing C++ Library
iopipeline.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_IOPIPELINE_H
32 #define _SCC_UTIL_IOPIPELINE_H
33 
34 #include <memory>
35 #include <list>
36 #include <util/iostream.h>
37 
38 namespace scc::util
39 {
40 
55 struct InChain
56 {
57  std::shared_ptr<Reader> rd_base;
58 
60  InChain(const std::shared_ptr<Reader>&);
61  virtual ~InChain() {}
62 
63  std::list<std::shared_ptr<PipelineReader>> rd_chain;
64 
66  void rd_replace_base(const std::shared_ptr<PipelineReader>& r)
67  {
68  r->read_reset(rd_base);
69  rd_base = r;
70  rd_fix_chain();
71  }
73  void rd_add_back(const std::shared_ptr<PipelineReader>& r)
74  {
75  rd_chain.emplace_back(r);
76  rd_fix_chain();
77  }
79  void rd_add_front(const std::shared_ptr<PipelineReader>& r)
80  {
81  rd_chain.emplace_front(r);
82  rd_fix_chain();
83  }
85  void rd_del(const std::shared_ptr<PipelineReader>&);
86 
88  virtual std::shared_ptr<Reader> rd_fix_chain();
89 };
90 
93 struct OutChain
94 {
95  std::shared_ptr<Writer> wr_base;
96 
98  OutChain(const std::shared_ptr<Writer>&);
99  virtual ~OutChain() {}
100 
101  std::list<std::shared_ptr<PipelineWriter>> wr_chain;
102 
104  void wr_replace_base(const std::shared_ptr<PipelineWriter>& w)
105  {
106  w->write_reset(wr_base);
107  wr_base = w;
108  wr_fix_chain();
109  }
111  void wr_add_back(const std::shared_ptr<PipelineWriter>& w)
112  {
113  wr_chain.emplace_back(w);
114  wr_fix_chain();
115  }
117  void wr_add_front(const std::shared_ptr<PipelineWriter>& w)
118  {
119  wr_chain.emplace_front(w);
120  wr_fix_chain();
121  }
123  void wr_del(const std::shared_ptr<PipelineWriter>&);
124 
126  virtual std::shared_ptr<Writer> wr_fix_chain();
127 };
128 
134 struct InPipeline : public InChain, public InStream
135 {
137  InPipeline(const std::shared_ptr<Reader>&, size_t = 1024);
138  virtual ~InPipeline() {}
139 
140  std::shared_ptr<Reader> rd_fix_chain();
141 };
142 
148 struct OutPipeline : public OutChain, public OutStream
149 {
151  OutPipeline(const std::shared_ptr<Writer>&, size_t = 1024);
152  virtual ~OutPipeline() {}
153 
154  std::shared_ptr<Writer> wr_fix_chain();
155 };
156 
162 struct IoPipeline : public InChain, public OutChain, public IoStream
163 {
165  IoPipeline(const std::shared_ptr<Reader>&, const std::shared_ptr<Writer>&, size_t = 1024, size_t = 1024);
166  virtual ~IoPipeline() {}
167 
169  void rw_add_back(const std::shared_ptr<PipelineReader>& r, const std::shared_ptr<PipelineWriter>& w)
170  {
171  rd_add_back(r);
172  wr_add_back(w);
173  }
175  void rw_add_front(const std::shared_ptr<PipelineReader>& r, const std::shared_ptr<PipelineWriter>& w)
176  {
177  rd_add_front(r);
178  wr_add_front(w);
179  }
180 
181  std::shared_ptr<Reader> rd_fix_chain();
182  std::shared_ptr<Writer> wr_fix_chain();
183 };
184 
187 }
188 
189 #endif
Input stream wrapper for reader.
Definition: iostream.h:59
Input/output stream wrapper for reader/writer.
Definition: iostream.h:157
Output stream wrapper for writer.
Definition: iostream.h:108
Base input/output stream classes.
Chain of readers base class.
Definition: iopipeline.h:56
void rd_replace_base(const std::shared_ptr< PipelineReader > &r)
Replace the base writer with a new base writer.
Definition: iopipeline.h:66
void rd_add_back(const std::shared_ptr< PipelineReader > &r)
Add a reader to the end of the chain (before the base).
Definition: iopipeline.h:73
void rd_del(const std::shared_ptr< PipelineReader > &)
Delete a reader from the chain.
Definition: iostream.cc:923
virtual std::shared_ptr< Reader > rd_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:932
InChain(const std::shared_ptr< Reader > &)
Create with base reader and read buffer size.
Definition: iostream.cc:919
void rd_add_front(const std::shared_ptr< PipelineReader > &r)
Add a reader to the start of the chain (after the stream).
Definition: iopipeline.h:79
Input stream with pipeline of readers.
Definition: iopipeline.h:135
std::shared_ptr< Reader > rd_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:986
InPipeline(const std::shared_ptr< Reader > &, size_t=1024)
Create with base reader and read buffer size.
Definition: iostream.cc:983
Input/output stream with pipeline of readers and writers.
Definition: iopipeline.h:163
void rw_add_back(const std::shared_ptr< PipelineReader > &r, const std::shared_ptr< PipelineWriter > &w)
Add to the end of the chain (before the base).
Definition: iopipeline.h:169
std::shared_ptr< Writer > wr_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:1014
IoPipeline(const std::shared_ptr< Reader > &, const std::shared_ptr< Writer > &, size_t=1024, size_t=1024)
Create with base reader/writer and buffer sizes.
Definition: iostream.cc:1003
std::shared_ptr< Reader > rd_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:1007
void rw_add_front(const std::shared_ptr< PipelineReader > &r, const std::shared_ptr< PipelineWriter > &w)
Add to the end of the chain (after the stream).
Definition: iopipeline.h:175
Chain of writers base class.
Definition: iopipeline.h:94
void wr_replace_base(const std::shared_ptr< PipelineWriter > &w)
Replace the base writer with a new base writer.
Definition: iopipeline.h:104
void wr_del(const std::shared_ptr< PipelineWriter > &)
Delete a writer from the chain.
Definition: iostream.cc:955
void wr_add_front(const std::shared_ptr< PipelineWriter > &w)
Add a writer to the start of the chain (after the stream).
Definition: iopipeline.h:117
void wr_add_back(const std::shared_ptr< PipelineWriter > &w)
Add a writer to the end of the chain (before the base).
Definition: iopipeline.h:111
OutChain(const std::shared_ptr< Writer > &)
Create with base writer and write buffer size.
Definition: iostream.cc:951
virtual std::shared_ptr< Writer > wr_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:964
Output stream pipeline of writers.
Definition: iopipeline.h:149
std::shared_ptr< Writer > wr_fix_chain()
Fix the chain, and return the pointer that should be pointed to by the stream.
Definition: iostream.cc:996
OutPipeline(const std::shared_ptr< Writer > &, size_t=1024)
Create with base writer and write buffer size.
Definition: iostream.cc:993