All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
carray.h
Go to the documentation of this file.
1 /* carray.h
2  */
3 #ifndef OSL_CARRAY_H
4 #define OSL_CARRAY_H
5 #include "osl/player.h"
6 #include "osl/ptype.h"
7 #include "osl/config.h"
8 #include <cstddef>
9 #include <cassert>
10 #include <algorithm>
11 #include <iterator>
12 #include <boost/type_traits.hpp>
13 
14 #define CONSERVATIVE_PLAYER_ACCESS
15 
16 namespace osl
17 {
18  namespace misc
19  {
26  template <typename T>
28  {
29  typedef std::random_access_iterator_tag iterator_category;
30  typedef T value_type;
31  typedef int difference_type;
32  typedef T* pointer;
33  typedef T& reference;
34 
35  T *ptr;
36  CArrayIterator(T *p) : ptr(p) {}
37  CArrayIterator(const CArrayIterator<typename boost::remove_cv<T>::type>& src) : ptr(src.ptr)
38  {
39  }
40  T& operator*() const { return *ptr; }
41  T* operator->() const { return ptr; }
43  {
44  ptr += diff;
45  return *this;
46  }
47  CArrayIterator& operator-=(int diff) { return operator+=(-diff); }
50  {
51  const CArrayIterator result = *this;
52  operator++();
53  return result;
54  }
57  {
58  const CArrayIterator result = *this;
59  operator--();
60  return result;
61  }
62  private:
63 #ifndef _MSC_VER
64  operator bool(); // not implemented
65 #endif
66  };
67  template <class T> inline
68  const CArrayIterator<T> operator+(const CArrayIterator<T>& iter, int diff)
69  {
71  result += diff;
72  return result;
73  }
74  template <class T> inline
75  const CArrayIterator<T> operator-(const CArrayIterator<T>& iter, int diff) {
76  return iter + (-diff);
77  }
78  // T と const T の違いを吸収,それ以外はcompile error になるはず
79  template <class T, class T2>
81  {
82  return l.ptr - r.ptr;
83  }
84  template <class T, class T2>
86  {
87  return l.ptr == r.ptr;
88  }
89  template <class T, class T2>
91  {
92  return l.ptr != r.ptr;
93  }
94  template <class T, class T2>
95  inline bool operator<(CArrayIterator<T> l, CArrayIterator<T2> r)
96  {
97  return l.ptr < r.ptr;
98  }
99  template <class T, class T2>
101  {
102  return l.ptr > r.ptr;
103  }
104 
108  template <typename T, size_t Capacity>
109  class CArray
110  {
111  public:
113  T elements[Capacity];
114  typedef typename boost::remove_cv<T>::type T_simple;
115  public:
116  typedef T value_type;
118  iterator begin() { return &elements[0]; }
119  iterator end() { return &elements[Capacity]; }
120 
121  void fill(T_simple value=T_simple())
122  {
123  std::fill(begin(), end(), value);
124  }
125  T& operator[] (size_t i)
126  {
127  assert(i < Capacity);
128  return elements[i];
129  }
130 
131  static size_t size() { return Capacity; }
132 
133  T const& operator[] (size_t i) const
134  {
135  assert(i < Capacity);
136  return elements[i];
137  }
138 
140  const_iterator begin() const { return &elements[0]; }
141  const_iterator end() const { return &elements[Capacity]; }
142 
143  bool operator==(const CArray& other) const
144  {
145  return std::equal(begin(), end(), other.begin());
146  }
147 
149  {
150  assert(1 < Capacity);
151 #ifndef CONSERVATIVE_PLAYER_ACCESS
152  // equivalent to operator[](playerToIndex(p))
153  return *((T*)((char *)&elements[0] +
154  (p & ((char *)&elements[1]-(char *)&elements[0]))));
155 #else
156  return operator[](playerToIndex(p));
157 #endif
158  }
159  const T& operator[] (Player p) const
160  {
161  assert(1 < Capacity);
162 #ifndef CONSERVATIVE_PLAYER_ACCESS
163  return *((T*)((char *)&elements[0] +
164  (p & ((char *)&elements[1]-(char *)&elements[0]))));
165 #else
166  return operator[](playerToIndex(p));
167 #endif
168  }
169  T& operator[] (PtypeO ptypeo)
170  {
171  assert(PTYPEO_SIZE <= (int)Capacity);
172  return operator[](ptypeOIndex(ptypeo));
173  }
174  const T& operator[] (PtypeO ptypeo) const
175  {
176  assert(PTYPEO_SIZE <= (int)Capacity);
177  return operator[](ptypeOIndex(ptypeo));
178  }
179 
180  T& front() { return *begin(); }
181  T& back() { return *(end() - 1); }
182  const T& front() const { return *begin(); }
183  const T& back() const { return *(end() - 1); }
184  };
185  } // namespace misc
186  using misc::CArray;
187 } // namespace osl
188 
189 
190 #endif /* _FIXED_CAPACITY_VECTOR_H */
191 // ;;; Local Variables:
192 // ;;; mode:c++
193 // ;;; c-basic-offset:2
194 // ;;; End: