Main MRPT website > C++ reference for MRPT 1.5.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/base/link_pragmas.h>
13 #include <mrpt/utils/CUncopiable.h>
14 #include <mrpt/utils/CStream.h>
15 #include <string>
16 #include <memory> // for auto_ptr<>, unique_ptr<>
17 
18 namespace mrpt
19 {
20  namespace synch
21  {
22  class CPipeReadEndPoint;
23  class CPipeWriteEndPoint;
24 
25  /** A pipe, portable across different OS.
26  * Pipes can be used as intraprocess (inter-threads) or interprocess communication mechanism.
27  * Read more on pipes here: 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  */
42  {
43  public:
44  /** Creates a new pipe and returns the read & write end-points as newly allocated objects.
45  * \exception std::exception On any error during the pipe creation
46  */
47  /** Creates a new pipe and returns the read & write end-points as newly allocated objects. */
48  template <typename ReadPtr, typename WritePtr>
49  static void createPipe(ReadPtr& outReadPipe,WritePtr& outWritePipe);
50 
51  static void initializePipe(CPipeReadEndPoint &outReadPipe, CPipeWriteEndPoint &outWritePipe);
52 
53  private:
54  CPipe(); //!< No need to create any object of this class.
55  ~CPipe();
56  }; // end of CPipe
57 
58 
59  /** Common interface of read & write pipe end-points */
63  {
64  friend class CPipe;
65  public:
67  virtual ~CPipeBaseEndPoint();
68 
69  /** De-serializes one end-point description, for example, from a parent process. */
70  explicit CPipeBaseEndPoint(const std::string &serialized);
71 
72  /** Converts the end-point into a string suitable for reconstruction at a child process.
73  * This *invalidates* this object, since only one real end-point can exist at once.
74  */
75  std::string serialize();
76 
77  unsigned int timeout_read_start_us; //!< (Default=0) Timeout for read operations: microseconds (us) to wait for the first byte. 0 means infinite timeout.
78  unsigned int timeout_read_between_us; //!< (Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial reads inside one large read. 0 means infinite timeout.
79 
80  /** Returns false if the pipe was closed due to some error. */
81  inline bool isOpen() const { return m_pipe_file!=0; }
82 
83  /** Closes the pipe (normally not needed to be called by users, automatically done at destructor) */
84  void close();
85 
86  protected:
87 #ifdef MRPT_OS_WINDOWS
88  void * m_pipe_file;
89 #else
91 #endif
92  virtual size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
93  virtual size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
94 
95  virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE; //!< Without effect in this class
96  virtual uint64_t getTotalBytesCount() MRPT_OVERRIDE; //!< Without effect in this class
97  virtual uint64_t getPosition() MRPT_OVERRIDE; //!< Without effect in this class
98  }; // end of CPipeBaseEndPoint
99 
100  /** The read end-point in a pipe created with mrpt::synch::CPipe.
101  * Use the method mrpt::utils::CStream::ReadBuffer() of the base class CStream for blocking reading. */
103  {
104  friend class CPipe;
105  public:
106  /** De-serializes one end-point description, for example, from a parent process. */
107  explicit CPipeReadEndPoint(const std::string &serialized);
108 
109  private:
111  void WriteBuffer (const void *Buffer, size_t Count); //!< Hide the write method in this read-only pipe.
112 
113  }; // end of CPipeReadEndPoint
114 
115  /** The write end-point in a pipe created with mrpt::synch::CPipe.
116  * Use the method mrpt::utils::CStream::WriteBuffer() of the base class CStream for blocking writing. */
118  {
119  friend class CPipe;
120  public:
121  /** De-serializes one end-point description, for example, from a parent process. */
122  explicit CPipeWriteEndPoint(const std::string &serialized);
123 
124  private:
126  size_t ReadBuffer(void *Buffer, size_t Count); //!< Hide the read method in this write-only pipe.
127 
128  }; // end of CPipeWriteEndPoint
129 
130  template <typename ReadPtr, typename WritePtr>
131  void CPipe::createPipe(ReadPtr& outReadPipe,WritePtr& outWritePipe)
132  {
133  outReadPipe = ReadPtr(new CPipeReadEndPoint);
134  outWritePipe = WritePtr(new CPipeWriteEndPoint);
135  CPipe::initializePipe(*outReadPipe, *outWritePipe);
136  }
137 
138 
139 
140  } // End of namespace
141 
142 } // End of namespace
143 
144 #endif
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:117
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
unsigned int timeout_read_start_us
(Default=0) Timeout for read operations: microseconds (us) to wait for the first byte. 0 means infinite timeout.
Definition: CPipe.h:77
__int64 int64_t
Definition: rptypes.h:51
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:102
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:30
GLsizei const GLchar ** string
Definition: glext.h:3919
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Common interface of read & write pipe end-points.
Definition: CPipe.h:60
bool isOpen() const
Returns false if the pipe was closed due to some error.
Definition: CPipe.h:81
unsigned int timeout_read_between_us
(Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial rea...
Definition: CPipe.h:78
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:131



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020