76 lines
1.0 KiB
C++
76 lines
1.0 KiB
C++
#include "direction.hh"
|
|
#include "posexcept.hh"
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
Direction::Direction(const char source):
|
|
direction(source)
|
|
{
|
|
assert_valid() ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
inline void Direction::assert_valid() const
|
|
{
|
|
if (! (is_valid() || direction == 0))
|
|
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!" ;
|
|
}
|
|
|
|
|
|
|
|
size_t hash_value(const Direction &source)
|
|
{
|
|
return boost::hash_value(source.direction) ;
|
|
}
|