[Positioning] Add class Direction
The new class Direction will be used to represent the direction of a calibration request. Update posexcept.hh to add new exception bad_direction, used in class Direction.
This commit is contained in:
parent
6ad31f9af4
commit
abb5536fdc
|
@ -28,12 +28,28 @@ LDFLAGS = -lm -lrt -lboost_program_options
|
|||
|
||||
# Targets
|
||||
TARGET = owlps-positioning
|
||||
OBJ = posutil.o stock.o timestamp.o point3d.o referencepoint.o \
|
||||
waypoint.o building.o area.o wifidevice.o accesspoint.o \
|
||||
mobile.o measurement.o request.o calibrationrequest.o \
|
||||
configuration.o userinterface.o input.o \
|
||||
inputcsv.o inputlogcsv.o
|
||||
OBJ_NOTEST = posexcept.o inputmedium.o
|
||||
OBJ = posutil.o \
|
||||
stock.o \
|
||||
timestamp.o \
|
||||
direction.o \
|
||||
point3d.o \
|
||||
referencepoint.o \
|
||||
waypoint.o \
|
||||
building.o \
|
||||
area.o \
|
||||
wifidevice.o \
|
||||
accesspoint.o \
|
||||
mobile.o \
|
||||
measurement.o \
|
||||
request.o \
|
||||
calibrationrequest.o \
|
||||
configuration.o \
|
||||
userinterface.o \
|
||||
input.o \
|
||||
inputcsv.o \
|
||||
inputlogcsv.o
|
||||
OBJ_NOTEST = posexcept.o \
|
||||
inputmedium.o
|
||||
INTERFACES = inputlogmedium.hh
|
||||
|
||||
TESTS_XX = $(TESTS_DIR)/tests.cc
|
||||
|
@ -67,7 +83,7 @@ accesspoint.o: wifidevice.o point3d.o
|
|||
mobile.o: wifidevice.o
|
||||
measurement.o: accesspoint.o
|
||||
request.o: timestamp.o measurement.o
|
||||
calibrationrequest.o: request.o referencepoint.o
|
||||
calibrationrequest.o: request.o referencepoint.o direction.o
|
||||
inputcsv.o: inputmedium.o request.o stock.o
|
||||
inputlogcsv.o: inputlogmedium.hh request.o
|
||||
input.o: posexcept.o
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
#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!" ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream &os, const Direction &d)
|
||||
{
|
||||
os << static_cast<int>(d) ;
|
||||
return os ;
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
#ifndef _OWLPS_POSITIONING_DIRECTION_HH_
|
||||
#define _OWLPS_POSITIONING_DIRECTION_HH_
|
||||
|
||||
#include <string>
|
||||
|
||||
/// \brief Represents a direction in which a mobile is when it sends a
|
||||
/// CalibrationRequest
|
||||
class Direction
|
||||
{
|
||||
protected:
|
||||
char direction ;
|
||||
|
||||
/** @name Operations */
|
||||
//@{
|
||||
void assert_valid(void) const ;
|
||||
bool is_valid(void) const ;
|
||||
//@}
|
||||
|
||||
public:
|
||||
enum {north = 1, east, south, west} ;
|
||||
|
||||
Direction(void): direction(0) {}
|
||||
Direction(const char source) ;
|
||||
Direction(const Direction &source): direction(source.direction) {}
|
||||
|
||||
/** @name Accessors */
|
||||
//@{
|
||||
void clear(void) ;
|
||||
//@}
|
||||
|
||||
/** @name Operators */
|
||||
//@{
|
||||
const Direction& operator=(const Direction &source) ;
|
||||
const Direction& operator=(const char source) ;
|
||||
bool operator==(const Direction &source) const ;
|
||||
bool operator!=(const Direction &source) const ;
|
||||
operator bool(void) const ;
|
||||
operator int(void) const ;
|
||||
operator std::string(void) const ;
|
||||
//@}
|
||||
|
||||
/// Displays a Direction
|
||||
friend std::ostream& operator<<(std::ostream &os,
|
||||
const Direction &d) ;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
/* *** Accessors *** */
|
||||
|
||||
|
||||
inline void Direction::clear()
|
||||
{
|
||||
direction = 0 ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* *** Operators *** */
|
||||
|
||||
|
||||
inline bool Direction::operator==(const Direction &source) const
|
||||
{
|
||||
return direction == source.direction ;
|
||||
}
|
||||
|
||||
|
||||
inline bool Direction::operator!=(const Direction &source) const
|
||||
{
|
||||
return !(*this == source) ;
|
||||
}
|
||||
|
||||
|
||||
inline Direction::operator bool() const
|
||||
{
|
||||
return is_valid() ;
|
||||
}
|
||||
|
||||
|
||||
inline Direction::operator int() const
|
||||
{
|
||||
return static_cast<int>(direction) ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // _OWLPS_POSITIONING_DIRECTION_HH_
|
|
@ -1,8 +1,23 @@
|
|||
#include "posexcept.hh"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
using namespace std ;
|
||||
|
||||
|
||||
bad_direction::bad_direction(const char _direction) throw():
|
||||
direction(_direction) {}
|
||||
|
||||
|
||||
const char* bad_direction::what() const throw()
|
||||
{
|
||||
ostringstream message ;
|
||||
message << "`" << static_cast<int>(direction)
|
||||
<< "` is not a valid direction value!" ;
|
||||
return message.str().c_str() ;
|
||||
}
|
||||
|
||||
|
||||
element_not_found::
|
||||
element_not_found(const string &_explanation) throw():
|
||||
explanation(_explanation) {}
|
||||
|
|
|
@ -5,6 +5,17 @@
|
|||
#include <string>
|
||||
|
||||
|
||||
class bad_direction: public std::exception
|
||||
{
|
||||
private:
|
||||
char direction ;
|
||||
|
||||
public:
|
||||
bad_direction(const char _direction) throw() ;
|
||||
const char* what() const throw() ;
|
||||
} ;
|
||||
|
||||
|
||||
class element_not_found: public std::exception
|
||||
{
|
||||
private:
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#include "direction.hh"
|
||||
#include "posexcept.hh"
|
||||
|
||||
class Direction_test: public CxxTest::TestSuite
|
||||
{
|
||||
public:
|
||||
|
||||
void test_constructors(void)
|
||||
{
|
||||
// Default constructor
|
||||
TS_ASSERT_THROWS_NOTHING(Direction()) ;
|
||||
Direction direction0 ;
|
||||
TS_ASSERT(! direction0) ;
|
||||
|
||||
// char constructor
|
||||
TS_ASSERT_THROWS(Direction(0), bad_direction) ;
|
||||
TS_ASSERT_THROWS(Direction(5), bad_direction) ;
|
||||
|
||||
TS_ASSERT_THROWS_NOTHING(Direction(north)) ;
|
||||
// FIXME: I wonder why north works but not Direction::north…
|
||||
TS_ASSERT_THROWS_NOTHING(Direction(east)) ;
|
||||
TS_ASSERT_THROWS_NOTHING(Direction(south)) ;
|
||||
TS_ASSERT_THROWS_NOTHING(Direction(west)) ;
|
||||
|
||||
// Copy constructor
|
||||
Direction direction1(Direction::north) ;
|
||||
Direction direction2(direction1) ;
|
||||
TS_ASSERT_EQUALS(direction1, direction2) ;
|
||||
}
|
||||
|
||||
|
||||
void test_accessors(void)
|
||||
{
|
||||
Direction direction0 ;
|
||||
Direction direction1(Direction::south) ;
|
||||
direction1.clear() ;
|
||||
TS_ASSERT(! direction1) ;
|
||||
TS_ASSERT_EQUALS(direction0, direction1) ;
|
||||
}
|
||||
|
||||
|
||||
void test_operators(void)
|
||||
{
|
||||
Direction direction1(Direction::north) ;
|
||||
Direction direction2(Direction::east) ;
|
||||
Direction direction3(Direction::south) ;
|
||||
Direction direction4(Direction::west) ;
|
||||
|
||||
// ==
|
||||
Direction direction5(Direction::north) ;
|
||||
TS_ASSERT_EQUALS(direction1, direction5) ;
|
||||
|
||||
// =
|
||||
Direction direction6 = direction5 ;
|
||||
TS_ASSERT_EQUALS(direction5, direction6) ;
|
||||
|
||||
// !=
|
||||
direction6 = direction4 ;
|
||||
TS_ASSERT_DIFFERS(direction5, direction6) ;
|
||||
|
||||
// bool
|
||||
TS_ASSERT(direction1) ;
|
||||
TS_ASSERT(direction2) ;
|
||||
TS_ASSERT(direction3) ;
|
||||
TS_ASSERT(direction4) ;
|
||||
|
||||
// int
|
||||
TS_ASSERT_EQUALS(static_cast<int>(direction1),
|
||||
static_cast<int>(Direction::north)) ;
|
||||
TS_ASSERT_EQUALS(static_cast<int>(direction2),
|
||||
static_cast<int>(Direction::east)) ;
|
||||
TS_ASSERT_EQUALS(static_cast<int>(direction3),
|
||||
static_cast<int>(Direction::south)) ;
|
||||
TS_ASSERT_EQUALS(static_cast<int>(direction4),
|
||||
static_cast<int>(Direction::west)) ;
|
||||
|
||||
// string
|
||||
TS_ASSERT_EQUALS(static_cast<std::string>(direction1), "north") ;
|
||||
TS_ASSERT_EQUALS(static_cast<std::string>(direction2), "east") ;
|
||||
TS_ASSERT_EQUALS(static_cast<std::string>(direction3), "south") ;
|
||||
TS_ASSERT_EQUALS(static_cast<std::string>(direction4), "west") ;
|
||||
}
|
||||
|
||||
} ;
|
Loading…
Reference in New Issue