67 lines
917 B
C++
67 lines
917 B
C++
|
#include "direction.hh"
|
||
|
#include "posexcept.hh"
|
||
|
|
||
|
|
||
|
|
||
|
/* *** Constructors *** */
|
||
|
|
||
|
|
||
|
Direction::Direction(const char source)
|
||
|
{
|
||
|
direction = source ;
|
||
|
assert_valid() ;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/* *** Operations *** */
|
||
|
|
||
|
|
||
|
inline void Direction::assert_valid() const
|
||
|
{
|
||
|
if (! is_valid())
|
||
|
throw bad_direction(direction) ;
|
||
|
}
|
||
|
|
||
|
|
||
|
inline bool Direction::is_valid() const
|
||
|
{
|
||
|
return direction > 0 && direction <= 4 ;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/* *** Operators *** */
|
||
|
|
||
|
|
||
|
const Direction& Direction::operator=(const Direction &source)
|
||
|
{
|
||
|
direction = source.direction ;
|
||
|
return *this ;
|
||
|
}
|
||
|
|
||
|
|
||
|
const Direction& Direction::operator=(const char source)
|
||
|
{
|
||
|
direction = source ;
|
||
|
assert_valid() ;
|
||
|
return *this ;
|
||
|
}
|
||
|
|
||
|
|
||
|
Direction::operator std::string() const
|
||
|
{
|
||
|
switch (direction)
|
||
|
{
|
||
|
case north:
|
||
|
return "north" ;
|
||
|
case east:
|
||
|
return "east" ;
|
||
|
case south:
|
||
|
return "south" ;
|
||
|
case west:
|
||
|
return "west" ;
|
||
|
}
|
||
|
return "Bad direction!" ;
|
||
|
}
|