Main MRPT website > C++ reference for MRPT 1.9.9
CPipe.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/io/CStream.h>
12 #include <string>
13 #include <memory> // for unique_ptr<>
14 
15 namespace mrpt
16 {
17 namespace io
18 {
19 class CPipeReadEndPoint;
20 class CPipeWriteEndPoint;
21 
22 /** A pipe, portable across different OS.
23  * Pipes can be used as intraprocess (inter-threads) or interprocess
24  * communication mechanism.
25  * Read more on pipes here:
26  * http://www.gnu.org/software/libc/manual/html_node/Pipes-and-FIFOs.html
27  *
28  * \code
29  * std::unique_ptr<CPipeReadEndPoint> read_pipe;
30  * std::unique_ptr<CPipeWriteEndPoint> write_pipe;
31  *
32  * CPipe::createPipe(read_pipe,write_pipe);
33  *
34  * \endcode
35  *
36  * See also the example: MRPT/samples/threadsPipe/
37  *
38  * \ingroup mrpt_io_grp
39  */
40 class CPipe
41 {
42  public:
43  /** Create via createPipe() instead */
44  CPipe() = delete;
45  ~CPipe() = delete;
46 
47  /** Creates a new pipe and returns the read & write end-points as newly
48  * allocated objects.
49  * \exception std::exception On any error during the pipe creation
50  */
51  /** Creates a new pipe and returns the read & write end-points as newly
52  * allocated objects. */
53  template <typename ReadPtr, typename WritePtr>
54  static void createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe);
55 
56  static void initializePipe(
57  CPipeReadEndPoint& outReadPipe, CPipeWriteEndPoint& outWritePipe);
58 }; // end of CPipe
59 
60 /** Common interface of read & write pipe end-points
61  * \ingroup mrpt_io_grp
62  */
64 {
65  friend class CPipe;
66 
67  public:
69 
70  CPipeBaseEndPoint(const CPipeBaseEndPoint&) = delete;
72 
73  virtual ~CPipeBaseEndPoint();
74 
75  /** De-serializes one end-point description, for example, from a parent
76  * process. */
77  explicit CPipeBaseEndPoint(const std::string& serialized);
78 
79  /** Converts the end-point into a string suitable for reconstruction at a
80  * child process.
81  * This *invalidates* this object, since only one real end-point can exist
82  * at once.
83  */
85 
86  /** (Default=0) Timeout for read operations: microseconds (us) to wait for
87  * the first byte. 0 means infinite timeout. */
88  unsigned int timeout_read_start_us;
89  /** (Default=0) Timeout between burst reads operations: microseconds (us) to
90  * wait between two partial reads inside one large read. 0 means infinite
91  * timeout. */
93 
94  /** Returns false if the pipe was closed due to some error. */
95  inline bool isOpen() const { return m_pipe_file != 0; }
96  /** Closes the pipe (normally not needed to be called by users,
97  * automatically done at destructor) */
98  void close();
99 
100  protected:
101 #ifdef _WIN32
102  void* m_pipe_file;
103 #else
104  int m_pipe_file;
105 #endif
106  public:
107  virtual size_t Read(void* Buffer, size_t Count) override;
108  virtual size_t Write(const void* Buffer, size_t Count) override;
109 
110  /** Without effect in this class */
111  virtual uint64_t Seek(
112  int64_t of, CStream::TSeekOrigin o = sFromBeginning) override;
113  /** Without effect in this class */
114  virtual uint64_t getTotalBytesCount() const override;
115  /** Without effect in this class */
116  virtual uint64_t getPosition() const override;
117 }; // end of CPipeBaseEndPoint
118 static_assert(
121  "Copy Check");
122 
123 /** The read end-point in a pipe created with mrpt::synch::CPipe.
124  * Use the method CStream::Read() of the base class CStream
125  * for blocking reading.
126  * \ingroup mrpt_io_grp
127  */
129 {
130  friend class CPipe;
131 
132  public:
133  /** De-serializes one end-point description, for example, from a parent
134  * process. */
135  explicit CPipeReadEndPoint(const std::string& serialized);
136 
137  /** Read-only pipe, don't call this method */
138  size_t Write(const void* Buffer, size_t Count)
139  {
140  throw std::runtime_error("CPipeReadEndPoint::Write() cant be called.");
141  }
142 
143  private:
145 
146 }; // end of CPipeReadEndPoint
147 
148 /** The write end-point in a pipe created with mrpt::synch::CPipe.
149  * Use the method CStream::Write() of the base class CStream
150  * for blocking writing. */
152 {
153  friend class CPipe;
154 
155  public:
156  /** De-serializes one end-point description, for example, from a parent
157  * process. */
158  explicit CPipeWriteEndPoint(const std::string& serialized);
159 
160  /** Write-only pipe: read launches exception */
161  size_t Read(void* Buffer, size_t Count)
162  {
163  throw std::runtime_error("CPipeWriteEndPoint::Read() cant be called.");
164  }
165 
166  private:
168 
169 }; // end of CPipeWriteEndPoint
170 
171 template <typename ReadPtr, typename WritePtr>
172 void CPipe::createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe)
173 {
174  outReadPipe = ReadPtr(new CPipeReadEndPoint);
175  outWritePipe = WritePtr(new CPipeWriteEndPoint);
176  CPipe::initializePipe(*outReadPipe, *outWritePipe);
177 }
178 
179 } // End of namespace
180 } // End of namespace
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
mrpt::io::CPipeBaseEndPoint::timeout_read_start_us
unsigned int timeout_read_start_us
(Default=0) Timeout for read operations: microseconds (us) to wait for the first byte.
Definition: CPipe.h:88
mrpt::io::CPipeBaseEndPoint::serialize
std::string serialize()
Converts the end-point into a string suitable for reconstruction at a child process.
Definition: CPipe.cpp:105
mrpt::io::CPipeReadEndPoint::Write
size_t Write(const void *Buffer, size_t Count)
Read-only pipe, don't call this method.
Definition: CPipe.h:138
mrpt::io::CPipeBaseEndPoint::m_pipe_file
void * m_pipe_file
Definition: CPipe.h:102
mrpt::io::CPipeBaseEndPoint::close
void close()
Closes the pipe (normally not needed to be called by users, automatically done at destructor)
Definition: CPipe.cpp:59
mrpt::io::CPipeReadEndPoint
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:128
mrpt::io::CPipeBaseEndPoint::Seek
virtual uint64_t Seek(int64_t of, CStream::TSeekOrigin o=sFromBeginning) override
Without effect in this class.
Definition: CPipe.cpp:116
mrpt::io::CPipeBaseEndPoint::operator=
CPipeBaseEndPoint & operator=(const CPipeBaseEndPoint &)=delete
mrpt::io::CPipeBaseEndPoint::isOpen
bool isOpen() const
Returns false if the pipe was closed due to some error.
Definition: CPipe.h:95
int64_t
__int64 int64_t
Definition: rptypes.h:49
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::io::CPipeBaseEndPoint::CPipeBaseEndPoint
CPipeBaseEndPoint()
Definition: CPipe.cpp:52
mrpt::io::CPipeBaseEndPoint
Common interface of read & write pipe end-points.
Definition: CPipe.h:63
mrpt::io::CPipe
A pipe, portable across different OS.
Definition: CPipe.h:40
mrpt::io::CPipeBaseEndPoint::timeout_read_between_us
unsigned int timeout_read_between_us
(Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial rea...
Definition: CPipe.h:92
mrpt::io::CPipeReadEndPoint::CPipeReadEndPoint
CPipeReadEndPoint()
Definition: CPipe.cpp:238
mrpt::io::CStream::sFromBeginning
@ sFromBeginning
Definition: io/CStream.h:36
mrpt::io::CPipe::~CPipe
~CPipe()=delete
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::io::CPipe::createPipe
static void createPipe(ReadPtr &outReadPipe, WritePtr &outWritePipe)
Creates a new pipe and returns the read & write end-points as newly allocated objects.
Definition: CPipe.h:172
mrpt::io::CPipeBaseEndPoint::~CPipeBaseEndPoint
virtual ~CPipeBaseEndPoint()
Definition: CPipe.cpp:57
mrpt::io::CPipe::initializePipe
static void initializePipe(CPipeReadEndPoint &outReadPipe, CPipeWriteEndPoint &outWritePipe)
Creates a new pipe and returns the read & write end-points as newly allocated objects.
Definition: CPipe.cpp:30
CStream.h
mrpt::io::CPipeWriteEndPoint::CPipeWriteEndPoint
CPipeWriteEndPoint()
Definition: CPipe.cpp:245
mrpt::io::CPipe::CPipe
CPipe()=delete
Create via createPipe() instead.
mrpt::io::CPipeWriteEndPoint
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:151
mrpt::io::CPipeWriteEndPoint::Read
size_t Read(void *Buffer, size_t Count)
Write-only pipe: read launches exception.
Definition: CPipe.h:161
mrpt::io::CPipeBaseEndPoint::getPosition
virtual uint64_t getPosition() const override
Without effect in this class.
Definition: CPipe.cpp:118
value
GLsizei const GLfloat * value
Definition: glext.h:4117
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::io::CPipeBaseEndPoint::Read
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CPipe.cpp:120
mrpt::io::CPipeBaseEndPoint::Write
virtual size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CPipe.cpp:219
mrpt::io::CPipeBaseEndPoint::getTotalBytesCount
virtual uint64_t getTotalBytesCount() const override
Without effect in this class.
Definition: CPipe.cpp:117
mrpt::io::CStream
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:30



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST