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