Main MRPT website > C++ reference for MRPT 1.9.9
io/CStream.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/common.h> // MRPT_printf_format_check
12 #include <cstdint>
13 #include <string>
14 
15 namespace mrpt
16 {
17 namespace io
18 {
19 /** This base class is used to provide a unified interface to
20  * files,memory buffers,..Please see the derived classes. This class is
21  * largely inspired by Borland VCL "TStream" class. <br><br>
22  * Apart of the "VCL like" methods, operators ">>" and "<<" have been
23  * defined so that simple types (int,bool,char,float,char *,std::string,...)
24  * can be directly written and read to and from any CStream easily.
25  * Please, it is recomendable to read CSerializable documentation also.
26  *
27  * \ingroup mrpt_io_grp
28  * \sa CFileStream, CMemoryStream,CSerializable
29  */
30 class CStream
31 {
32  public:
33  /** Used in CStream::Seek */
35  {
39  };
40 
41  /** Introduces a pure virtual method responsible for reading from the
42  * stream. */
43  virtual size_t Read(void* Buffer, size_t Count) = 0;
44 
45  /** Introduces a pure virtual method responsible for writing to the stream.
46  * Write attempts to write up to Count bytes to Buffer, and returns the
47  * number of bytes actually written. */
48  virtual size_t Write(const void* Buffer, size_t Count) = 0;
49 
50  /* Constructor
51  */
52  CStream() {}
53  /* Destructor
54  */
55  virtual ~CStream();
56 
57  /** Reads a block of bytes from the stream into Buffer, and returns the
58  *amound of bytes actually read, without waiting for more extra bytes to
59  *arrive (just those already enqued in the stream).
60  * Note that this method will fallback to ReadBuffer() in most CStream
61  *classes but in some hardware-related classes.
62  * \exception std::exception On any error, or if ZERO bytes are read.
63  */
64  virtual size_t ReadBufferImmediate(void* Buffer, size_t Count)
65  {
66  return Read(Buffer, Count);
67  }
68 
69  /** Introduces a pure virtual method for moving to a specified position in
70  *the streamed resource.
71  * he Origin parameter indicates how to interpret the Offset parameter.
72  *Origin should be one of the following values:
73  * - sFromBeginning (Default) Offset is from the beginning of the
74  *resource. Seek moves to the position Offset. Offset must be >= 0.
75  * - sFromCurrent Offset is from the current position in the resource.
76  *Seek moves to Position + Offset.
77  * - sFromEnd Offset is from the end of the resource. Offset must
78  *be
79  *<= 0 to indicate a number of bytes before the end of the file.
80  * \return Seek returns the new value of the Position property.
81  */
82  virtual uint64_t Seek(
83  int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0;
84 
85  /** Returns the total amount of bytes in the stream.
86  */
87  virtual uint64_t getTotalBytesCount() const = 0;
88 
89  /** Method for getting the current cursor position, where 0 is the first
90  * byte and TotalBytesCount-1 the last one.
91  */
92  virtual uint64_t getPosition() const = 0;
93 
94  /** Writes a string to the stream in a textual form.
95  * \sa CStdOutStream
96  */
97  virtual int printf(const char* fmt, ...)
98  MRPT_printf_format_check(2, 3); // The first argument (1) is "this" !!!
99 
100  /** Prints a vector in the format [A,B,C,...] using CStream::printf, and the
101  * fmt string for <b>each</b> vector element `T`.
102  * \tparam CONTAINER_TYPE can be any vector<T>, deque<T> or alike. */
103  template <typename CONTAINER_TYPE>
105  const char* fmt, const CONTAINER_TYPE& V, char separator = ',')
106  {
107  this->printf("[");
108  const size_t N = V.size();
109  for (size_t i = 0; i < N; i++)
110  {
111  this->printf(fmt, V[i]);
112  if (i != (N - 1)) this->printf("%c", separator);
113  }
114  this->printf("]");
115  }
116 
117  /** Reads from the stream until a '\n' character is found ('\r' characters
118  * are ignored).
119  * \return false on EOF or any other read error.
120  */
121  bool getline(std::string& out_str);
122 
123 }; // End of class def.
124 
125 } // namespace io
126 } // namespace mrpt
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:30
virtual size_t Read(void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for reading from the stream.
virtual uint64_t getTotalBytesCount() const =0
Returns the total amount of bytes in the stream.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30
__int64 int64_t
Definition: rptypes.h:49
virtual ~CStream()
Definition: CStream.cpp:26
GLsizei const GLchar ** string
Definition: glext.h:4101
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
virtual uint64_t getPosition() const =0
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
virtual size_t Write(const void *Buffer, size_t Count)=0
Introduces a pure virtual method responsible for writing to the stream.
virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning)=0
Introduces a pure virtual method for moving to a specified position in the streamed resource...
virtual size_t ReadBufferImmediate(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read...
Definition: io/CStream.h:64
bool getline(std::string &out_str)
Reads from the stream until a &#39; &#39; character is found (&#39;&#39; characters are ignored). ...
Definition: CStream.cpp:69
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
Definition: common.h:158
virtual int void printf_vector(const char *fmt, const CONTAINER_TYPE &V, char separator=',')
Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector e...
Definition: io/CStream.h:104



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019