offset.h
Go to the documentation of this file.
00001 /* offset.h
00002  */
00003 #ifndef OSL_OFFSET_H
00004 #define OSL_OFFSET_H
00005 
00006 #include "osl/player.h"
00007 #include "osl/direction.h"
00008 #include "osl/misc/loki.h"
00009 #include <iosfwd>
00010 
00011 namespace osl 
00012 {
00016   class Offset
00017   {
00018   public:
00019     enum {
00020       OFFSET_MIN=-0x100,
00021       ONBOARD_OFFSET_MIN=-0x88,
00022       OFFSET_ZERO=0,
00023       ONBOARD_OFFSET_MAX=0x88,
00024       OFFSET_MAX=0x100,
00025       ONBOARD_OFFSET_SIZE=0x88*2+1
00026     };
00027     static const int BOARD_HEIGHT=16;
00028   private:
00029     int offset;
00030     explicit Offset(int o) : offset(o)
00031     {
00032     }
00033   public:
00034     static const Offset makeDirect(int value) { return Offset(value); }
00035     int intValue() const { return offset; }
00036   public:
00037     static int makeOffset(int dx,int dy) { return dx*BOARD_HEIGHT + dy; }
00038     Offset(int dx,int dy) : offset(makeOffset(dx,dy))
00039     {
00040     }
00041     Offset(Player, Direction);
00042     Offset() : offset(OFFSET_ZERO)
00043     {
00044     }
00045     template <Player, Direction>
00046     static Offset make();       // defined in directionTraits.h
00047     static const Offset ZERO() { return Offset(OFFSET_ZERO); }
00048     int
00049 #ifdef __GNUC__
00050         __attribute__ ((pure))
00051 #endif
00052     dx() const;
00053     int
00054 #ifdef __GNUC__
00055         __attribute__ ((pure))
00056 #endif
00057     dy() const;
00058     unsigned int index() const { return offset - OFFSET_MIN; }
00059 
00060     Offset& operator+=(Offset other)
00061     {
00062       offset += other.offset;
00063       return *this;
00064     }
00065     Offset& operator-=(Offset other){
00066       offset -= other.offset;
00067       return *this;
00068     }
00069     const Offset operator+(Offset other) const 
00070     {
00071       Offset result(*this);
00072       return result += other;
00073     }
00074     const Offset operator-(const Offset other) const
00075     {
00076       Offset result(*this);
00077       return result -= other;
00078     }
00079     const Offset operator*(const int mult) const {
00080       return static_cast<Offset>(static_cast<int>(offset)*mult);
00081     }
00082     const Offset operator-() const { return Offset(-offset); }
00083 #if 0
00084     inline Offset operator*(const Offset off1,const Offset off2){
00085       return static_cast<Offset>(static_cast<int>(off1)*static_cast<int>(off2));
00086     }
00087 #endif
00088   private:
00089     const Offset blackOffset(Int2Type<BLACK>) const { return *this; }
00090     const Offset blackOffset(Int2Type<WHITE>) const { return -(*this); }
00091   public:
00095     template <Player P>
00096     const Offset blackOffset() const { return blackOffset(Int2Type<P>()); }
00097 
00098     bool zero() const { return offset == OFFSET_ZERO; }
00099   };
00100 
00104   inline Offset newOffset(int dx,int dy){
00105     return Offset(dx,dy);
00106   }
00107 
00108   inline bool operator==(Offset l, Offset r)
00109   {
00110     return l.intValue() == r.intValue();
00111   }
00112   inline bool operator!=(Offset l, Offset r)
00113   {
00114     return ! (l == r);
00115   }
00116   inline bool operator<(Offset l, Offset r)
00117   {
00118     return l.intValue() < r.intValue();
00119   }
00120   
00121 
00122   std::ostream& operator<<(std::ostream&, Offset);
00123 
00124 } // namespace osl
00125 
00126 #endif /* OSL_OFFSET_H */
00127 // ;;; Local Variables:
00128 // ;;; mode:c++
00129 // ;;; c-basic-offset:2
00130 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines