foobar2000 SDK  2015-08-03
nix-objects.h
Go to the documentation of this file.
1 #include <sys/select.h>
2 #include <sys/time.h>
3 #include <set>
4 
5 namespace pfc {
6 
7  void nixFormatError( string_base & str, int code );
8 
9  class exception_nix : public std::exception {
10  public:
11  exception_nix();
12  exception_nix(int code);
13 
14  ~exception_nix() throw() { }
15 
16  int code() const throw() {return m_code;}
17  const char * what() const throw() {return m_msg.get_ptr();}
18  private:
19  void _init(int code);
20  int m_code;
22  };
23 
24  // Toggles non-blocking mode on a file descriptor.
25  void setNonBlocking( int fd, bool bNonBlocking = true );
26 
27  // Toggles close-on-exec mode on a file descriptor.
28  void setCloseOnExec( int fd, bool bCloseOnExec = true );
29 
30  // Toggles inheritable mode on a file descriptor. Reverse of close-on-exec.
31  void setInheritable( int fd, bool bInheritable = true );
32 
33  // Creates a pipe. The pipe is NOT inheritable by default (close-on-exec set).
34  void createPipe( int fd[2], bool bInheritable = false );
35 
36  timeval makeTimeVal( double seconds );
37  double importTimeval(const timeval & tv);
38 
39  class fdSet {
40  public:
41 
42  void operator+=( int fd );
43  void operator-=( int fd );
44  bool operator[] (int fd );
45  void clear();
46 
47  void operator+=( fdSet const & other );
48 
49  std::set<int> m_fds;
50  };
51 
52 
53  bool fdCanRead( int fd );
54  bool fdCanWrite( int fd );
55 
56  bool fdWaitRead( int fd, double timeOutSeconds );
57  bool fdWaitWrite( int fd, double timeOutSeconds );
58 
59  class fdSelect {
60  public:
61 
62  int Select();
63  int Select( double timeOutSeconds );
64  int Select_( int timeOutMS );
65 
66  fdSet Reads, Writes, Errors;
67  };
68 
69  void nixSleep(double seconds);
70 
71  class nix_event {
72  public:
73  nix_event();
74  ~nix_event();
75 
76  void set_state( bool state );
77 
78  bool is_set( ) {return wait_for(0); }
79 
80  bool wait_for( double p_timeout_seconds );
81 
82  static bool g_wait_for( int p_event, double p_timeout_seconds );
83 
84  int get_handle() const {return m_fd[0]; }
85 
86  // Two-wait event functions, return 0 on timeout, 1 on evt1 set, 2 on evt2 set
87  static int g_twoEventWait( nix_event & ev1, nix_event & ev2, double timeout );
88  static int g_twoEventWait( int h1, int h2, double timeOut );
89 
90  private:
91  nix_event(nix_event const&);
92  void operator=(nix_event const&);
93  int m_fd[2];
94  };
95 
96  double nixGetTime();
97 
98  bool nixReadSymLink( string_base & strOut, const char * path );
99  bool nixSelfProcessPath( string_base & strOut );
100 
101  void nixGetRandomData( void * outPtr, size_t outBytes );
102 
103  bool isShiftKeyPressed();
104  bool isCtrlKeyPressed();
105  bool isAltKeyPressed();
106 }
107 
108 void uSleepSeconds( double seconds, bool );
void _init(int code)
Definition: nix-objects.cpp:59
void setNonBlocking(int fd, bool bNonBlocking)
Definition: nix-objects.cpp:21
std::set< int > m_fds
Definition: nix-objects.h:49
bool nixSelfProcessPath(string_base &strOut)
void setCloseOnExec(int fd, bool bCloseOnExec)
Definition: nix-objects.cpp:29
void nixGetRandomData(void *outPtr, size_t outBytes)
bool isShiftKeyPressed()
void nixFormatError(string_base &str, int code)
Definition: nix-objects.cpp:15
bool isCtrlKeyPressed()
bool isAltKeyPressed()
void uSleepSeconds(double seconds, bool)
bool nixReadSymLink(string_base &strOut, const char *path)
double nixGetTime()
const char * get_ptr() const
Definition: string_base.h:382
bool fdWaitRead(int fd, double timeOutSeconds)
bool fdCanRead(int fd)
bool fdWaitWrite(int fd, double timeOutSeconds)
std::exception exception
Definition: primitives.h:193
bool fdCanWrite(int fd)
timeval makeTimeVal(double timeSeconds)
Definition: nix-objects.cpp:64
void createPipe(int fd[2], bool bInheritable)
Definition: nix-objects.cpp:41
int code() const
Definition: nix-objects.h:16
int get_handle() const
Definition: nix-objects.h:84
void setInheritable(int fd, bool bInheritable)
Definition: nix-objects.cpp:37
const char * what() const
Definition: nix-objects.h:17
void nixSleep(double seconds)
double importTimeval(const timeval &in)
Definition: nix-objects.cpp:71