CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
InputOutput.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2017 Mateusz Szpakowski
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 Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_INPUTOUTPUT_H__
24 #define __CLRX_INPUTOUTPUT_H__
25 
26 #include <CLRX/Config.h>
27 #include <vector>
28 #include <cstring>
29 #include <istream>
30 #include <ostream>
31 #include <memory>
32 #include <streambuf>
33 #include <CLRX/utils/Utilities.h>
34 
36 namespace CLRX
37 {
38 
39 /*
40  * stream utilities
41  */
42 
44 class MemoryStreamBuf: public std::streambuf
45 {
46 protected:
47  std::ios_base::openmode openMode;
48 
50  MemoryStreamBuf(std::ios_base::openmode openMode);
51 
53  pos_type seekoff(off_type off, std::ios_base::seekdir dir,
54  std::ios_base::openmode which);
55 
57  pos_type seekpos(pos_type pos, std::ios_base::openmode which);
59  std::streamsize showmanyc();
61  int_type pbackfail(int_type ch);
62 
64  void safePBump(ssize_t offset);
65 public:
67  ~MemoryStreamBuf() = default;
68 };
69 
72 {
73 public:
75  ArrayStreamBuf(size_t size, char* buffer, std::ios_base::openmode openMode);
77  ~ArrayStreamBuf() = default;
78 
80  size_t getArraySize() const
81  { return egptr()-eback(); }
83  char* getArray() const
84  { return eback(); }
85 protected:
87  std::streambuf* setbuf(char_type* buffer, std::streamsize size);
88 };
89 
92 {
93 private:
94  std::string& string;
95 public:
97  StringStreamBuf(std::string& string, std::ios_base::openmode openMode);
99  ~StringStreamBuf() = default;
100 
102  std::string& getString() const
103  { return string; }
105  std::string& getString()
106  { return string; }
107 protected:
109  int_type overflow(int_type ch);
111  std::streambuf* setbuf(char_type* buffer, std::streamsize size);
112 };
113 
116 {
117 private:
118  std::vector<char>& vector;
119 public:
121  VectorStreamBuf(std::vector<char>& vector, std::ios_base::openmode openMode);
123  ~VectorStreamBuf() = default;
124 
126  std::vector<char>& getVector() const
127  { return vector; }
129  std::vector<char>& getVector()
130  { return vector; }
131 protected:
133  int_type overflow(int_type ch);
135  std::streambuf* setbuf(char_type* buffer, std::streamsize size);
136 };
137 
139 class ArrayIStream: public std::istream
140 {
141 private:
142  ArrayStreamBuf buffer;
143 public:
145  ArrayIStream(size_t size, const char* array);
147  ~ArrayIStream() = default;
148 
150  size_t getArraySize() const
151  { return buffer.getArraySize(); }
153  const char* getArray() const
154  { return buffer.getArray(); }
155 };
156 
158 class ArrayIOStream: public std::iostream
159 {
160 private:
161  ArrayStreamBuf buffer;
162 public:
164  ArrayIOStream(size_t size, char* array);
166  ~ArrayIOStream() = default;
167 
169  size_t getArraySize() const
170  { return buffer.getArraySize(); }
172  char* getArray() const
173  { return buffer.getArray(); }
174 };
175 
177 class ArrayOStream: public std::ostream
178 {
179 private:
180  ArrayStreamBuf buffer;
181 public:
183  ArrayOStream(size_t size, char* array);
185  ~ArrayOStream() = default;
186 
188  size_t getArraySize() const
189  { return buffer.getArraySize(); }
191  char* getArray() const
192  { return buffer.getArray(); }
193 };
194 
196 class StringIStream: public std::istream
197 {
198 private:
199  StringStreamBuf buffer;
200 public:
202  StringIStream(const std::string& string);
204  ~StringIStream() = default;
205 
207  const std::string& getString() const
208  { return buffer.getString(); }
209 };
210 
212 class StringIOStream: public std::iostream
213 {
214 private:
215  StringStreamBuf buffer;
216 public:
218  StringIOStream(std::string& string);
220  ~StringIOStream() = default;
221 
223  std::string& getString() const
224  { return buffer.getString(); }
225 };
226 
228 class StringOStream: public std::ostream
229 {
230 private:
231  StringStreamBuf buffer;
232 public:
234  StringOStream(std::string& string);
236  ~StringOStream() = default;
237 
239  std::string& getString() const
240  { return buffer.getString(); }
241 };
242 
244 class VectorIStream: public std::istream
245 {
246 private:
247  VectorStreamBuf buffer;
248 public:
250  VectorIStream(const std::vector<char>& string);
252  ~VectorIStream() = default;
253 
255  const std::vector<char>& getVector() const
256  { return buffer.getVector(); }
257 };
258 
260 class VectorIOStream: public std::iostream
261 {
262 private:
263  VectorStreamBuf buffer;
264 public:
266  VectorIOStream(std::vector<char>& string);
268  ~VectorIOStream() = default;
269 
271  std::vector<char>& getVector() const
272  { return buffer.getVector(); }
273 };
274 
276 class VectorOStream: public std::ostream
277 {
278 private:
279  VectorStreamBuf buffer;
280 public:
282  VectorOStream(std::vector<char>& string);
284  ~VectorOStream() = default;
285 
287  std::vector<char>& getVector() const
288  { return buffer.getVector(); }
289 };
290 
291 /*
292  * adaptor
293  */
294 
297 {
298 private:
299  std::istream& is;
300  cxuint pos, endPos;
301  cxuint bufSize;
302  std::unique_ptr<char[]> buffer;
303 public:
305 
309  FastInputBuffer(cxuint _bufSize, std::istream& input) : is(input), pos(0), endPos(0),
310  bufSize(_bufSize), buffer(new char[_bufSize])
311  { }
312 
314  const std::istream& getIStream() const
315  { return is; }
317  std::istream& getIStream()
318  { return is; }
319 
321  int get()
322  {
323  if (pos == endPos)
324  {
325  pos = 0;
326  is.read(buffer.get(), bufSize);
327  endPos = is.gcount();
328  if (endPos == 0 || is.bad())
329  return std::streambuf::traits_type::eof();
330  }
331  return (cxuchar)buffer.get()[pos++];
332  }
333 
335  size_t read(char* buf, cxuint n)
336  {
337  size_t toRead = std::min(n, endPos-pos);
338  ::memcpy(buf, buffer.get()+pos, toRead);
339  if (toRead < n)
340  {
341  is.read(buf+toRead, n-toRead);
342  toRead += is.gcount();
343  pos = endPos = 0;
344  }
345  return toRead;
346  }
347 };
348 
351 {
352 private:
353  std::ostream& os;
354  cxuint endPos;
355  cxuint bufSize;
356  std::unique_ptr<char[]> buffer;
357  uint64_t written;
358 public:
360 
364  FastOutputBuffer(cxuint _bufSize, std::ostream& output) : os(output), endPos(0),
365  bufSize(_bufSize), buffer(new char[_bufSize]), written(0)
366  { }
369  {
370  flush();
371  os.flush();
372  }
373 
375  uint64_t getWritten() const
376  { return written; }
377 
379  void flush()
380  {
381  os.write(buffer.get(), endPos);
382  endPos = 0;
383  }
384 
386  char* reserve(cxuint toReserve)
387  {
388  if (toReserve > bufSize-endPos)
389  flush();
390  return buffer.get() + endPos;
391  }
392 
394  void forward(cxuint toWrite)
395  {
396  endPos += toWrite;
397  written += toWrite;
398  }
399 
401  void write(size_t length, const char* string)
402  {
403  if (length > bufSize-endPos)
404  {
405  flush();
406  os.write(string, length);
407  }
408  else
409  {
410  ::memcpy(buffer.get()+endPos, string, length);
411  endPos += length;
412  }
413  written += length;
414  }
415 
417  void writeString(const char* string)
418  { write(::strlen(string), string); }
419 
421  template<typename T>
422  void writeObject(const T& t)
423  { write(sizeof(T), reinterpret_cast<const char*>(&t)); }
424 
426  template<typename T>
427  void writeArray(size_t size, const T* t)
428  { write(sizeof(T)*size, reinterpret_cast<const char*>(t)); }
429 
431  void put(char c)
432  {
433  if (endPos == bufSize)
434  flush();
435  buffer[endPos++] = c;
436  written++;
437  }
438 
440  void fill(size_t num, char c)
441  {
442  size_t count = num;
443  while (count != 0)
444  {
445  size_t bufNum = std::min(size_t(bufSize-endPos), count);
446  ::memset(buffer.get()+endPos, c, bufNum);
447  count -= bufNum;
448  endPos += bufNum;
449  if (endPos == bufSize)
450  flush();
451  }
452  written += num;
453  }
454 
456  const std::ostream& getOStream() const
457  { return os; }
459  std::ostream& getOStream()
460  { return os; }
461 };
462 
463 };
464 
465 #endif
std::string & getString() const
get a held string
Definition: InputOutput.h:102
memory stream buffer
Definition: InputOutput.h:44
specialized output stream that holds external char-vector for memory saving
Definition: InputOutput.h:276
non copyable and non movable base structure (class)
Definition: Utilities.h:43
uint64_t getWritten() const
get written bytes number
Definition: InputOutput.h:375
specialized input stream that holds external array for memory saving
Definition: InputOutput.h:139
std::ios_base::openmode openMode
open mode
Definition: InputOutput.h:47
std::vector< char > & getVector() const
get a held char vector
Definition: InputOutput.h:287
string stream buffer that holds external string for memory saving
Definition: InputOutput.h:91
std::string & getString() const
get a held string
Definition: InputOutput.h:223
pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which)
seekoff implementation
fast and direct output buffer
Definition: InputOutput.h:350
void write(size_t length, const char *string)
write sequence of characters
Definition: InputOutput.h:401
std::string & getString() const
get a held string
Definition: InputOutput.h:239
specialized output stream that holds external array for memory saving
Definition: InputOutput.h:177
void writeObject(const T &t)
write object of type T
Definition: InputOutput.h:422
void forward(cxuint toWrite)
finish reservation and go forward
Definition: InputOutput.h:394
void writeString(const char *string)
write string
Definition: InputOutput.h:417
std::vector< char > & getVector() const
get a held char vector
Definition: InputOutput.h:271
std::ostream & getOStream()
get output stream
Definition: InputOutput.h:459
size_t getArraySize() const
get a held array size
Definition: InputOutput.h:188
specialized input stream that holds external string for memory saving
Definition: InputOutput.h:196
std::streamsize showmanyc()
showmanyc implementation
~MemoryStreamBuf()=default
destructor
int_type pbackfail(int_type ch)
pbackfail implementation
size_t getArraySize() const
get a held array size
Definition: InputOutput.h:150
const std::string & getString() const
get a held string
Definition: InputOutput.h:207
const char * getArray() const
get a held array content
Definition: InputOutput.h:153
specialized output stream that holds external string for memory saving
Definition: InputOutput.h:228
specialized input/output stream that holds external array for memory saving
Definition: InputOutput.h:158
const std::istream & getIStream() const
get input stream
Definition: InputOutput.h:314
main namespace
Definition: AsmDefs.h:38
fast input buffer adapter
Definition: InputOutput.h:296
~FastOutputBuffer()
destructor
Definition: InputOutput.h:368
std::vector< char > & getVector()
get a held char vector
Definition: InputOutput.h:129
FastInputBuffer(cxuint _bufSize, std::istream &input)
constructor
Definition: InputOutput.h:309
array stream buffer that holds external static array for memory saving
Definition: InputOutput.h:71
std::istream & getIStream()
get input stream
Definition: InputOutput.h:317
specialized input/output stream that holds external char-vector for memory saving ...
Definition: InputOutput.h:260
char * getArray() const
get a held array content
Definition: InputOutput.h:83
size_t getArraySize() const
get a held array size
Definition: InputOutput.h:169
specialized input/output stream that holds external string for memory saving
Definition: InputOutput.h:212
utilities for other libraries and programs
specialized input stream that holds external char-vector for memory saving
Definition: InputOutput.h:244
void fill(size_t num, char c)
fill (put num c character)
Definition: InputOutput.h:440
size_t getArraySize() const
get a held array size
Definition: InputOutput.h:80
pos_type seekpos(pos_type pos, std::ios_base::openmode which)
seekpos implementation
char * getArray() const
get a held array content
Definition: InputOutput.h:191
char * getArray() const
get a held array content
Definition: InputOutput.h:172
const std::vector< char > & getVector() const
get a held char vector
Definition: InputOutput.h:255
std::string & getString()
get a held string
Definition: InputOutput.h:105
MemoryStreamBuf(std::ios_base::openmode openMode)
constructor
std::vector< char > & getVector() const
get a held char vector
Definition: InputOutput.h:126
void writeArray(size_t size, const T *t)
write array of objects
Definition: InputOutput.h:427
void safePBump(ssize_t offset)
safe pbump version
void put(char c)
put single character
Definition: InputOutput.h:431
void flush()
write output buffer
Definition: InputOutput.h:379
char * reserve(cxuint toReserve)
reserve and write out buffer if too few free bytes in buffer
Definition: InputOutput.h:386
const std::ostream & getOStream() const
get output stream
Definition: InputOutput.h:456
size_t read(char *buf, cxuint n)
read data from buffer and returns number of read bytes
Definition: InputOutput.h:335
vector char stream buffer external char-vector for memory saving
Definition: InputOutput.h:115
FastOutputBuffer(cxuint _bufSize, std::ostream &output)
constructor with inBufSize and output
Definition: InputOutput.h:364