#ifndef EXT_STREAM_INCLUDED
#define EXT_STREAM_INCLUDED

namespace ext {

class stream_base
{
	public:
		enum state
		{
			goodbit=0,
			failbit=1,
			eofbit=2,
			badbit=4
		};
		
	private:
		int m_state;
		int m_flags;
		
	public:
		stream_base() { m_flags=0; clear(goodbit); }
		virtual ~stream_base() { }
		
		void clear(state s) { m_state=s; }
		
		int setstate(state s)
		{
			int old=m_state;
			m_state|=s;
			return old;
		}
		
		int rdstate() const { return m_state; }
		
		bool good() const { return rdstate() == goodbit; }
		bool bad() const { return (rdstate() & badbit) > 0; }
		bool fail() const { return ((rdstate() & (failbit | badbit))!=0); }
		bool eof() const { return (rdstate() & eofbit) > 0; }
		
		int flags() const	{	return m_flags;	}

		int flags(int newflags)
		{
			int old=m_flags;
			m_flags = newflags;
			return old;
		}

		int setf(int newflags)
		{
			int old=m_flags;
			m_flags|=newflags;
			return old;
		}
	
		void unsetf(int mask)	{	m_flags&=~mask;	}
};

}

#endif // !defined(EXT_STREAM_INCLUDED)