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-2017, 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 #ifndef mrpt_synch_pipe_H
10 #define mrpt_synch_pipe_H
11 
12 #include <mrpt/utils/CStream.h>
13 #include <string>
14 #include <memory> // for unique_ptr<>
15 
16 namespace mrpt
17 {
18 namespace synch
19 {
20 class CPipeReadEndPoint;
21 class CPipeWriteEndPoint;
22 
23 /** A pipe, portable across different OS.
24  * Pipes can be used as intraprocess (inter-threads) or interprocess
25  * communication mechanism.
26  * Read more on pipes here:
27  * http://www.gnu.org/software/libc/manual/html_node/Pipes-and-FIFOs.html
28  *
29  * \code
30  * std::unique_ptr<CPipeReadEndPoint> read_pipe;
31  * std::unique_ptr<CPipeWriteEndPoint> write_pipe;
32  *
33  * CPipe::createPipe(read_pipe,write_pipe);
34  *
35  * \endcode
36  *
37  * See also the example: MRPT/samples/threadsPipe/
38  *
39  * \ingroup synch_grp
40  */
41 class CPipe
42 {
43  public:
44  /** Creates a new pipe and returns the read & write end-points as newly
45  * allocated objects.
46  * \exception std::exception On any error during the pipe creation
47  */
48  /** Creates a new pipe and returns the read & write end-points as newly
49  * allocated objects. */
50  template <typename ReadPtr, typename WritePtr>
51  static void createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe);
52 
53  static void initializePipe(
54  CPipeReadEndPoint& outReadPipe, CPipeWriteEndPoint& outWritePipe);
55 
56  private:
57  /** No need to create any object of this class. */
58  CPipe();
59  ~CPipe();
60 }; // end of CPipe
61 
62 /** Common interface of read & write pipe end-points */
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 MRPT_OS_WINDOWS
102  void* m_pipe_file;
103 #else
105 #endif
106  virtual size_t Read(void* Buffer, size_t Count) override;
107  virtual size_t Write(const void* Buffer, size_t Count) override;
108 
109  /** Without effect in this class */
110  virtual uint64_t Seek(
111  uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
112  /** Without effect in this class */
113  virtual uint64_t getTotalBytesCount() override;
114  /** Without effect in this class */
115  virtual uint64_t getPosition() override;
116 }; // end of CPipeBaseEndPoint
117 static_assert(
120  "Copy Check");
121 
122 /** The read end-point in a pipe created with mrpt::synch::CPipe.
123  * Use the method mrpt::utils::CStream::ReadBuffer() of the base class CStream
124  * for blocking reading. */
126 {
127  friend class CPipe;
128 
129  public:
130  /** De-serializes one end-point description, for example, from a parent
131  * process. */
132  explicit CPipeReadEndPoint(const std::string& serialized);
133 
134  private:
136  /** Hide the write method in this read-only pipe. */
137  void WriteBuffer(const void* Buffer, size_t Count);
138 
139 }; // end of CPipeReadEndPoint
140 
141 /** The write end-point in a pipe created with mrpt::synch::CPipe.
142  * Use the method mrpt::utils::CStream::WriteBuffer() of the base class CStream
143  * for blocking writing. */
145 {
146  friend class CPipe;
147 
148  public:
149  /** De-serializes one end-point description, for example, from a parent
150  * process. */
151  explicit CPipeWriteEndPoint(const std::string& serialized);
152 
153  private:
155  /** Hide the read method in this write-only pipe. */
156  size_t ReadBuffer(void* Buffer, size_t Count);
157 
158 }; // end of CPipeWriteEndPoint
159 
160 template <typename ReadPtr, typename WritePtr>
161 void CPipe::createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe)
162 {
163  outReadPipe = ReadPtr(new CPipeReadEndPoint);
164  outWritePipe = WritePtr(new CPipeWriteEndPoint);
165  CPipe::initializePipe(*outReadPipe, *outWritePipe);
166 }
167 
168 } // End of namespace
169 
170 } // End of namespace
171 
172 #endif
void WriteBuffer(const void *Buffer, size_t Count)
Hide the write method in this read-only pipe.
std::string serialize()
Converts the end-point into a string suitable for reconstruction at a child process.
Definition: CPipe.cpp:107
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:144
virtual uint64_t getPosition() override
Without effect in this class.
Definition: CPipe.cpp:125
CPipe()
No need to create any object of this class.
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:226
CPipeBaseEndPoint & operator=(const CPipeBaseEndPoint &)=delete
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
unsigned int timeout_read_start_us
(Default=0) Timeout for read operations: microseconds (us) to wait for the first byte.
Definition: CPipe.h:88
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
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:125
virtual uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Without effect in this class.
Definition: CPipe.cpp:123
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CPipe.cpp:127
virtual uint64_t getTotalBytesCount() override
Without effect in this class.
Definition: CPipe.cpp:124
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.
Common interface of read & write pipe end-points.
Definition: CPipe.h:63
size_t ReadBuffer(void *Buffer, size_t Count)
Hide the read method in this write-only pipe.
bool isOpen() const
Returns false if the pipe was closed due to some error.
Definition: CPipe.h:95
void close()
Closes the pipe (normally not needed to be called by users, automatically done at destructor) ...
Definition: CPipe.cpp:59
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
GLsizei const GLfloat * value
Definition: glext.h:4117
A pipe, portable across different OS.
Definition: CPipe.h:41
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:161



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019