]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/lib/libk/cxx/static_vector
Update
[l4.git] / kernel / fiasco / src / lib / libk / cxx / static_vector
1 // vi:ft=cpp
2
3 #pragma once
4
5 #include <cxx/type_traits>
6
7 namespace cxx {
8
9 /**
10  * Simple encapsulation for a dynamically allocated array.
11  *
12  * The main purppose of this class is to support C++11 range for
13  * for simple dynamically allocated array with static size.
14  */
15 template<typename T, typename IDX = unsigned>
16 class static_vector
17 {
18 private:
19   template<typename X, typename IDX2> friend class static_vector;
20   T *_v;
21   IDX _l;
22
23 public:
24   typedef T value_type;
25   typedef IDX index_type;
26
27   static_vector() = default;
28   static_vector(value_type *v, index_type length) : _v(v), _l(length) {}
29
30   /// Conversion from compatible arrays
31   template<typename X,
32            typename = typename enable_if<is_convertible<X, T>::value>::type>
33   static_vector(static_vector<X, IDX> const &o) : _v(o._v), _l(o._l) {}
34
35   index_type size() const { return _l; }
36   bool empty() const { return _l == 0; }
37
38   value_type &operator [] (index_type idx) { return _v[idx]; }
39   value_type const &operator [] (index_type idx) const { return _v[idx]; }
40
41   value_type *begin() { return _v; }
42   value_type *end() { return _v + _l; }
43   value_type const *begin() const { return _v; }
44   value_type const *end() const { return _v + _l; }
45   value_type const *cbegin() const { return _v; }
46   value_type const *cend() const { return _v + _l; }
47
48   /// Get the index of the given element of the array
49   index_type index(value_type const *o) const { return o - _v; }
50   index_type index(value_type const &o) const { return &o - _v; }
51 };
52
53 }