diff --git a/owlps-positioning/Makefile b/owlps-positioning/Makefile index 43096a1..604a83d 100644 --- a/owlps-positioning/Makefile +++ b/owlps-positioning/Makefile @@ -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 diff --git a/owlps-positioning/direction.cc b/owlps-positioning/direction.cc new file mode 100644 index 0000000..fd0022f --- /dev/null +++ b/owlps-positioning/direction.cc @@ -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(d) ; + return os ; +} diff --git a/owlps-positioning/direction.hh b/owlps-positioning/direction.hh new file mode 100644 index 0000000..8006d34 --- /dev/null +++ b/owlps-positioning/direction.hh @@ -0,0 +1,87 @@ +#ifndef _OWLPS_POSITIONING_DIRECTION_HH_ +#define _OWLPS_POSITIONING_DIRECTION_HH_ + +#include + +/// \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(direction) ; +} + + + +#endif // _OWLPS_POSITIONING_DIRECTION_HH_ diff --git a/owlps-positioning/posexcept.cc b/owlps-positioning/posexcept.cc index fb9af41..b675897 100644 --- a/owlps-positioning/posexcept.cc +++ b/owlps-positioning/posexcept.cc @@ -1,8 +1,23 @@ #include "posexcept.hh" +#include + 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(direction) + << "` is not a valid direction value!" ; + return message.str().c_str() ; +} + + element_not_found:: element_not_found(const string &_explanation) throw(): explanation(_explanation) {} diff --git a/owlps-positioning/posexcept.hh b/owlps-positioning/posexcept.hh index 3d1f137..06e213d 100644 --- a/owlps-positioning/posexcept.hh +++ b/owlps-positioning/posexcept.hh @@ -5,6 +5,17 @@ #include +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: diff --git a/owlps-positioning/tests/direction_test.hh b/owlps-positioning/tests/direction_test.hh new file mode 100644 index 0000000..9293d45 --- /dev/null +++ b/owlps-positioning/tests/direction_test.hh @@ -0,0 +1,86 @@ +#include + +#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(direction1), + static_cast(Direction::north)) ; + TS_ASSERT_EQUALS(static_cast(direction2), + static_cast(Direction::east)) ; + TS_ASSERT_EQUALS(static_cast(direction3), + static_cast(Direction::south)) ; + TS_ASSERT_EQUALS(static_cast(direction4), + static_cast(Direction::west)) ; + + // string + TS_ASSERT_EQUALS(static_cast(direction1), "north") ; + TS_ASSERT_EQUALS(static_cast(direction2), "east") ; + TS_ASSERT_EQUALS(static_cast(direction3), "south") ; + TS_ASSERT_EQUALS(static_cast(direction4), "west") ; + } + +} ;