scclib
Stable Cloud Computing C++ Library
rwcounter.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_RWCOUNTER_H
32 #define _SCC_UTIL_RWCOUNTER_H
33 
34 #include <atomic>
35 #include <memory>
36 #include <util/iobase.h>
37 
38 namespace scc::util
39 {
40 
59 {
60  Reader* m_reader;
61  std::shared_ptr<Reader> m_shared;
62  std::atomic_uint_least64_t m_count;
63  std::atomic_uint_least64_t m_calls;
64 
65 public:
67  ReadCounter();
71  ReadCounter(const std::shared_ptr<Reader>&);
72 
73  virtual ~ReadCounter() {}
74 
76  void read_reset(Reader&);
78  virtual void read_reset(const std::shared_ptr<Reader>&);
79  virtual std::shared_ptr<Reader> read_shared() const { return m_shared; }
80 
81  virtual size_t read(void*, size_t);
82 
83  uint64_t read_count() const { return m_count; }
84  void read_count(uint64_t v) { m_count = v; }
85  void read_count_reset() { m_count = 0; }
86  uint64_t read_calls() const { return m_calls; }
87  void read_calls(uint64_t v) { m_calls = v; }
88  void read_calls_reset() { m_calls = 0; }
89 };
90 
97 {
98  Writer* m_writer;
99  std::shared_ptr<Writer> m_shared;
100  std::atomic_uint_least64_t m_count;
101  std::atomic_uint_least64_t m_calls;
102 
103 public:
105  WriteCounter();
109  WriteCounter(const std::shared_ptr<Writer>&);
110 
111  virtual ~WriteCounter() {}
112 
114  void write_reset(Writer&);
116  virtual void write_reset(const std::shared_ptr<Writer>&);
117  virtual std::shared_ptr<Writer> write_shared() const { return m_shared; }
118 
119  virtual size_t write(const void*, size_t);
120 
121  uint64_t write_count() const { return m_count; }
122  void write_count(uint64_t v) { m_count = v; }
123  void write_count_reset() { m_count = 0; }
124  uint64_t write_calls() const { return m_calls; }
125  void write_calls(uint64_t v) { m_calls = v; }
126  void write_calls_reset() { m_calls = 0; }
127 };
128 
134 class RwCounter : public ReadCounter, public WriteCounter
135 {
136 public:
138  RwCounter();
140  RwCounter(Reader&, Writer&);
142  RwCounter(const std::shared_ptr<Reader>&, const std::shared_ptr<Writer>&);
143 
144  virtual ~RwCounter() {}
145 };
146 
149 }
150 
151 #endif
Adds byte count to a read stream.
Definition: rwcounter.h:59
ReadCounter()
Reads will return 0 until reset.
Definition: iostream.cc:807
void read_reset(Reader &)
Reset the chained reader.
Definition: iostream.cc:831
virtual size_t read(void *, size_t)
Read interface.
Definition: iostream.cc:821
Adds byte count to a read/write stream.
Definition: rwcounter.h:135
RwCounter()
Reads and writes will return 0 until reset.
Definition: iostream.cc:895
Adds byte count to a write stream.
Definition: rwcounter.h:97
void write_reset(Writer &)
Reset the chained writer.
Definition: iostream.cc:875
virtual size_t write(const void *, size_t)
Write interface.
Definition: iostream.cc:865
WriteCounter()
Writes will return 0 until reset.
Definition: iostream.cc:851
Input/output stream base reader/writer interface classes.
Pipeline reader to carry out processing in a pipeline (chain of readers).
Definition: iobase.h:76
Pipeline writer to carry out processing in a pipeline (chain of writers).
Definition: iobase.h:95
Interface class for objects which can be read.
Definition: iobase.h:67
Interface class for objects which can be written.
Definition: iobase.h:86