From 965148b43b5b859934b7af2e8447ba1026a43a19 Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 10 Oct 2011 19:36:55 -0700 Subject: Adding the basic "Handle" structure, and adding an early version of Input. Renamed suspend() to yield(). Fixed a couple of bugs, and reorganized slightly some code. --- includes/Handle.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ includes/Input.h | 18 ++++++++++++++++ includes/Task.h | 12 ++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 includes/Handle.h create mode 100644 includes/Input.h (limited to 'includes') diff --git a/includes/Handle.h b/includes/Handle.h new file mode 100644 index 0000000..d2e1f8c --- /dev/null +++ b/includes/Handle.h @@ -0,0 +1,64 @@ +#pragma once + +#include + +namespace Balau { + +class EAgain : public GeneralException { + public: + EAgain() : GeneralException("Try Again") { } +}; + +class IO; + +class Handle { + public: + virtual ~Handle() { Assert(m_refCount == 0); } + virtual void close() throw (GeneralException) = 0; + virtual bool isClosed() = 0; + virtual bool canSeek(); + virtual bool canRead(); + virtual bool canWrite(); + virtual const char * getName() = 0; + virtual ssize_t read(void * buf, size_t count) throw (GeneralException); + virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); + virtual void rseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); + virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); + virtual off_t rtell() throw (GeneralException); + virtual off_t wtell() throw (GeneralException); + virtual off_t getSize(); + protected: + Handle() : m_refCount(0) { } + private: + void addRef() { m_refCount++; } + void delRef() { if (--m_refCount == 0) { if (!isClosed()) close(); delete this; } } + int refCount() { return m_refCount; } + friend class IO; + + int m_refCount; +}; + +class IO { + public: + template IO(T * h) { setHandle(h); } + ~IO() { m_h->delRef(); } + IO(const IO & io) { setHandle(io.m_h); } + Handle * operator->() { return m_h; } + protected: + void setHandle(Handle * h) { m_h = h; m_h->addRef(); } + private: + Handle * m_h; +}; + +class SeekableHandle : public Handle { + public: + virtual bool canSeek(); + virtual void rseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); + virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); + virtual off_t rtell() throw (GeneralException); + virtual off_t wtell() throw (GeneralException); + private: + off_t m_wOffset, m_rOffset; +}; + +}; diff --git a/includes/Input.h b/includes/Input.h new file mode 100644 index 0000000..418b018 --- /dev/null +++ b/includes/Input.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace Balau { + +class Input : public SeekableHandle { + public: + Input(const char * fname) throw (GeneralException); + virtual void close() throw (GeneralException); + virtual bool isClosed(); + virtual const char * getName(); + private: + int m_fd; + String m_name; +}; + +}; diff --git a/includes/Task.h b/includes/Task.h index f0c1bbb..8a504c7 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -44,6 +44,15 @@ class TaskEvent : public BaseEvent { Task * m_taskWaited; }; +class Custom : public BaseEvent { + public: + void doSignal() { BaseEvent::doSignal(); ev_break(m_loop, EVBREAK_ALL); } + protected: + virtual void gotOwner(Task * task); + private: + struct ev_loop * m_loop; +}; + }; class Task { @@ -60,9 +69,10 @@ class Task { virtual const char * getName() = 0; Status getStatus() { return m_status; } static Task * getCurrentTask(); + static void yield(Events::BaseEvent * evt) { Task * t = getCurrentTask(); t->waitFor(evt); t->yield(); } TaskMan * getTaskMan() { return m_taskMan; } protected: - void suspend(); + void yield(); virtual void Do() = 0; void waitFor(Events::BaseEvent * event); private: -- cgit v1.2.3