MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ByteSink.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  ByteSink.h Stream output support
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef BYTESINK_H
28 #define BYTESINK_H
29 
30 #include "itype.h"
31 #include "Fail.h"
32 #include "Format.h"
33 #include <stdarg.h> /* For ... */
34 
35 namespace MFM {
36 
37  class ByteSerializable; // FORWARD
38 
47  class ByteSink {
48  public:
49 
63  virtual void WriteBytes(const u8 * data, const u32 len) = 0;
64 
65 
73  virtual s32 CanWrite() = 0;
74 
81  virtual void WriteByte(u8 ch) {
82  WriteBytes(&ch, 1);
83  }
84 
91  virtual void WriteNewline() {
92  WriteByte('\n');
93  }
94 
95  virtual ~ByteSink() { }
96 
97  void Print(const char * str, s32 fieldWidth = -1, u8 padChar = ' ');
98  void Print(const u8 * str, u32 len, s32 fieldWidth = -1, u8 padChar = ' ');
99  void Print(s32 decimal, s32 fieldWidth = -1, u8 padChar = ' ');
100  void Print(u32 decimal, s32 fieldWidth = -1, u8 padChar = ' ');
101  void Print(s64 decimal, s32 fieldWidth = -1, u8 padChar = ' ');
102  void Print(u64 decimal, s32 fieldWidth = -1, u8 padChar = ' ');
103  void Print(u32 num, Format::Type code, s32 fieldWidth = -1, u8 padChar = ' ');
104  void Print(s32 num, Format::Type code, s32 fieldWidth = -1, u8 padChar = ' ');
105  void Print(u64 num, Format::Type code, s32 fieldWidth = -1, u8 padChar = ' ');
106  void Print(s64 num, Format::Type code, s32 fieldWidth = -1, u8 padChar = ' ');
107 
108  void Print(ByteSerializable & byteSerializble, s32 argument = 0) ;
109 
110  void Println()
111  {
112  WriteNewline();
113  }
114 
115  void Println(u8 byte)
116  {
117  Print(byte);
118  Println();
119  }
120  void Println(const char * str)
121  {
122  Print(str);
123  Println();
124  }
125  void Println(const u8 * str, u32 len)
126  {
127  Print(str,len);
128  Println();
129  }
130  void Println(s32 decimal)
131  {
132  Print(decimal);
133  Println();
134  }
135  void Println(u32 decimal)
136  {
137  Print(decimal);
138  Println();
139  }
140  void Println(u32 decimal, Format::Type code)
141  {
142  Print(decimal,code);
143  Println();
144  }
145 
146  void Println(ByteSerializable & byteSerializable, s32 argument = 0) ;
147 
148  void Printf(const char * format, ...) ;
149 
150  void Vprintf(const char * format, va_list & ap) ;
151 
152  private:
153  template <class UNSIGNED_TYPE>
154  static u32 CountDigits(UNSIGNED_TYPE num, u32 base) {
155  u32 digits = 0;
156  do {
157  ++digits;
158  num /= base;
159  } while (num > 0);
160  return digits;
161  }
162 
163  bool IsPrint(u32 ch) { return ch >= ' ' && ch <= '~'; }
164 
165  void PrintLexDigits(u32 digits) ;
166 
167  template <class UNSIGNED_TYPE>
168  void PrintInBase(UNSIGNED_TYPE num, u32 base, s32 width = 0, u8 pad = ' ') ;
169  };
170 
171  class DiscardBytes : public ByteSink {
172  public:
173 
174  virtual void WriteBytes(const u8 * data, const u32 len)
175  { }
176 
177  virtual s32 CanWrite()
178  {
179  return S32_MAX;
180  }
181 
182  };
183 
184  extern DiscardBytes DevNull;
185 
186 }
187 
188 #include "ByteSink.tcc"
189 
190 #endif /* BYTESINK_H */
virtual s32 CanWrite()
Definition: ByteSink.h:177
Definition: ByteSink.h:171
void Println(s32 decimal)
Definition: ByteSink.h:130
virtual void WriteNewline()
Definition: ByteSink.h:91
virtual void WriteBytes(const u8 *data, const u32 len)
Definition: ByteSink.h:174
Definition: ByteSink.h:47
virtual void WriteByte(u8 ch)
Definition: ByteSink.h:81
void Println(const u8 *str, u32 len)
Definition: ByteSink.h:125
void Print(const char *str, s32 fieldWidth=-1, u8 padChar= ' ')
Definition: ByteSink.cpp:31
Type
Definition: Format.h:39
#define S32_MAX
Definition: itype.h:86
void Println(u32 decimal)
Definition: ByteSink.h:135
void Println(const char *str)
Definition: ByteSink.h:120
virtual void WriteBytes(const u8 *data, const u32 len)=0
virtual s32 CanWrite()=0
void Println(u32 decimal, Format::Type code)
Definition: ByteSink.h:140
Definition: ByteSerializable.h:45