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