#ifndef PRINTABLEH #define PRINTABLEH #include #include //for ostringstream #include //for std::isprint #include //for UCHAR_MAX #include "except.h" using namespace std; class printable { char c; static bool isprint(char c) throw () { return std::isprint(static_cast(c)) != 0; } static bool isprint(int i) throw () { return i >= 0 && i <= UCHAR_MAX && std::isprint(i) != 0; } static unsigned code(char c) throw () { return static_cast(c); } static int code(int i) throw () {return i;} public: template //T must be char or int. printable& operator=(T t) throw (except); template //T must be char or int. printable(T t) throw (except) {*this = t;} operator char() const throw () {return c;} }; template printable& printable::operator=(T t) throw (except) { if (!isprint(t)) { ostringstream ost; ost << "character code " << code(t) << " is not printable"; throw except(ost); } c = t; return *this; } inline printable& operator+=(printable& p, int i) throw (except) { return p = p + i; //return p.operator=(p.operator char() + i); } inline printable& operator-=(printable& p, int i) throw (except) { return p = p - i; } inline printable& operator++(printable& p) throw (except) {return p += 1;} inline printable& operator--(printable& p) throw (except) {return p -= 1;} inline const printable operator++(printable& p, int) throw (except) { const printable old = p; ++p; return old; } inline const printable operator--(printable& p, int) throw (except) { const printable old = p; --p; return old; } inline const printable operator+(printable p, int i) throw (except) { return p += i; } inline const printable operator+(int i, printable p) throw (except) { return p += i; } inline const printable operator-(printable p, int i) throw (except) { return p -= i; } istream& operator>>(istream& ist, printable& p) throw (except); #endif