P4C
The P4 Compiler
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
fdstream.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_FDSTREAM_H_
19#define BACKENDS_TOFINO_BF_ASM_FDSTREAM_H_
20
21#include <sys/socket.h>
22#include <sys/types.h>
23
24#include <functional>
25#include <iostream>
26#include <streambuf>
27
28class fdstream : public std::iostream {
29 struct buffer_t : public std::basic_streambuf<char> {
30 int fd;
31
32 public:
33 explicit buffer_t(int _fd) : fd(_fd) {}
34 ~buffer_t() {
35 delete[] eback();
36 delete[] pbase();
37 }
38 int sync();
39 int_type underflow();
40 int_type overflow(int_type c = traits_type::eof());
41 void reset() {
42 setg(eback(), eback(), eback());
43 setp(pbase(), epptr());
44 }
45 } buffer;
46 std::function<void()> closefn;
47
48 public:
49 explicit fdstream(int fd = -1) : std::iostream(&buffer), buffer(fd) { init(&buffer); }
50 ~fdstream() {
51 if (closefn) closefn();
52 }
53 void connect(int fd) {
54 flush();
55 buffer.reset();
56 buffer.fd = fd;
57 }
58 void setclose(std::function<void()> fn) { closefn = fn; }
59};
60
61#endif /* BACKENDS_TOFINO_BF_ASM_FDSTREAM_H_ */