/* ** $Id: counter,v 1.1 2003/01/21 05:59:00 phf Exp $ ** ** Copyright (c) 2003 by Peter H. Froehlich . ** Freely distributable for educational purposes. */ #ifndef _CS14_COUNTER #define _CS14_COUNTER #include namespace cs14 { /* ** Interface class for ADT Counter. ** ** Counters have an integer value and can be restricted to a ** certain range. */ class Counter { public: /* ** Constructor. ** ** Create new counters according to the ADT specification. ** ** new: Integer x Integer -/-> Counter ** lower and upper as given, lower <= upper must hold, ** new counter starts at 0 (i.e. get( new() ) == 0). ** ** We cannot declare the constructor here, it must be added ** in each implementation class. */ /* ** Observers. ** ** lower: Counter ---> Integer ** lower bound ** upper: Counter ---> Integer ** upper bound ** get: Counter ---> Integer ** current value */ virtual int lower() const =0; virtual int upper() const =0; virtual int get() const =0; /* ** Modifiers. ** ** set: Counter x Integer -/-> Counter ** set current value, must be in range ** inc: Counter -/-> Counter ** increase current value, result must be in range, ** increase by 1 (i.e. get( inc( c ) ) == get(c)+1). ** dec: Counter -/-> Counter ** decrease current value, result must be in range, ** decrease by 1 (i.e. get( dec( c ) ) == get(c)-1). */ virtual void set( int i ) throw (std::domain_error) =0; virtual void inc() throw (std::domain_error) =0; virtual void dec() throw (std::domain_error) =0; /* ** More idiomatic ways to express inc and dec. They can ** be implemented here since they just use the above. */ virtual Counter& operator++() throw (std::domain_error) { inc(); return *this; } virtual Counter& operator--() throw (std::domain_error) { dec(); return *this; } }; // Counter } // namespace cs14 #endif /* _CS14_COUNTER */