File: stream_stdout.cpp

package info (click to toggle)
watchman 4.9.0-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,936 kB
  • sloc: cpp: 27,450; python: 6,534; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (77 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Copyright 2014-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */
#include "watchman.h"

using watchman::FileDescriptor;

namespace {
class StdioStream : public watchman_stream {
  const FileDescriptor& fd_;

 public:
  explicit StdioStream(const FileDescriptor& fd) : fd_(fd) {}

  int read(void* buf, int size) override {
    auto result = fd_.read(buf, size);
    if (result.hasError()) {
      errno = result.error().value();
#ifdef _WIN32
      // TODO: propagate Result<int, std::error_code> as return type
      errno = map_win32_err(errno);
#endif
      return -1;
    }
    return result.value();
  }

  int write(const void* buf, int size) override {
    auto result = fd_.write(buf, size);
    if (result.hasError()) {
      errno = result.error().value();
#ifdef _WIN32
      // TODO: propagate Result<int, std::error_code> as return type
      errno = map_win32_err(errno);
#endif
      return -1;
    }
    return result.value();
  }

  w_evt_t getEvents() override {
    w_log(W_LOG_FATAL, "calling get_events on a stdio stm\n");
    return nullptr;
  }

  void setNonBlock(bool) override {}

  bool rewind() override {
    return false;
  }

  bool shutdown() override {
    return false;
  }

  bool peerIsOwner() override {
    return false;
  }

  pid_t getPeerProcessID() const override {
    return 0;
  }

  const watchman::FileDescriptor& getFileDescriptor() const override {
    return fd_;
  }
};
}

w_stm_t w_stm_stdout(void) {
  static StdioStream stdoutStream(FileDescriptor::stdOut());
  return &stdoutStream;
}

w_stm_t w_stm_stdin(void) {
  static StdioStream stdinStream(FileDescriptor::stdIn());
  return &stdinStream;
}