From c21ab7e49852585df01b4cc19599e25a918b581b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 11 Jun 2011 17:12:38 +0300 Subject: [PATCH] Separate event-related stuff from Base File objects will no longer provide events; it never made much sense anyway --- source/io/base.cpp | 15 ---------- source/io/base.h | 29 ------------------- source/io/buffered.cpp | 5 ---- source/io/buffered.h | 2 -- source/io/console.h | 4 +-- source/io/eventdispatcher.cpp | 9 +++--- source/io/eventdispatcher.h | 14 ++++----- source/io/eventobject.cpp | 24 ++++++++++++++++ source/io/eventobject.h | 54 +++++++++++++++++++++++++++++++++++ source/io/file.cpp | 4 --- source/io/file.h | 2 -- source/io/memory.cpp | 5 ---- source/io/memory.h | 2 -- source/io/pipe.h | 4 +-- source/io/poll.cpp | 10 +++---- source/io/poll.h | 14 ++++----- source/io/serial.h | 4 +-- 17 files changed, 108 insertions(+), 93 deletions(-) create mode 100644 source/io/eventobject.cpp create mode 100644 source/io/eventobject.h diff --git a/source/io/base.cpp b/source/io/base.cpp index 48de3ee..926440a 100644 --- a/source/io/base.cpp +++ b/source/io/base.cpp @@ -8,7 +8,6 @@ namespace IO { Base::Base(): mode(M_READ), - events(P_NONE), eof_flag(false) { } @@ -43,19 +42,5 @@ int Base::get() return static_cast(c); } -void Base::set_events(PollEvent e) -{ - events = e; - signal_events_changed.emit(events); -} - -void Base::event(PollEvent ev) -{ - if(ev&P_INPUT) - signal_data_available.emit(); - - on_event(ev); -} - } // namespace IO } // namespace Msp diff --git a/source/io/base.h b/source/io/base.h index df63b5b..8e4d4a5 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -8,8 +8,6 @@ namespace Msp { namespace IO { -struct Handle; - /** Common interface for all I/O objects. @@ -19,10 +17,6 @@ leaving stale pointers in an EventDispatcher. class Base { public: - /** Emitted when there is data available for reading. If all data is not - read, the signal is emitted again immediately. */ - sigc::signal signal_data_available; - /** Emitted when there is no more data to be read. */ sigc::signal signal_end_of_file; @@ -33,17 +27,12 @@ public: /** Emitted when the I/O object has closed. */ sigc::signal signal_closed; - /** Emitted when the mask of interesting events changes. Mainly for use by - EventDispatcher. */ - sigc::signal signal_events_changed; - /** Emitted when the object is deleted. Mainly for use by EventDispatcher. */ sigc::signal signal_deleted; protected: Mode mode; - PollEvent events; bool eof_flag; Base(); @@ -96,24 +85,6 @@ public: /** Returns the end-of-file flag. */ bool eof() const { return eof_flag; } - -protected: - void set_events(PollEvent); - -public: - /** Returns a mask of the currently interesting events. Used by - EventDispatcher. */ - PollEvent get_events() const { return events; } - - /** Returns a handle for polling. Should throw if the object does not have - an event handle. */ - virtual const Handle &get_event_handle() = 0; - - /** Notifies the object of an event. Used by EventDispatcher. */ - void event(PollEvent); - -protected: - virtual void on_event(PollEvent) { } }; } // namespace IO diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index b2d48e6..be4c8d4 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -180,10 +180,5 @@ unsigned Buffered::get_current_size() const return end-begin; } -const Handle &Buffered::get_event_handle() -{ - throw logic_error("Buffered doesn't support events"); -} - } // namespace IO } // namespace Msp diff --git a/source/io/buffered.h b/source/io/buffered.h index 1de54d2..095be0f 100644 --- a/source/io/buffered.h +++ b/source/io/buffered.h @@ -36,8 +36,6 @@ private: public: Mode get_current_op() const { return cur_op; } unsigned get_current_size() const; - - virtual const Handle &get_event_handle(); }; } // namespace IO diff --git a/source/io/console.h b/source/io/console.h index 5477432..6d6c788 100644 --- a/source/io/console.h +++ b/source/io/console.h @@ -1,7 +1,7 @@ #ifndef MSP_IO_CONSOLE_H_ #define MSP_IO_CONSOLE_H_ -#include "base.h" +#include "eventobject.h" #include "handle.h" namespace Msp { @@ -12,7 +12,7 @@ Provides access to standard input, output and error streams. This class can't be instantiated directly - use one of the cin, cout and cerr references instead. */ -class Console: public Base +class Console: public EventObject { private: Handle handle; diff --git a/source/io/eventdispatcher.cpp b/source/io/eventdispatcher.cpp index 1583597..8660b5b 100644 --- a/source/io/eventdispatcher.cpp +++ b/source/io/eventdispatcher.cpp @@ -1,6 +1,7 @@ #include #include "base.h" #include "eventdispatcher.h" +#include "eventobject.h" #include "poll.h" namespace Msp { @@ -9,7 +10,7 @@ namespace IO { EventDispatcher::EventDispatcher() { } -void EventDispatcher::add(Base &obj) +void EventDispatcher::add(EventObject &obj) { SlotMap::iterator i = objects.find(&obj); if(i==objects.end()) @@ -23,7 +24,7 @@ void EventDispatcher::add(Base &obj) } } -void EventDispatcher::remove(Base &obj) +void EventDispatcher::remove(EventObject &obj) { SlotMap::iterator i = objects.find(&obj); if(i!=objects.end()) @@ -54,12 +55,12 @@ void EventDispatcher::tick(const Time::TimeDelta &dt) dispatch(); } -void EventDispatcher::object_events_changed(PollEvent ev, Base *obj) +void EventDispatcher::object_events_changed(PollEvent ev, EventObject *obj) { poller.set_object(*obj, ev); } -void EventDispatcher::object_deleted(Base *obj) +void EventDispatcher::object_deleted(EventObject *obj) { remove(*obj); } diff --git a/source/io/eventdispatcher.h b/source/io/eventdispatcher.h index 7e89d47..fec8ded 100644 --- a/source/io/eventdispatcher.h +++ b/source/io/eventdispatcher.h @@ -17,14 +17,14 @@ class EventDispatcher: public sigc::trackable private: struct Slot { - Base *obj; + EventObject *obj; sigc::connection evch_conn; sigc::connection del_conn; - Slot(Base *o): obj(o) { } + Slot(EventObject *o): obj(o) { } }; - typedef std::map SlotMap; + typedef std::map SlotMap; Poller poller; SlotMap objects; @@ -32,8 +32,8 @@ private: public: EventDispatcher(); - void add(Base &); - void remove(Base &); + void add(EventObject &); + void remove(EventObject &); /** Checks for and dispatches events. If there are no events available, blocks until there are. */ @@ -44,8 +44,8 @@ public: void tick(const Time::TimeDelta &); private: - void object_events_changed(PollEvent, Base *); - void object_deleted(Base *); + void object_events_changed(PollEvent, EventObject *); + void object_deleted(EventObject *); void dispatch(); }; diff --git a/source/io/eventobject.cpp b/source/io/eventobject.cpp new file mode 100644 index 0000000..333d112 --- /dev/null +++ b/source/io/eventobject.cpp @@ -0,0 +1,24 @@ +#include "eventobject.h" + +namespace Msp { +namespace IO { + +EventObject::EventObject(): + events(P_NONE) +{ } + +void EventObject::set_events(PollEvent e) +{ + events = e; + signal_events_changed.emit(events); +} + +void EventObject::event(PollEvent ev) +{ + if(ev&P_INPUT) + signal_data_available.emit(); + + on_event(ev); +} +} // namespace IO +} // namespace Msp diff --git a/source/io/eventobject.h b/source/io/eventobject.h new file mode 100644 index 0000000..b8423c9 --- /dev/null +++ b/source/io/eventobject.h @@ -0,0 +1,54 @@ +#ifndef MSP_IO_EVENTOBJECT_H_ +#define MSP_IO_EVENTOBJECT_H_ + +#include "base.h" + +namespace Msp { +namespace IO { + +struct Handle; + +/** +Interface class for objects that can provide event-based I/O. These objects +can be fed to the various poll functions in poll.h, or added to an +EventDispatcher to generate event signals. +*/ +class EventObject: public Base +{ +public: + /** Emitted when there is data available for reading. If all data is not + read, the signal is emitted again immediately. */ + sigc::signal signal_data_available; + + /** Emitted when the mask of interesting events changes. Mainly for use by + EventDispatcher. */ + sigc::signal signal_events_changed; + +private: + PollEvent events; + +protected: + EventObject(); + + void set_events(PollEvent); +public: + /** Returns a mask of the currently interesting events. Used by + EventDispatcher. */ + PollEvent get_events() const { return events; } + + /** Returns a handle for polling. */ + virtual const Handle &get_event_handle() = 0; + + /** Notifies the object of an event. Used by EventDispatcher. */ + void event(PollEvent); + +protected: + /** Called when an event occurs. Derived classes can implement this to + process events. */ + virtual void on_event(PollEvent) { } +}; + +} // namespace IO +} // namespace Msp + +#endif diff --git a/source/io/file.cpp b/source/io/file.cpp index 25e3b4a..c9e5d49 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -82,8 +82,6 @@ File::File(const string &fn, Mode m, CreateMode cm) throw system_error(format("open(%s)", fn), err); } #endif - - set_events(P_INPUT); } File::~File() @@ -96,8 +94,6 @@ void File::close() if(!handle) return; - set_events(P_NONE); - signal_flush_required.emit(); #ifdef WIN32 diff --git a/source/io/file.h b/source/io/file.h index 57dc683..ed0a3f8 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -59,8 +59,6 @@ public: virtual unsigned seek(int, SeekType); virtual unsigned tell() const; - virtual const Handle &get_event_handle() { return handle; } - private: void check_access(Mode) const; }; diff --git a/source/io/memory.cpp b/source/io/memory.cpp index f89d9fc..af6ef3d 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -107,11 +107,6 @@ unsigned Memory::seek(int off, SeekType type) return pos-begin; } -const Handle &Memory::get_event_handle() -{ - throw logic_error("Memory doesn't support events"); -} - void Memory::check_mode(Mode m) const { if(m==M_WRITE && !(mode&M_WRITE)) diff --git a/source/io/memory.h b/source/io/memory.h index 870edcf..ebd57ed 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -31,8 +31,6 @@ public: virtual unsigned seek(int, SeekType); virtual unsigned tell() const { return pos-begin; } - virtual const Handle &get_event_handle(); - private: void check_mode(Mode) const; }; diff --git a/source/io/pipe.h b/source/io/pipe.h index a2a7d0f..eb77e6a 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -1,13 +1,13 @@ #ifndef MSP_IO_PIPE_H_ #define MSP_IO_PIPE_H_ -#include "base.h" +#include "eventobject.h" #include "handle.h" namespace Msp { namespace IO { -class Pipe: public Base +class Pipe: public EventObject { private: struct Private; diff --git a/source/io/poll.cpp b/source/io/poll.cpp index 1ca0987..9697ebf 100644 --- a/source/io/poll.cpp +++ b/source/io/poll.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "base.h" +#include "eventobject.h" #include "handle.h" #include "handle_private.h" #include "poll.h" @@ -57,7 +57,7 @@ inline PollEvent poll_event_from_sys(int event) return result; } -inline PollEvent do_poll(Base &obj, PollEvent pe, int timeout) +inline PollEvent do_poll(EventObject &obj, PollEvent pe, int timeout) { #ifdef WIN32 if(timeout<0) @@ -107,7 +107,7 @@ Poller::Poller(): objs_changed(false) { } -void Poller::set_object(Base &obj, PollEvent ev) +void Poller::set_object(EventObject &obj, PollEvent ev) { // Verify that the object has an event handle if(ev) @@ -218,12 +218,12 @@ int Poller::do_poll(int timeout) } -PollEvent poll(Base &obj, PollEvent pe) +PollEvent poll(EventObject &obj, PollEvent pe) { return do_poll(obj, pe, -1); } -PollEvent poll(Base &obj, PollEvent pe, const Time::TimeDelta &timeout) +PollEvent poll(EventObject &obj, PollEvent pe, const Time::TimeDelta &timeout) { if(timeout SlotList; private: - typedef std::map EventMap; + typedef std::map EventMap; struct Private; @@ -55,7 +55,7 @@ private: public: Poller(); - void set_object(Base &, PollEvent); + void set_object(EventObject &, PollEvent); int poll(); int poll(const Time::TimeDelta &); private: @@ -65,8 +65,8 @@ public: const SlotList &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 diff --git a/source/io/serial.h b/source/io/serial.h index dba62fb..98b1667 100644 --- a/source/io/serial.h +++ b/source/io/serial.h @@ -1,13 +1,13 @@ #ifndef MSP_IO_SERIAL_H_ #define MSP_IO_SERIAL_H_ -#include "base.h" +#include "eventobject.h" #include "handle.h" namespace Msp { namespace IO { -class Serial: public Base +class Serial: public EventObject { public: enum Parity -- 2.43.0