SatCat5
io_writeable.h
Go to the documentation of this file.
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
12 
13 #pragma once
14 
15 #include <satcat5/types.h>
16 
17 namespace satcat5 {
18  namespace io {
24  class Writeable {
25  public:
29  virtual unsigned get_write_space() const = 0;
30 
31  // Write various data types in big-endian format.
50  void write_u8(u8 data);
52  void write_u16(u16 data);
53  void write_u24(u32 data);
54  void write_u32(u32 data);
55  void write_u48(u64 data);
56  void write_u64(u64 data);
57  void write_s8(s8 data);
58  void write_s16(s16 data);
59  void write_s24(s32 data);
60  void write_s32(s32 data);
61  void write_s48(s64 data);
62  void write_s64(s64 data);
63  void write_f32(float data);
64  void write_f64(double data);
65 
66  // Write various data types in little-endian format.
67  void write_u16l(u16 data);
68  void write_u24l(u32 data);
69  void write_u32l(u32 data);
70  void write_u48l(u64 data);
71  void write_u64l(u64 data);
72  void write_s16l(s16 data);
73  void write_s24l(s32 data);
74  void write_s32l(s32 data);
75  void write_s48l(s64 data);
76  void write_s64l(s64 data);
77  void write_f32l(float data);
78  void write_f64l(double data);
80 
84  virtual void write_bytes(unsigned nbytes, const void* src);
85 
88  void write_str(const char* str);
89 
94  virtual bool write_finalize();
95 
100  virtual void write_abort();
101 
104  template <class T> inline void write_obj(const T& obj)
105  {obj.write_to(this);}
106 
107  protected:
108  friend LimitedWrite;
109  friend WriteableBroadcast;
110  friend WriteableRedirect;
111 
113  constexpr Writeable() {}
114  ~Writeable() {}
115 
118  virtual void write_next(u8 data) = 0;
119 
123  virtual void write_overflow();
124  };
125 
128  public:
134  constexpr ArrayWrite(void* dst, unsigned len)
135  : m_dst((u8*)dst), m_len(len), m_ovr(false), m_wridx(0), m_wrlen(0) {}
136  constexpr ArrayWrite(unsigned len, void* dst)
137  : m_dst((u8*)dst), m_len(len), m_ovr(false), m_wridx(0), m_wrlen(0) {}
139 
140  // Implement the public Readable API.
141  unsigned get_write_space() const override;
142  void write_abort() override;
143  bool write_finalize() override;
144  void write_overflow() override;
145 
147  const u8* buffer() const
148  { return m_dst; }
149 
151  inline unsigned written_len() const
152  { return m_wrlen; }
153 
154  private:
155  // Implement the private Writeable API.
156  void write_next(u8 data) override;
157 
158  // Internal state.
159  u8* const m_dst;
160  const unsigned m_len;
161  bool m_ovr;
162  unsigned m_wridx;
163  unsigned m_wrlen;
164  };
165 
168  template <unsigned SIZE>
170  public:
171  constexpr ArrayWriteStatic()
172  : ArrayWrite(m_buff, SIZE), m_buff{} {}
173  private:
174  u8 m_buff[SIZE];
175  };
176 
184  public:
189  constexpr LimitedWrite(satcat5::io::Writeable* dst, unsigned maxwr)
190  : m_dst(dst), m_rem(maxwr) {}
191  constexpr LimitedWrite(unsigned maxwr, satcat5::io::Writeable* dst)
192  : m_dst(dst), m_rem(maxwr) {}
194 
195  unsigned get_write_space() const override;
196  void write_bytes(unsigned nbytes, const void* src) override;
197 
198  protected:
199  // Implement the private Writeable API.
200  void write_next(u8 data) override;
201 
202  private:
203  satcat5::io::Writeable* const m_dst;
204  unsigned m_rem;
205  };
206 
219  public:
220  unsigned get_write_space() const override;
221  void write_abort() override;
222  void write_bytes(unsigned nbytes, const void* src) override;
223  bool write_finalize() override;
224  protected:
225  explicit constexpr WriteableRedirect(satcat5::io::Writeable* dst)
226  : m_dst(dst) {}
227  ~WriteableRedirect() {}
228  void write_next(u8 data) override;
229  void write_overflow() override;
230  inline void write_dst(satcat5::io::Writeable* dst) {m_dst = dst;}
231  private:
232  satcat5::io::Writeable* m_dst;
233  };
234 
237  public:
238  explicit constexpr NullWrite(unsigned wspace)
239  : m_write_space(wspace) {}
240  unsigned get_write_space() const override;
241  void write_bytes(unsigned nbytes, const void* src) override;
242  private:
243  void write_next(u8 data) override;
244  const unsigned m_write_space;
245  };
246 
250  }
251 }
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
unsigned written_len() const
Report total length after write_finalize() is called.
Definition: io_writeable.h:151
constexpr ArrayWrite(unsigned len, void *dst)
Create an ArrayWrite object linked to the provided working buffer.
Definition: io_writeable.h:136
constexpr ArrayWrite(void *dst, unsigned len)
Create an ArrayWrite object linked to the provided working buffer.
Definition: io_writeable.h:134
bool write_finalize() override
Mark end of frame and release temporary working data.
void write_abort() override
If possible, abort the current partially-written packet.
void write_overflow() override
Optional error handling for write overflow.
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
const u8 * buffer() const
Read-only access to the working buffer.
Definition: io_writeable.h:147
unsigned get_write_space() const override
How many bytes can be written without blocking?
Thin wrapper for ArrayWrite with a built-in buffer.
Definition: io_writeable.h:169
Limited write up to N bytes.
Definition: io_writeable.h:183
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
constexpr LimitedWrite(unsigned maxwr, satcat5::io::Writeable *dst)
Explicitly set maximum write length in bytes.
Definition: io_writeable.h:191
unsigned get_write_space() const override
How many bytes can be written without blocking?
constexpr LimitedWrite(satcat5::io::Writeable *dst, unsigned maxwr)
Explicitly set maximum write length in bytes.
Definition: io_writeable.h:189
Writeable object that accepts and discards all incoming data.
Definition: io_writeable.h:236
unsigned get_write_space() const override
How many bytes can be written without blocking?
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
Forwards incoming writes to multiple downstream Writeables.
Definition: io_broadcast.h:35
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
virtual unsigned get_write_space() const =0
How many bytes can be written without blocking?
virtual void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
virtual void write_abort()
If possible, abort the current partially-written packet.
void write_u8(u8 data)
One of many functions for writing integer/floating point values, see details.
Definition: io_writeable.cc:20
virtual void write_overflow()
Optional error handling for write overflow.
void write_str(const char *str)
Write the contents of a null-terminated string.
virtual void write_next(u8 data)=0
Write the next byte to the underlying buffer or device.
constexpr Writeable()
Only children should create or destroy the base class.
Definition: io_writeable.h:113
virtual bool write_finalize()
Mark end of frame and release temporary working data.
void write_obj(const T &obj)
Templated wrapper for any object with the following method: void write_to(satcat5::io::Writeable* wr)...
Definition: io_writeable.h:104
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
unsigned get_write_space() const override
How many bytes can be written without blocking?
void write_overflow() override
Optional error handling for write overflow.
bool write_finalize() override
Mark end of frame and release temporary working data.
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
void write_abort() override
If possible, abort the current partially-written packet.
satcat5::io::NullWrite null_write
Global instance of the basic NullWrite object.
Basic type aliases and prototypes used throughout SatCat5.