YARP
Yet Another Robot Platform
FakeTwoWayStream.h
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 #ifndef YARP_OS_IMPL_FAKETWOWAYSTREAM_H
11 #define YARP_OS_IMPL_FAKETWOWAYSTREAM_H
12 
15 #include <yarp/os/TwoWayStream.h>
16 
17 namespace yarp {
18 namespace os {
19 namespace impl {
20 
25  public TwoWayStream
26 {
27 public:
28  FakeTwoWayStream(StringInputStream* target = nullptr) :
29  TwoWayStream()
30  {
31  this->out.owner = this;
32  this->target = target;
33  }
34 
36  {
37  this->target = &target;
38  }
39 
41  {
42  return in;
43  }
44 
46  {
47  return in;
48  }
49 
51  {
52  return out;
53  }
54 
55  const Contact& getLocalAddress() const override
56  {
57  return local;
58  }
59 
60  const Contact& getRemoteAddress() const override
61  {
62  return remote;
63  }
64 
65  void close() override
66  {
67  in.close();
68  out.close();
69  }
70 
71  virtual void apply(const Bytes& b)
72  {
73  if (target != nullptr) {
74  target->add(b);
75  }
76  }
77 
78  void addInputText(const std::string& str)
79  {
80  in.add(str);
81  }
82 
83  std::string getOutputText() const
84  {
85  return out.toString();
86  }
87 
88  std::string getInputText() const
89  {
90  return in.toString();
91  }
92 
93  bool isOk() const override
94  {
95  return true;
96  }
97 
98  void reset() override
99  {
100  }
101 
102  void beginPacket() override
103  {
104  }
105 
106  void endPacket() override
107  {
108  }
109 
110 private:
111  class ActiveStringOutputStream : public StringOutputStream
112  {
113  public:
114  ActiveStringOutputStream() :
115  owner(nullptr)
116  {
117  }
118 
120  void write(const Bytes& b) override
121  {
123  if (owner) {
124  owner->apply(b);
125  }
126  }
127 
128  FakeTwoWayStream* owner;
129  };
130 
131  StringInputStream in;
132  ActiveStringOutputStream out;
133  Contact local;
134  Contact remote;
135  StringInputStream* target;
136 };
137 
138 } // namespace impl
139 } // namespace os
140 } // namespace yarp
141 
142 #endif // YARP_OS_IMPL_FAKETWOWAYSTREAM_H
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
yarp::os::impl::FakeTwoWayStream::reset
void reset() override
Reset the stream.
Definition: FakeTwoWayStream.h:98
yarp::os::OutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
yarp::os::impl::FakeTwoWayStream::getLocalAddress
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: FakeTwoWayStream.h:55
yarp::os::impl::FakeTwoWayStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: FakeTwoWayStream.h:93
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
yarp::os::impl::FakeTwoWayStream::getRemoteAddress
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: FakeTwoWayStream.h:60
yarp::os::StringInputStream::add
void add(const std::string &txt)
Definition: StringInputStream.h:47
yarp::os::StringInputStream::close
void close() override
Terminate the stream.
Definition: StringInputStream.h:74
yarp::os::impl::FakeTwoWayStream::getInputStream
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: FakeTwoWayStream.h:40
yarp::os::impl::FakeTwoWayStream::apply
virtual void apply(const Bytes &b)
Definition: FakeTwoWayStream.h:71
yarp::os::StringOutputStream
An OutputStream that produces a string.
Definition: StringOutputStream.h:25
StringInputStream.h
TwoWayStream.h
yarp::os::impl::FakeTwoWayStream::getOutputText
std::string getOutputText() const
Definition: FakeTwoWayStream.h:83
yarp::os::impl::FakeTwoWayStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: FakeTwoWayStream.h:106
yarp::os::impl::FakeTwoWayStream::setTarget
void setTarget(StringInputStream &target)
Definition: FakeTwoWayStream.h:35
yarp::os::impl::FakeTwoWayStream::getInputText
std::string getInputText() const
Definition: FakeTwoWayStream.h:88
yarp::os::StringInputStream
An InputStream that reads from a string.
Definition: StringInputStream.h:25
yarp::os::impl::FakeTwoWayStream::getStringInputStream
virtual StringInputStream & getStringInputStream()
Definition: FakeTwoWayStream.h:45
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::impl::FakeTwoWayStream::close
void close() override
Terminate the stream.
Definition: FakeTwoWayStream.h:65
yarp::os::impl::FakeTwoWayStream
A dummy two way stream for testing purposes.
Definition: FakeTwoWayStream.h:26
yarp::os::impl::FakeTwoWayStream::addInputText
void addInputText(const std::string &str)
Definition: FakeTwoWayStream.h:78
StringOutputStream.h
yarp::os::impl::FakeTwoWayStream::getOutputStream
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: FakeTwoWayStream.h:50
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::StringInputStream::toString
virtual std::string toString() const
Definition: StringInputStream.h:78
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
yarp::os::StringOutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::os::impl::FakeTwoWayStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: FakeTwoWayStream.h:102
yarp::os::impl::FakeTwoWayStream::FakeTwoWayStream
FakeTwoWayStream(StringInputStream *target=nullptr)
Definition: FakeTwoWayStream.h:28