From: Mikko Rasa Date: Wed, 30 May 2012 17:38:40 +0000 (+0000) Subject: Use an enum rather than plain integers to distinguish Console instances X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=33bfa130254370803efc6882a29a0f0c9f2b6d28;p=libs%2Fcore.git Use an enum rather than plain integers to distinguish Console instances --- diff --git a/source/io/console.cpp b/source/io/console.cpp index 4d686db..447377b 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -12,7 +12,19 @@ using namespace std; namespace { -#ifndef WIN32 +#ifdef WIN32 +DWORD stream_to_sys(Console::Stream stream) +{ + switch(stream) + { + case INPUT: return STD_INPUT_HANDLE; + case OUTPUT: return STD_OUTPUT_HANDLE; + case ERROR: return STD_ERROR_HANDLE; + default: throw invalid_argument("stream_to_sys"); + } +} + +#else termios orig_attr; #endif @@ -21,35 +33,28 @@ termios orig_attr; namespace Msp { namespace IO { -Console::Console(unsigned n) +Console::Console(Stream s): + stream(s) { - if(n>2) - throw invalid_argument("Console::Console"); - - mode = (n==0 ? M_READ : M_WRITE); + mode = (stream==INPUT ? M_READ : M_WRITE); #ifdef WIN32 - switch(n) - { - case 0: *handle = GetStdHandle(STD_INPUT_HANDLE); break; - case 1: *handle = GetStdHandle(STD_OUTPUT_HANDLE); break; - case 2: *handle = GetStdHandle(STD_ERROR_HANDLE); break; - } + *handle = GetStdHandle(stream_to_sys(stream)); #else - *handle = n; + *handle = stream; - if(n==0) + if(stream==INPUT) tcgetattr(*handle, &orig_attr); #endif - if(n==0) + if(stream==INPUT) set_events(P_INPUT); } Console::~Console() { #ifndef WIN32 - if(handle==0) + if(stream==INPUT) tcsetattr(*handle, TCSADRAIN, &orig_attr); #endif } @@ -133,25 +138,25 @@ unsigned Console::do_read(char *buf, unsigned len) return ret; } -Console &Console::instance(unsigned n) +Console &Console::instance(Stream s) { - static Console in(0); - static Console out(1); - static Console err(2); + static Console in(INPUT); + static Console out(OUTPUT); + static Console err(ERROR); - switch(n) + switch(s) { - case 0: return in; - case 1: return out; - case 2: return err; + case INPUT: return in; + case OUTPUT: return out; + case ERROR: return err; } throw invalid_argument("Console::instance"); } -Console &cin = Console::instance(0); -Console &cout = Console::instance(1); -Console &cerr = Console::instance(2); +Console &cin = Console::instance(Console::INPUT); +Console &cout = Console::instance(Console::OUTPUT); +Console &cerr = Console::instance(Console::ERROR); } // namespace IO } // namespace Msp diff --git a/source/io/console.h b/source/io/console.h index 6d6c788..5b4158f 100644 --- a/source/io/console.h +++ b/source/io/console.h @@ -14,10 +14,19 @@ instead. */ class Console: public EventObject { +public: + enum Stream + { + INPUT = 0, + OUTPUT = 1, + ERROR = 2 + }; + private: + Stream stream; Handle handle; - Console(unsigned); + Console(Stream); public: ~Console(); @@ -44,7 +53,7 @@ protected: public: virtual const Handle &get_event_handle() { return handle; } - static Console &instance(unsigned); + static Console &instance(Stream); }; extern Console &cin;