#ifndef MSP_IO_POLL_H_
#define MSP_IO_POLL_H_
-#ifndef WIN32
-#include <poll.h>
-#endif
-#include <list>
#include <map>
#include <vector>
+#include <msp/core/noncopyable.h>
#include <msp/time/timedelta.h>
-#include "types.h"
namespace Msp {
namespace IO {
-class Base;
+class EventObject;
enum PollEvent
{
P_INPUT = 1,
P_PRIO = 2,
P_OUTPUT = 4,
- P_ERROR = 8
+ P_ERROR = 8,
+ P_HANGUP = 16
};
inline PollEvent operator|(PollEvent e, PollEvent f)
{ return PollEvent(~static_cast<int>(e)); }
-class Poller
+class Poller: private NonCopyable
{
public:
- struct Slot
+ struct PolledObject
{
- Base *object;
+ EventObject *object;
PollEvent events;
-
- Slot(Base *o, PollEvent e): object(o), events(e) { }
};
- typedef std::list<Slot> SlotSeq;
private:
- typedef std::map<Base *, Slot> SlotMap;
+ struct Private;
-#ifdef WIN32
- struct pollfd
- {
- Handle fd;
- };
-#endif
-
- SlotMap objects;
- std::vector<pollfd> pfd;
- bool pfd_dirty;
- SlotSeq poll_result;
-
- void rebuild_pfd();
- int do_poll(int);
+ std::vector<PolledObject> objects;
+ Private *priv = nullptr;
+ bool events_changed = false;
+ bool objs_changed = false;
+ std::vector<PolledObject> poll_result;
public:
Poller();
+ ~Poller();
- void set_object(Base &, PollEvent);
- int poll();
- int poll(const Time::TimeDelta &);
- const SlotSeq &get_result() const { return poll_result; }
+ void set_object(EventObject &, PollEvent);
+ unsigned poll();
+ unsigned poll(const Time::TimeDelta &);
+private:
+ void rebuild_array();
+ unsigned do_poll(int);
+ void platform_poll(int);
+public:
+ const std::vector<PolledObject> &get_result() const { return poll_result; }
};
-PollEvent poll(Base &, PollEvent);
-PollEvent poll(Base &, PollEvent, const Time::TimeDelta &);
+PollEvent poll(EventObject &, PollEvent);
+PollEvent poll(EventObject &, PollEvent, const Time::TimeDelta &);
} // namespace IO
} // namespace Msp