owlps/owlps-positioner/direction.cc

87 lines
1.6 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS) project.
* It is subject to the copyright notice and license terms in the
* COPYRIGHT.t2t file found in the top-level directory of this
* distribution and at
* https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
* No part of the OwlPS Project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
* distributed along with this file, either separately or by replacing
* this notice by the COPYRIGHT.t2t file's contents.
*/
#include "direction.hh"
#include "posexcept.hh"
/* *** Constructors *** */
Direction::Direction(const uint_fast8_t 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 *** */
Direction& Direction::operator=(const Direction &source)
{
direction = source.direction ;
return *this ;
}
Direction& Direction::operator=(const uint_fast8_t source)
{
direction = source ;
assert_valid() ;
return *this ;
}
Direction::operator bool() const
{
return is_valid() ;
}
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!" ;
}