]> git.tdb.fi Git - libs/core.git/commitdiff
Separate event-related stuff from Base
authorMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 14:12:38 +0000 (17:12 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 14:57:16 +0000 (17:57 +0300)
File objects will no longer provide events; it never made much sense anyway

17 files changed:
source/io/base.cpp
source/io/base.h
source/io/buffered.cpp
source/io/buffered.h
source/io/console.h
source/io/eventdispatcher.cpp
source/io/eventdispatcher.h
source/io/eventobject.cpp [new file with mode: 0644]
source/io/eventobject.h [new file with mode: 0644]
source/io/file.cpp
source/io/file.h
source/io/memory.cpp
source/io/memory.h
source/io/pipe.h
source/io/poll.cpp
source/io/poll.h
source/io/serial.h

index 48de3ee56ca8e3fc304f5d4caca2fcf8a57b8a9a..926440a9c4575da477b37e4ad60eea3593ef86a4 100644 (file)
@@ -8,7 +8,6 @@ namespace IO {
 
 Base::Base():
        mode(M_READ),
 
 Base::Base():
        mode(M_READ),
-       events(P_NONE),
        eof_flag(false)
 { }
 
        eof_flag(false)
 { }
 
@@ -43,19 +42,5 @@ int Base::get()
        return static_cast<unsigned char>(c);
 }
 
        return static_cast<unsigned char>(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
 } // namespace IO
 } // namespace Msp
index df63b5b1ed3ed2d5cfbd4797bc319919fa110794..8e4d4a547243ec86a867c6fce4c2ba8122c8f11b 100644 (file)
@@ -8,8 +8,6 @@
 namespace Msp {
 namespace IO {
 
 namespace Msp {
 namespace IO {
 
-struct Handle;
-
 /**
 Common interface for all I/O objects.
 
 /**
 Common interface for all I/O objects.
 
@@ -19,10 +17,6 @@ leaving stale pointers in an EventDispatcher.
 class Base
 {
 public:
 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<void> signal_data_available;
-
        /** Emitted when there is no more data to be read. */
        sigc::signal<void> signal_end_of_file;
 
        /** Emitted when there is no more data to be read. */
        sigc::signal<void> signal_end_of_file;
 
@@ -33,17 +27,12 @@ public:
        /** Emitted when the I/O object has closed. */
        sigc::signal<void> signal_closed;
 
        /** Emitted when the I/O object has closed. */
        sigc::signal<void> signal_closed;
 
-       /** Emitted when the mask of interesting events changes.  Mainly for use by
-       EventDispatcher. */
-       sigc::signal<void, PollEvent> signal_events_changed;
-
        /** Emitted when the object is deleted.  Mainly for use by
        EventDispatcher. */
        sigc::signal<void> signal_deleted;
 
 protected:
        Mode mode;
        /** Emitted when the object is deleted.  Mainly for use by
        EventDispatcher. */
        sigc::signal<void> signal_deleted;
 
 protected:
        Mode mode;
-       PollEvent events;
        bool eof_flag;
 
        Base();
        bool eof_flag;
 
        Base();
@@ -96,24 +85,6 @@ public:
 
        /** Returns the end-of-file flag. */
        bool eof() const { return eof_flag; }
 
        /** 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
 };
 
 } // namespace IO
index b2d48e660a56f337334db1f2f2decc8dbee96a86..be4c8d47c8bc263d516846c4053018b9696f2824 100644 (file)
@@ -180,10 +180,5 @@ unsigned Buffered::get_current_size() const
        return end-begin;
 }
 
        return end-begin;
 }
 
-const Handle &Buffered::get_event_handle()
-{
-       throw logic_error("Buffered doesn't support events");
-}
-
 } // namespace IO
 } // namespace Msp
 } // namespace IO
 } // namespace Msp
index 1de54d202456546cecf9604c01d9a766b061648d..095be0f01625eaac5672278a7f20ae724268b227 100644 (file)
@@ -36,8 +36,6 @@ private:
 public:
        Mode get_current_op() const { return cur_op; }
        unsigned get_current_size() const;
 public:
        Mode get_current_op() const { return cur_op; }
        unsigned get_current_size() const;
-
-       virtual const Handle &get_event_handle();
 };
 
 } // namespace IO
 };
 
 } // namespace IO
index 54774323558a905d8dbe416114d932fd5e279d83..6d6c788780f70887de6d0571cb15ece611dfaf0f 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef MSP_IO_CONSOLE_H_
 #define MSP_IO_CONSOLE_H_
 
 #ifndef MSP_IO_CONSOLE_H_
 #define MSP_IO_CONSOLE_H_
 
-#include "base.h"
+#include "eventobject.h"
 #include "handle.h"
 
 namespace Msp {
 #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.
 */
 be instantiated directly - use one of the cin, cout and cerr references
 instead.
 */
-class Console: public Base
+class Console: public EventObject
 {
 private:
        Handle handle;
 {
 private:
        Handle handle;
index 1583597ee067db4e73b81ecded2b4a609b23a0a7..8660b5b29562029daef671c892a325db51dcd783 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/time/units.h>
 #include "base.h"
 #include "eventdispatcher.h"
 #include <msp/time/units.h>
 #include "base.h"
 #include "eventdispatcher.h"
+#include "eventobject.h"
 #include "poll.h"
 
 namespace Msp {
 #include "poll.h"
 
 namespace Msp {
@@ -9,7 +10,7 @@ namespace IO {
 EventDispatcher::EventDispatcher()
 { }
 
 EventDispatcher::EventDispatcher()
 { }
 
-void EventDispatcher::add(Base &obj)
+void EventDispatcher::add(EventObject &obj)
 {
        SlotMap::iterator i = objects.find(&obj);
        if(i==objects.end())
 {
        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())
 {
        SlotMap::iterator i = objects.find(&obj);
        if(i!=objects.end())
@@ -54,12 +55,12 @@ void EventDispatcher::tick(const Time::TimeDelta &dt)
                dispatch();
 }
 
                dispatch();
 }
 
-void EventDispatcher::object_events_changed(PollEvent ev, Base *obj)
+void EventDispatcher::object_events_changed(PollEvent ev, EventObject *obj)
 {
        poller.set_object(*obj, ev);
 }
 
 {
        poller.set_object(*obj, ev);
 }
 
-void EventDispatcher::object_deleted(Base *obj)
+void EventDispatcher::object_deleted(EventObject *obj)
 {
        remove(*obj);
 }
 {
        remove(*obj);
 }
index 7e89d4739e845f0e0c180845ffedb43184eefd9f..fec8ded63049ba28911b2e92ef6257accec162e0 100644 (file)
@@ -17,14 +17,14 @@ class EventDispatcher: public sigc::trackable
 private:
        struct Slot
        {
 private:
        struct Slot
        {
-               Base *obj;
+               EventObject *obj;
                sigc::connection evch_conn;
                sigc::connection del_conn;
 
                sigc::connection evch_conn;
                sigc::connection del_conn;
 
-               Slot(Base *o): obj(o) { }
+               Slot(EventObject *o): obj(o) { }
        };
 
        };
 
-       typedef std::map<Base *, Slot> SlotMap;
+       typedef std::map<EventObject *, Slot> SlotMap;
 
        Poller poller;
        SlotMap objects;
 
        Poller poller;
        SlotMap objects;
@@ -32,8 +32,8 @@ private:
 public:
        EventDispatcher();
 
 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. */
 
        /** 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 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();
 };
 
        void dispatch();
 };
 
diff --git a/source/io/eventobject.cpp b/source/io/eventobject.cpp
new file mode 100644 (file)
index 0000000..333d112
--- /dev/null
@@ -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 (file)
index 0000000..b8423c9
--- /dev/null
@@ -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<void> signal_data_available;
+
+       /** Emitted when the mask of interesting events changes.  Mainly for use by
+       EventDispatcher. */
+       sigc::signal<void, PollEvent> 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
index 25e3b4af115855a6b332e99ebcf947599590cbc8..c9e5d4978c5255e418c12d7794146e2d062f0667 100644 (file)
@@ -82,8 +82,6 @@ File::File(const string &fn, Mode m, CreateMode cm)
                        throw system_error(format("open(%s)", fn), err);
        }
 #endif
                        throw system_error(format("open(%s)", fn), err);
        }
 #endif
-
-       set_events(P_INPUT);
 }
 
 File::~File()
 }
 
 File::~File()
@@ -96,8 +94,6 @@ void File::close()
        if(!handle)
                return;
 
        if(!handle)
                return;
 
-       set_events(P_NONE);
-
        signal_flush_required.emit();
 
 #ifdef WIN32
        signal_flush_required.emit();
 
 #ifdef WIN32
index 57dc6831449416a0a0e9af75626ac1702757c8ce..ed0a3f8336ac42ff0237e2cfcb4e95d2f7b97d07 100644 (file)
@@ -59,8 +59,6 @@ public:
        virtual unsigned seek(int, SeekType);
        virtual unsigned tell() const;
 
        virtual unsigned seek(int, SeekType);
        virtual unsigned tell() const;
 
-       virtual const Handle &get_event_handle() { return handle; }
-
 private:
        void check_access(Mode) const;
 };
 private:
        void check_access(Mode) const;
 };
index f89d9fc894c26d207e6e3b8300d9f9dc99c6f7ae..af6ef3d44ce386ce2bd7d2caaf8b1a8de24a9fa7 100644 (file)
@@ -107,11 +107,6 @@ unsigned Memory::seek(int off, SeekType type)
        return pos-begin;
 }
 
        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))
 void Memory::check_mode(Mode m) const
 {
        if(m==M_WRITE && !(mode&M_WRITE))
index 870edcf0adf3f6d506a71de57a6c0be52dacc51e..ebd57eda09d1af492d32df338737edac7eb98fc1 100644 (file)
@@ -31,8 +31,6 @@ public:
        virtual unsigned seek(int, SeekType);
        virtual unsigned tell() const { return pos-begin; }
 
        virtual unsigned seek(int, SeekType);
        virtual unsigned tell() const { return pos-begin; }
 
-       virtual const Handle &get_event_handle();
-
 private:
        void check_mode(Mode) const;
 };
 private:
        void check_mode(Mode) const;
 };
index a2a7d0f99f53ec6fafb69752c2f60886d5365a93..eb77e6ab830d0d50cfa5bbedc16a29b7bbfbe4d2 100644 (file)
@@ -1,13 +1,13 @@
 #ifndef MSP_IO_PIPE_H_
 #define MSP_IO_PIPE_H_
 
 #ifndef MSP_IO_PIPE_H_
 #define MSP_IO_PIPE_H_
 
-#include "base.h"
+#include "eventobject.h"
 #include "handle.h"
 
 namespace Msp {
 namespace IO {
 
 #include "handle.h"
 
 namespace Msp {
 namespace IO {
 
-class Pipe: public Base
+class Pipe: public EventObject
 {
 private:
        struct Private;
 {
 private:
        struct Private;
index 1ca0987ac10fd0ce7795f5649f667ffc6f178c42..9697ebfc3aa2094225325ea0aebcadb75d19953d 100644 (file)
@@ -6,7 +6,7 @@
 #include <msp/core/systemerror.h>
 #include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/core/systemerror.h>
 #include <msp/strings/format.h>
 #include <msp/time/units.h>
-#include "base.h"
+#include "eventobject.h"
 #include "handle.h"
 #include "handle_private.h"
 #include "poll.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;
 }
 
        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)
 {
 #ifdef WIN32
        if(timeout<0)
@@ -107,7 +107,7 @@ Poller::Poller():
        objs_changed(false)
 { }
 
        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)
 {
        // 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);
 }
 
 {
        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<Time::zero)
                throw invalid_argument("poll");
 {
        if(timeout<Time::zero)
                throw invalid_argument("poll");
index f59b499ddbc3bad3702c42a56507c32969ddac84..78e6088f8bfe0f5289864550136cedab5154df84 100644 (file)
@@ -9,7 +9,7 @@
 namespace Msp {
 namespace IO {
 
 namespace Msp {
 namespace IO {
 
-class Base;
+class EventObject;
 
 enum PollEvent
 {
 
 enum PollEvent
 {
@@ -35,15 +35,15 @@ class Poller
 public:
        struct Slot
        {
 public:
        struct Slot
        {
-               Base *object;
+               EventObject *object;
                PollEvent events;
 
                PollEvent events;
 
-               Slot(Base *o, PollEvent e): object(o), events(e) { }
+               Slot(EventObject *o, PollEvent e): object(o), events(e) { }
        };
 
        typedef std::list<Slot> SlotList;
 private:
        };
 
        typedef std::list<Slot> SlotList;
 private:
-       typedef std::map<Base *, PollEvent> EventMap;
+       typedef std::map<EventObject *, PollEvent> EventMap;
 
        struct Private;
 
 
        struct Private;
 
@@ -55,7 +55,7 @@ private:
 public:
        Poller();
 
 public:
        Poller();
 
-       void set_object(Base &, PollEvent);
+       void set_object(EventObject &, PollEvent);
        int poll();
        int poll(const Time::TimeDelta &);
 private:
        int poll();
        int poll(const Time::TimeDelta &);
 private:
@@ -65,8 +65,8 @@ public:
        const SlotList &get_result() const { return poll_result; }
 };
 
        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
 
 } // namespace IO
 } // namespace Msp
index dba62fbf955e5db860f763c2a233cb3e130d75fe..98b16674b464d098f7c051e72b5837c68d1fa797 100644 (file)
@@ -1,13 +1,13 @@
 #ifndef MSP_IO_SERIAL_H_
 #define MSP_IO_SERIAL_H_
 
 #ifndef MSP_IO_SERIAL_H_
 #define MSP_IO_SERIAL_H_
 
-#include "base.h"
+#include "eventobject.h"
 #include "handle.h"
 
 namespace Msp {
 namespace IO {
 
 #include "handle.h"
 
 namespace Msp {
 namespace IO {
 
-class Serial: public Base
+class Serial: public EventObject
 {
 public:
        enum Parity
 {
 public:
        enum Parity