/usr/include/osl/misc/carray.h is in libosl-dev 0.6.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | /* carray.h
*/
#ifndef OSL_CARRAY_H
#define OSL_CARRAY_H
#include "osl/player.h"
#include "osl/ptype.h"
#include "osl/config.h"
#include <cstddef>
#include <cassert>
#include <algorithm>
#include <iterator>
#include <boost/type_traits.hpp>
#define CONSERVATIVE_PLAYER_ACCESS
namespace osl
{
namespace misc
{
/**
* CArray の iterator.
* 単純にT*を使うと, if (iter == end()) と書くべきところで
* ポインタのつもりで if (iter) と書いてしまっても気付けないため.
* TODO: boost にない?
*/
template <typename T>
struct CArrayIterator
{
typedef std::random_access_iterator_tag iterator_category;
typedef T value_type;
typedef int difference_type;
typedef T* pointer;
typedef T& reference;
T *ptr;
CArrayIterator(T *p) : ptr(p) {}
CArrayIterator(const CArrayIterator<typename boost::remove_cv<T>::type>& src) : ptr(src.ptr)
{
}
T& operator*() const { return *ptr; }
T* operator->() const { return ptr; }
CArrayIterator& operator+=(int diff)
{
ptr += diff;
return *this;
}
CArrayIterator& operator-=(int diff) { return operator+=(-diff); }
CArrayIterator& operator++() { return operator+=(1); }
CArrayIterator operator++(int)
{
const CArrayIterator result = *this;
operator++();
return result;
}
CArrayIterator& operator--() { return operator+=(-1); }
CArrayIterator operator--(int)
{
const CArrayIterator result = *this;
operator--();
return result;
}
private:
#ifndef _MSC_VER
operator bool(); // not implemented
#endif
};
template <class T> inline
const CArrayIterator<T> operator+(const CArrayIterator<T>& iter, int diff)
{
CArrayIterator<T> result(iter);
result += diff;
return result;
}
template <class T> inline
const CArrayIterator<T> operator-(const CArrayIterator<T>& iter, int diff) {
return iter + (-diff);
}
// T と const T の違いを吸収,それ以外はcompile error になるはず
template <class T, class T2>
inline int operator-(CArrayIterator<T> l, CArrayIterator<T2> r)
{
return l.ptr - r.ptr;
}
template <class T, class T2>
inline bool operator==(CArrayIterator<T> l, CArrayIterator<T2> r)
{
return l.ptr == r.ptr;
}
template <class T, class T2>
inline bool operator!=(CArrayIterator<T> l, CArrayIterator<T2> r)
{
return l.ptr != r.ptr;
}
template <class T, class T2>
inline bool operator<(CArrayIterator<T> l, CArrayIterator<T2> r)
{
return l.ptr < r.ptr;
}
template <class T, class T2>
inline bool operator>(CArrayIterator<T> l, CArrayIterator<T2> r)
{
return l.ptr > r.ptr;
}
/**
* boost::array のまね。operator[] に assert をいれたかったので。
*/
template <typename T, size_t Capacity>
class CArray
{
public:
/** {} による初期化を許すために public にしておく */
T elements[Capacity];
typedef typename boost::remove_cv<T>::type T_simple;
public:
typedef T value_type;
typedef CArrayIterator<T> iterator;
iterator begin() { return &elements[0]; }
iterator end() { return &elements[Capacity]; }
void fill(T_simple value=T_simple())
{
std::fill(begin(), end(), value);
}
T& operator[] (size_t i)
{
assert(i < Capacity);
return elements[i];
}
static size_t size() { return Capacity; }
T const& operator[] (size_t i) const
{
assert(i < Capacity);
return elements[i];
}
typedef CArrayIterator<const T> const_iterator;
const_iterator begin() const { return &elements[0]; }
const_iterator end() const { return &elements[Capacity]; }
bool operator==(const CArray& other) const
{
return std::equal(begin(), end(), other.begin());
}
T& operator[] (Player p)
{
assert(1 < Capacity);
#ifndef CONSERVATIVE_PLAYER_ACCESS
// equivalent to operator[](playerToIndex(p))
return *((T*)((char *)&elements[0] +
(p & ((char *)&elements[1]-(char *)&elements[0]))));
#else
return operator[](playerToIndex(p));
#endif
}
const T& operator[] (Player p) const
{
assert(1 < Capacity);
#ifndef CONSERVATIVE_PLAYER_ACCESS
return *((T*)((char *)&elements[0] +
(p & ((char *)&elements[1]-(char *)&elements[0]))));
#else
return operator[](playerToIndex(p));
#endif
}
T& operator[] (PtypeO ptypeo)
{
assert(PTYPEO_SIZE <= (int)Capacity);
return operator[](ptypeOIndex(ptypeo));
}
const T& operator[] (PtypeO ptypeo) const
{
assert(PTYPEO_SIZE <= (int)Capacity);
return operator[](ptypeOIndex(ptypeo));
}
T& front() { return *begin(); }
T& back() { return *(end() - 1); }
const T& front() const { return *begin(); }
const T& back() const { return *(end() - 1); }
};
} // namespace misc
using misc::CArray;
} // namespace osl
#endif /* _FIXED_CAPACITY_VECTOR_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
|