00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #ifndef GIRLDIRECTION_HPP
00028 #define GIRLDIRECTION_HPP
00029
00030
00031
00032
00033 #include "girlIO.hpp"
00034
00035 namespace girl {
00036
00040 typedef enum eDirection
00041 {
00042 RIGHT = 0,
00043 UP = 1,
00044 LEFT = 2,
00045 DOWN = 3,
00046 INVALID = -2
00047 } eDirection;
00048
00049
00055 class direction
00056 {
00057 public:
00061 direction(eDirection d = INVALID)
00062 : m_d(d)
00063 {}
00064
00068 operator int () const
00069 {
00070 return static_cast<int>(m_d);
00071 }
00072
00073
00079 girl::direction opposite() const
00080 {
00081 assert(m_d!=INVALID);
00082 return static_cast<eDirection>(static_cast<int>(m_d)^2);
00083 }
00084
00090 girl::direction previous() const
00091 {
00092 assert(m_d!=INVALID);
00093
00094 return static_cast<eDirection>((static_cast<int>(m_d)+1)&3);
00095 }
00096
00102 girl::direction next() const
00103 {
00104 assert(m_d!=INVALID);
00105
00106 return static_cast<eDirection>((static_cast<int>(m_d)-1)&3);
00107 }
00108
00114 void write(girl::io::Writer &w, girl::io::Type ioType) const
00115 {
00116 if (ioType == girl::io::MEMORY) {
00117 w.write(&m_d, sizeof(girl::direction));
00118 }
00119 else {
00120 assert(ioType == girl::io::DISK);
00121 girl::io::writeInt(m_d, w);
00122 }
00123
00124 }
00125
00133 bool read(girl::io::Reader &r, girl::io::Type ioType)
00134 {
00135 if (ioType == girl::io::MEMORY) {
00136 if (r.read(&m_d, sizeof(girl::direction)) != sizeof(girl::direction))
00137 return false;
00138 }
00139 else {
00140 assert(ioType == girl::io::DISK);
00141 int dir;
00142 if (! girl::io::readInt(&dir, r))
00143 return false;
00144 m_d = static_cast<eDirection>(dir);
00145 }
00146 return true;
00147 }
00148
00149
00150
00151 private:
00152 girl::eDirection m_d;
00153 };
00154
00155
00156 }
00157
00158
00159
00160 #endif