YARP
Yet Another Robot Platform
PortWriterBuffer.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_PORTWRITERBUFFER_H
8 #define YARP_OS_PORTWRITERBUFFER_H
9 
10 #include <yarp/os/Portable.h>
12 
13 namespace yarp {
14 namespace os {
15 
16 class Port;
17 
18 #ifndef DOXYGEN_SHOULD_SKIP_THIS
19 
20 template <class T>
21 class PortWriterBufferAdaptor : public PortWriterWrapper
22 {
23 public:
24  PortWriterBufferManager& creator;
25  T writer;
26  void* tracker;
27 
28  PortWriterBufferAdaptor(PortWriterBufferManager& creator,
29  void* tracker) :
30  creator(creator),
31  tracker(tracker)
32  {
33  }
34 
35  bool write(ConnectionWriter& connection) const override
36  {
37  return writer.write(connection);
38  }
39 
40  void onCompletion() const override
41  {
42  writer.onCompletion();
43  creator.onCompletion(tracker);
44  }
45 
46  void onCommencement() const override
47  {
48  writer.onCommencement();
49  }
50 
51  PortWriter* getInternal() override
52  {
53  return &writer;
54  }
55 };
56 
57 #endif // DOXYGEN_SHOULD_SKIP_THIS
58 
59 
65 template <class T>
67 {
68 public:
78  T& prepare()
79  {
80  return get();
81  }
82 
90  bool unprepare()
91  {
92  return releaseContent();
93  }
94 
99  T& get()
100  {
101  PortWriterBufferAdaptor<T>* content = (PortWriterBufferAdaptor<T>*)getContent(); // guaranteed to be non-NULL
102  return content->writer;
103  }
104 
113  int getCount()
114  {
116  }
117 
122  void attach(Port& port)
123  {
125  }
126 
130  void write(bool forceStrict = false)
131  {
132  PortWriterBufferBase::write(forceStrict);
133  }
134 
139  {
141  }
142 
143  virtual PortWriterWrapper* create(PortWriterBufferManager& man,
144  void* tracker) override
145  {
146  return new PortWriterBufferAdaptor<T>(man, tracker);
147  }
148 };
149 
150 } // namespace os
151 } // namespace yarp
152 
153 #endif // YARP_OS_PORTWRITERBUFFER_H
Buffer outgoing data to a port.
bool unprepare()
Give the last prepared object back to YARP without writing it.
virtual PortWriterWrapper * create(PortWriterBufferManager &man, void *tracker) override
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
void waitForWrite()
Wait until any pending writes are done.
T & prepare()
Access the object which will be transmitted by the next call to PortWriterBuffer::write.
int getCount()
Check the number of buffers currently in use for communication.
T & get()
A synonym of PortWriterBuffer::prepare.
void attach(Port &port)
Set the Port to which objects will be written.
A mini-server for network communication.
Definition: Port.h:47
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1099
The main, catch-all namespace for YARP.
Definition: dirs.h:16