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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020