-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathview.h
185 lines (150 loc) · 5.05 KB
/
view.h
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
#ifndef RTC_BASE_VIEW_H_
#define RTC_BASE_VIEW_H_
#include <stddef.h>
#include "rtc_base/checks.h"
#include "rtc_base/constructor_magic.h"
#if defined(__cplusplus)
// C++ version.
#include <algorithm>
// #include <array>
namespace rtc {
template <typename T>
struct View {
public:
// Construct View from a pointer and a length.
template <typename U>
View(U* data, size_t size)
: data_(data),
size_(size) {
RTC_DCHECK_EQ(size == 0 ? nullptr : data, this->data());
RTC_DCHECK_EQ(size, this->size());
RTC_DCHECK_EQ(!this->data(),
this->size() == 0); // data is null if size == 0.
}
// Construct an empty View. Note that fixed-size Views of size > 0
// cannot be empty.
View()
: View(static_cast<T*>(nullptr), 0) {}
/* View(std::nullptr_t) // NOLINT
: View() {}
View(std::nullptr_t, size_t size)
: View(static_cast<T*>(nullptr), size) {
RTC_DCHECK_EQ(0, size);
} */
// Construct an View from a C-style array.
template <typename U, size_t N>
View(U (&array)[N]) // NOLINT
: View(array, N) {}
// Construct an View from any type U that
// has a size() method whose return value converts implicitly to size_t, and
// a data() method whose return value converts implicitly to T*.
// std::array and rtc::View itself can also be constructed by these method
template <typename U>
View(U& u) // NOLINT
: View(u.data(), u.size()) {}
template <typename U>
View(const U& u) // NOLINT(runtime/explicit)
: View(u.data(), u.size()) {}
T* data() const { return data_; }
size_t size() const { return size_; }
bool empty() const { return this->size() == 0; }
// Indexing and iteration. These allow mutation even if the View is
// const, because the View doesn't own the array. (To prevent mutation,
// use a const element type.)
T& operator[](size_t idx) const {
RTC_DCHECK_LT(idx, this->size());
RTC_DCHECK(this->data());
return this->data()[idx];
}
T* begin() const { return this->data(); }
T* end() const { return this->data() + this->size(); }
/* const T* cbegin() const { return this->data(); }
const T* cend() const { return this->data() + this->size(); } */
T& front() const { return *this->begin(); } // *this->begin()
T& back() const { return *std::prev(this->end()); } // *(--this->end())
void fill(T value) { std::fill(this->begin(), this->end(), value); }
View<T> subview(size_t offset, size_t size) const {
return offset < this->size()
? View<T>(this->data() + offset,
std::min(size, this->size() - offset))
: View<T>();
}
View<T> subview(size_t offset) const {
return subview(offset, this->size());
}
// Comparing two Views compares their (pointer,size) pairs; it does *not*
// dereference the pointers.
/* template <typename T>
bool operator==(const View<T>& a, const View<T>& b) {
return a.data() == b.data() && a.size() == b.size();
}
template <typename T>
bool operator!=(const View<T>& a, const View<T>& b) {
return !(a == b);
} */
private:
T* data_;
size_t size_;
// RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(View);
};
/* template <typename T>
inline View<T> MakeView(T* data, size_t size) {
return View<T>(data, size);
} */
} // namespace rtc
#define RTC_VIEW(T) rtc::View<T>
#define RTC_MAKE_VIEW(T) rtc::View<T> // rtc::MakeView<T>
#if 0 /* container */
namespace rtc {
template <typename T>
struct Container {
// public:
// Container(T data) : data_(data) {};
// T& data() { return data_; }
// private:
T data_;
};
} // namespace rtc
#define RTC_CONTAINER(T) rtc::Container<T>
#else /* container */
#include <array>
/* namespace rtc {
template <typename T>
struct Array {
// public:
// Construct an View from a C-style array.
template <typename U, size_t N>
Array(U (&data)[N]) // NOLINT
: data_(data),
view_(RTC_MAKE_VIEW(U)(data_)) {};
// cannot things below, eg.
// T is float[96], we need view_ to be
// RTC_VIEW(float) not RTC_VIEW(float[96])
// T* data() { return view_.data(); }
// size_t size() const { return view_.size(); }
// T& operator[](size_t idx) const { return view_[idx]; }
// T* begin() const { return view_.begin(); }
// T* end() const { return view_.end(); }
// void fill(T value) { view_.fill(value); }
// private:
T data_;
// RTC_VIEW(T) view_; // cannot get U
};
} // namespace rtc
#define RTC_ARRAY(T) rtc::array<T> */
#endif /* container */
#elif 0 // #else // __cplusplus not defined
// C version. Lacks many features compared to the C++ version, but usage
// guidelines are the same.
typedef struct { void* data_; size_t size_; } rtc_View;
inline rtc_View rtc_MakeView(void *data, size_t size)
{
return (rtc_View) { data, size, };
}
// error: types may not be defined in parameter types
#define RTC_VIEW(T) rtc_View // struct { T* data_; size_t size_; }
#define RTC_MAKE_VIEW(T) rtc_MakeView
// RTC_VIEW(float) xxx_view = RTC_MAKE_VIEW(float)(xxx.data(), xxx.size());
// #define RTC_CONTAINER(T) struct { T data_; }
#endif // defined(__cplusplus)
#endif // RTC_BASE_VIEW_H_