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