Files
moslab-code/src/l4/pkg/l4re-core/cxx/lib/tl/include/static_vector
2025-09-12 15:55:45 +02:00

59 lines
1.6 KiB
C++

// vi:set ft=cpp: -*- Mode: C++ -*-
#pragma once
#include "type_traits"
namespace cxx {
/**
* Simple encapsulation for a dynamically allocated array.
*
* The main purpose of this class is to support C++11 range for
* for simple dynamically allocated array with static size.
*/
template<typename T, typename IDX = unsigned>
class static_vector
{
private:
template<typename X, typename IDX2> friend class static_vector;
T *_v;
IDX _l;
public:
typedef T value_type;
typedef IDX index_type;
static_vector() = default;
static_vector(value_type *v, index_type length) : _v(v), _l(length) {}
template<typename Z,
typename = enable_if_t<is_same<remove_extent_t<Z>, T>::value>>
constexpr static_vector(Z &v) : _v(v), _l(array_size(v))
{}
/// Conversion from compatible arrays
template<typename X,
typename = enable_if_t<is_convertible<X, T>::value>>
static_vector(static_vector<X, IDX> const &o) : _v(o._v), _l(o._l) {}
index_type size() const { return _l; }
bool empty() const { return _l == 0; }
value_type &operator [] (index_type idx) { return _v[idx]; }
value_type const &operator [] (index_type idx) const { return _v[idx]; }
value_type *begin() { return _v; }
value_type *end() { return _v + _l; }
value_type const *begin() const { return _v; }
value_type const *end() const { return _v + _l; }
value_type const *cbegin() const { return _v; }
value_type const *cend() const { return _v + _l; }
/// Get the index of the given element of the array
index_type index(value_type const *o) const { return o - _v; }
index_type index(value_type const &o) const { return &o - _v; }
};
}