Savarese Software Research Corporation
EventLoop.h
Go to the documentation of this file.
1 /* Copyright 2006-2008, 2016 Savarese Software Research Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.savarese.com/software/ApacheLicense-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
21 #ifndef __SSRC_WISP_SERVICE_EVENT_LOOP_H
22 #define __SSRC_WISP_SERVICE_EVENT_LOOP_H
23 
25 
26 #include <memory>
27 
29 
30 struct EventLoopState;
31 
35 class EventLoop {
36 public:
37 
38  static const bool Once = true;
39  static const bool Persist = false;
40 
41  typedef int EventIO;
42 
43  static const EventIO None;
44  static const EventIO Read;
45  static const EventIO Write;
46  static const EventIO Error;
47  static const EventIO Hangup;
48  /*
49  static const EventIO Signal;
50  static const EventIO Timeout;
51  */
52  explicit EventLoop() SSRC_DECL_THROW(std::runtime_error);
53 
54  // Note, we cannot use ~EventLoop() = default; here because then
55  // the compiler must generate the destructor, which requires EventLoopState
56  // to be a complete type at this stage so that the destructor of
57  // std::unique_ptr<EventLoopState> may be properly invoked. All
58  // implementation must be in EventLoop.cc for this to work.
59  ~EventLoop();
60 
61  void add_handler(EventHandler & handler,
62  const int events,
63  const TimeValue & timeout = InfiniteTimeValue,
64  const bool once = Persist)
65  SSRC_DECL_THROW(std::runtime_error);
66 
67  void remove_handler(EventHandler & handler);
68 
69  unsigned int count_handlers();
70 
71  unsigned int count_io_handlers();
72 
73  bool running();
74 
75  void start();
76 
77  void stop();
78 
79 private:
80  // Only allow EventLoop.cc to see EventLoopState implementation details.
81  // This speeds up client compilation times and avoids potential
82  // conflicts arising from our placing system headers in custom
83  // namespaces (e.g., Linux:: for <sys/epoll.h>). This comes
84  // at the expense of awkwardness, requiring client code to
85  // specify events as int instead of the native type,
86  // and inability to inline accessor functions.
87  const std::unique_ptr<EventLoopState> _state;
88 };
89 
90 class EventInfo {
91  friend void EventLoop::start();
92 
93  //int _fd;
94  bool _timeout;
95  int _io_events;
96  EventLoop & _loop;
97  TimeValue _now;
98 
99 public:
100 
101  explicit EventInfo(EventLoop & loop,
102  //const int fd = -1,
103  const int io_events = EventLoop::None,
104  const bool timeout = false) :
105  /* _fd(fd),*/ _timeout(timeout), _io_events(io_events), _loop(loop), _now()
106  { }
107 
108  int io_events() const {
109  return _io_events;
110  }
111 
112  /*
113  int descriptor() const {
114  return _fd;
115  }
116  */
117  EventLoop & event_loop() const {
118  return _loop;
119  }
120  /*
121  bool signal_event() const {
122  return (_event & 0);
123  }
124  */
125 
130  const TimeValue & now() const {
131  return _now;
132  }
133 
134  bool timeout_event() const {
135  return _timeout;
136  }
137 
138  bool error_event() const {
139  return (_io_events & EventLoop::Error);
140  }
141 
142  bool hangup_event() const {
143  return (_io_events & EventLoop::Hangup);
144  }
145 
146  bool read_event() const {
147  return (_io_events & EventLoop::Read);
148  }
149 
150  bool write_event() const {
151  return (_io_events & EventLoop::Write);
152  }
153 };
154 
156 
157 #endif

Savarese Software Research Corporation
Copyright © 2006-2012 Savarese Software Research Corporation. All rights reserved.