All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
offset.h
Go to the documentation of this file.
1 /* offset.h
2  */
3 #ifndef OSL_OFFSET_H
4 #define OSL_OFFSET_H
5 
6 #include "osl/player.h"
7 #include "osl/direction.h"
8 #include "osl/misc/loki.h"
9 #include <iosfwd>
10 
11 namespace osl
12 {
16  class Offset
17  {
18  public:
19  enum {
20  OFFSET_MIN=-0x100,
24  OFFSET_MAX=0x100,
26  };
27  static const int BOARD_HEIGHT=16;
28  private:
29  int offset;
30  explicit Offset(int o) : offset(o)
31  {
32  }
33  public:
34  static const Offset makeDirect(int value) { return Offset(value); }
35  int intValue() const { return offset; }
36  public:
37  static int makeOffset(int dx,int dy) { return dx*BOARD_HEIGHT + dy; }
38  Offset(int dx,int dy) : offset(makeOffset(dx,dy))
39  {
40  }
43  {
44  }
45  template <Player, Direction>
46  static Offset make(); // defined in directionTraits.h
47  static const Offset ZERO() { return Offset(OFFSET_ZERO); }
48  int
49 #ifdef __GNUC__
50  __attribute__ ((pure))
51 #endif
52  dx() const;
53  int
54 #ifdef __GNUC__
55  __attribute__ ((pure))
56 #endif
57  dy() const;
58  unsigned int index() const { return offset - OFFSET_MIN; }
59 
61  {
62  offset += other.offset;
63  return *this;
64  }
66  offset -= other.offset;
67  return *this;
68  }
69  const Offset operator+(Offset other) const
70  {
71  Offset result(*this);
72  return result += other;
73  }
74  const Offset operator-(const Offset other) const
75  {
76  Offset result(*this);
77  return result -= other;
78  }
79  const Offset operator*(const int mult) const {
80  return static_cast<Offset>(static_cast<int>(offset)*mult);
81  }
82  const Offset operator-() const { return Offset(-offset); }
83 #if 0
84  inline Offset operator*(const Offset off1,const Offset off2){
85  return static_cast<Offset>(static_cast<int>(off1)*static_cast<int>(off2));
86  }
87 #endif
88  private:
89  const Offset blackOffset(Int2Type<BLACK>) const { return *this; }
90  const Offset blackOffset(Int2Type<WHITE>) const { return -(*this); }
91  public:
95  template <Player P>
96  const Offset blackOffset() const { return blackOffset(Int2Type<P>()); }
97 
98  bool zero() const { return offset == OFFSET_ZERO; }
99  };
100 
104  inline Offset newOffset(int dx,int dy){
105  return Offset(dx,dy);
106  }
107 
108  inline bool operator==(Offset l, Offset r)
109  {
110  return l.intValue() == r.intValue();
111  }
112  inline bool operator!=(Offset l, Offset r)
113  {
114  return ! (l == r);
115  }
116  inline bool operator<(Offset l, Offset r)
117  {
118  return l.intValue() < r.intValue();
119  }
120 
121 
122  std::ostream& operator<<(std::ostream&, Offset);
123 
124 } // namespace osl
125 
126 #endif /* OSL_OFFSET_H */
127 // ;;; Local Variables:
128 // ;;; mode:c++
129 // ;;; c-basic-offset:2
130 // ;;; End: