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
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
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:29
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:804
The main, catch-all namespace for YARP.
Definition: environment.h:18