1 //===-- jitcs_adt_slice.h ---------------------------------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
6 #ifndef _JITCS_ADT_SLICE_H_
7 #define _JITCS_ADT_SLICE_H_
9 #include "jitcs_base.h"
10 #include "jitcs_adt_ref.h"
14 template <typename T
, unsigned FLAGS
>
16 typedef typename
std::add_const
<T
>::type CType
;
18 static const bool IsConstRef
= (FLAGS
& REF_Const
) != 0;
19 typedef typename
std::conditional
<IsConstRef
, CType
, NCType
>::type Type
;
20 typedef Type
* PtrType
;
21 typedef Type
& RefType
;
23 typedef Type
* iterator
;
24 typedef Type
const* const_iterator
;
31 _Slice(const _Slice
&) = default;
32 _Slice
& operator = (const _Slice
&) = default;
33 // RefType operator *() {
34 // guarantee<(FLAGS & REF_Nullable) == 0>
35 // ("dereferencing only for non-nullable refs, remove nullable first");
39 template <typename T2
, unsigned FLAGS2
>
40 _Slice(_Slice
<T2
, FLAGS2
> o
) {
41 static_assert((~FLAGS
& FLAGS2
) == 0, "incompatible _Slice flags");
42 //static_assert(std::is_base_of<T, T2>::value, "incompatible _Slice types");
45 template <typename T2
>
46 _Slice(T2
* o
, size_t n
) {
47 typedef typename
std::remove_const
<T2
>::type T2B
;
48 //static_assert(std::is_base_of<T, T2B>::value, "incompatible _Slice types");
49 //if ((FLAGS & REF_Nullable) == 0)
50 // assert(o != nullptr);
55 template <typename T2
, unsigned FLAGS2
>
56 _Slice
& operator =(_Slice
<T2
, FLAGS2
> o
) {
57 static_assert((~FLAGS
& FLAGS2
) == 0, "incompatible _Slice flags");
58 //static_assert(std::is_base_of<T, T2>::value, "incompatible _Ref types");
63 // Type* ptr() { return _ptr; }
64 // Type const* ptr() const { return _ptr; }
65 PtrType
ptr() const { return _ptr
; }
66 size_t size() const { return _size
; }
68 bool empty() const { return _size
== 0; }
69 bool isEmpty() const { return _size
== 0; }
71 bool isValidIndex(size_t r
) const { return r
>= 0 && r
< size(); }
73 RefType
operator [](size_t i
) const {
74 assert(isValidIndex(i
));
77 void popFront(size_t n
) {
78 _JITCS_DBG_CHECK_(n
<= _size
);
82 void popBack(size_t n
) {
83 _JITCS_DBG_CHECK_(n
<= _size
);
86 void fill(const NCType
& v
) {
87 guarantee
<!IsConstRef
>("mutation disabled on const container");
88 for (size_t i
= 0, n
= _size
; i
< n
; ++i
)
92 iterator
begin() { return _ptr
; }
93 iterator
end() { return _ptr
+ _size
; }
94 const_iterator
begin() const { return _ptr
; }
95 const_iterator
end() const { return _ptr
+ _size
; }
96 const_iterator
cbegin() const { return _ptr
; }
97 const_iterator
cend() const { return _ptr
+ _size
; }
103 template <typename T
>
104 using Slice
= _Slice
<typename
std::remove_const
<T
>::type
,
105 (std::is_const
<T
>::value
? REF_Const
: REF_Any
)>;
107 } // End jitcs namespace
110 // _JITCS_ADT_SLICE_H_