YARP
Yet Another Robot Platform
ShiftStream.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/ShiftStream.h>
8 
14 
16 {
17 public:
19  ~Private();
20  void close();
21 
22  TwoWayStream* stream{nullptr};
24 };
25 
27 
29 {
30  close();
31 }
32 
34 {
35  if (stream != nullptr) {
36  stream->close();
37  delete stream;
38  stream = nullptr;
39  }
40 }
41 
42 
43 ShiftStream::ShiftStream() :
44  mPriv(new Private())
45 {
46 }
47 
49 {
50  delete mPriv;
51 }
52 
53 void ShiftStream::check() const
54 {
55 }
56 
58 {
59  check();
60  if (mPriv->stream == nullptr) {
61  return mPriv->nullStream;
62  }
63  return mPriv->stream->getInputStream();
64 }
65 
67 {
68  check();
69  if (mPriv->stream == nullptr) {
70  return mPriv->nullStream;
71  }
72  return mPriv->stream->getOutputStream();
73 }
74 
76 {
77  check();
78  return (mPriv->stream == nullptr) ? mPriv->nullStream.getLocalAddress()
79  : (mPriv->stream->getLocalAddress());
80 }
81 
83 {
84  check();
85  return (mPriv->stream == nullptr) ? mPriv->nullStream.getRemoteAddress()
86  : (mPriv->stream->getRemoteAddress());
87 }
88 
90 {
91  mPriv->close();
92 }
93 
95 {
96  close();
97  mPriv->stream = stream;
98 }
99 
101 {
102  TwoWayStream* result = mPriv->stream;
103  mPriv->stream = nullptr;
104  return result;
105 }
106 
108 {
109  return mPriv->stream;
110 }
111 
113 {
114  return mPriv->stream == nullptr;
115 }
116 
117 bool ShiftStream::isOk() const
118 {
119  if (mPriv->stream != nullptr) {
120  return mPriv->stream->isOk();
121  }
122  return false;
123 }
124 
126 {
127  if (mPriv->stream != nullptr) {
128  mPriv->stream->reset();
129  }
130 }
131 
133 {
134  if (mPriv->stream != nullptr) {
135  mPriv->stream->beginPacket();
136  }
137 }
138 
140 {
141  if (mPriv->stream != nullptr) {
142  mPriv->stream->endPacket();
143  }
144 }
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
A "null" stream, always invalid.
Definition: TwoWayStream.h:106
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
A container for a stream, allowing the stream implementation to be replaced when needed.
Definition: ShiftStream.h:23
void endPacket() override
Mark the end of a logical packet (see beginPacket).
virtual void takeStream(TwoWayStream *stream)
Wrap the supplied stream.
Definition: ShiftStream.cpp:94
virtual ~ShiftStream()
Destructor.
Definition: ShiftStream.cpp:48
bool isOk() const override
Check if the stream is ok or in an error state.
virtual TwoWayStream * giveStream()
Removes the wrapped stream and returns it.
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: ShiftStream.cpp:57
virtual void check() const
Perform maintenance actions, if needed.
Definition: ShiftStream.cpp:53
virtual TwoWayStream * getStream() const
void reset() override
Reset the stream.
void beginPacket() override
Mark the beginning of a logical packet.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: ShiftStream.cpp:75
virtual bool isEmpty() const
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: ShiftStream.cpp:82
void close() override
Terminate the stream.
Definition: ShiftStream.cpp:89
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: ShiftStream.cpp:66
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26