• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguicwindows翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

Simple C++ UI framework and more...


コミットメタ情報

リビジョンe342a2a37395c9bd33e486f29019a3104a22e9ad (tree)
日時2018-03-07 17:54:51
作者Starg <starg@user...>
コミッターStarg

ログメッセージ

Add ArrayView

変更サマリ

差分

--- /dev/null
+++ b/include/sirius/array_view.hpp
@@ -0,0 +1,359 @@
1+// Copyright 2018 Starg
2+// Distributed under the 3-Clause BSD License.
3+
4+#pragma once
5+
6+#include <cassert>
7+#include <cstddef>
8+
9+#include <algorithm>
10+#include <iterator>
11+#include <limits>
12+#include <stdexcept>
13+#include <type_traits>
14+#include <utility>
15+
16+namespace Sirius
17+{
18+
19+namespace Detail
20+{
21+
22+using std::data;
23+using std::size;
24+
25+template<typename TArrayView, typename TContainer>
26+constexpr bool CanInitializeArrayView = std::is_convertible_v<decltype(data(std::declval<TContainer>())), typename TArrayView::const_pointer>
27+ && std::is_convertible_v<decltype(size(std::declval<TContainer>())), typename TArrayView::size_type>;
28+
29+template<typename T>
30+constexpr auto InvokeData(const T& c) noexcept
31+{
32+ return data(c);
33+}
34+
35+template<typename T>
36+constexpr auto InvokeSize(const T& c) noexcept
37+{
38+ return size(c);
39+}
40+
41+} // namespace Detail
42+
43+template<typename T>
44+class ArrayViewIterator
45+{
46+public:
47+ using value_type = T;
48+ using difference_type = std::ptrdiff_t;
49+ using pointer = const T*;
50+ using reference = const T&;
51+ using iterator_category = std::random_access_iterator_tag;
52+
53+ constexpr ArrayViewIterator() noexcept : m_p(nullptr)
54+ {
55+ }
56+
57+ constexpr explicit ArrayViewIterator(pointer p) noexcept : m_p(p)
58+ {
59+ }
60+
61+ constexpr ArrayViewIterator(const ArrayViewIterator<T>&) noexcept = default;
62+ constexpr ArrayViewIterator<T>& operator=(const ArrayViewIterator<T>&) noexcept = default;
63+
64+ constexpr bool operator==(const ArrayViewIterator<T>& rhs) const noexcept
65+ {
66+ return m_p == rhs.m_p;
67+ }
68+
69+ constexpr bool operator!=(const ArrayViewIterator<T>& rhs) const noexcept
70+ {
71+ return !(*this == rhs);
72+ }
73+
74+ constexpr reference operator*() const noexcept
75+ {
76+ return *m_p;
77+ }
78+
79+ constexpr pointer operator->() const noexcept
80+ {
81+ return m_p;
82+ }
83+
84+ constexpr ArrayViewIterator<T>& operator++() noexcept
85+ {
86+ ++m_p;
87+ return *this;
88+ }
89+
90+ constexpr ArrayViewIterator<T> operator++(int) noexcept
91+ {
92+ auto dup = *this;
93+ ++*this;
94+ return dup;
95+ }
96+
97+ constexpr ArrayViewIterator<T>& operator--() noexcept
98+ {
99+ --m_p;
100+ return *this;
101+ }
102+
103+ constexpr ArrayViewIterator<T> operator--(int) noexcept
104+ {
105+ auto dup = *this;
106+ --*this;
107+ return dup;
108+ }
109+
110+ constexpr ArrayViewIterator<T>& operator+=(difference_type n) noexcept
111+ {
112+ m_p += n;
113+ return *this;
114+ }
115+
116+ constexpr ArrayViewIterator<T> operator+(difference_type n) const noexcept
117+ {
118+ auto dup = *this;
119+ return dup += n;
120+ }
121+
122+ friend constexpr ArrayViewIterator<T> operator+(difference_type n, const ArrayViewIterator<T>& rhs) noexcept
123+ {
124+ auto dup = rhs;
125+ return dup += n;
126+ }
127+
128+ constexpr ArrayViewIterator<T>& operator-=(difference_type n) noexcept
129+ {
130+ return *this += -n;
131+ }
132+
133+ constexpr ArrayViewIterator<T> operator-(difference_type n) const noexcept
134+ {
135+ auto dup = *this;
136+ return dup -= n;
137+ }
138+
139+ constexpr difference_type operator-(const ArrayViewIterator<T>& rhs) const noexcept
140+ {
141+ return m_p - rhs.m_p;
142+ }
143+
144+ constexpr reference operator[](difference_type n) const noexcept
145+ {
146+ return *(*this + n);
147+ }
148+
149+ constexpr bool operator<(const ArrayViewIterator<T>& rhs) const noexcept
150+ {
151+ return rhs - *this > 0;
152+ }
153+
154+ constexpr bool operator>(const ArrayViewIterator<T>& rhs) const noexcept
155+ {
156+ return rhs < *this;
157+ }
158+
159+ constexpr bool operator>=(const ArrayViewIterator<T>& rhs) const noexcept
160+ {
161+ return !(*this < rhs);
162+ }
163+
164+ constexpr bool operator<=(const ArrayViewIterator<T>& rhs) const noexcept
165+ {
166+ return !(*this > rhs);
167+ }
168+
169+private:
170+ pointer m_p;
171+};
172+
173+template<typename T>
174+class ArrayView
175+{
176+public:
177+ using value_type = T;
178+ using size_type = std::size_t;
179+ using difference_type = std::ptrdiff_t;
180+ using pointer = T*;
181+ using const_pointer = const T*;
182+ using reference = T&;
183+ using const_reference = const T&;
184+ using iterator = ArrayViewIterator<T>;
185+ using const_iterator = ArrayViewIterator<T>;
186+ using reverse_iterator = std::reverse_iterator<iterator>;
187+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
188+
189+ constexpr ArrayView() noexcept : m_pData(nullptr), m_Size(0)
190+ {
191+ }
192+
193+ constexpr ArrayView(const_pointer pData, size_type size) noexcept : m_pData(pData), m_Size(size)
194+ {
195+ }
196+
197+ template<std::size_t TSize>
198+ constexpr ArrayView(T(&arr)[TSize]) : m_pData(arr), m_Size(TSize)
199+ {
200+ }
201+
202+ template<std::size_t TSize>
203+ constexpr ArrayView(const T(&arr)[TSize]) : m_pData(arr), m_Size(TSize)
204+ {
205+ }
206+
207+ template<typename TContainer, std::enable_if_t<Detail::CanInitializeArrayView<ArrayView<T>, const TContainer&>, int> = 0>
208+ constexpr ArrayView(const TContainer& c) noexcept : m_pData(Detail::InvokeData(c)), m_Size(Detail::InvokeSize(c))
209+ {
210+ }
211+
212+ constexpr ArrayView(const ArrayView<T>&) noexcept = default;
213+ constexpr ArrayView<T>& operator=(const ArrayView<T>&) noexcept = default;
214+
215+ constexpr const_iterator begin() const noexcept
216+ {
217+ return const_iterator(m_pData);
218+ }
219+
220+ constexpr const_iterator cbegin() const noexcept
221+ {
222+ return const_iterator(m_pData);
223+ }
224+
225+ constexpr const_iterator end() const noexcept
226+ {
227+ return const_iterator(m_pData + m_Size);
228+ }
229+
230+ constexpr const_iterator cend() const noexcept
231+ {
232+ return const_iterator(m_pData + m_Size);
233+ }
234+
235+ constexpr const_reverse_iterator rbegin() const noexcept
236+ {
237+ return const_reverse_iterator(end());
238+ }
239+
240+ constexpr const_reverse_iterator crbegin() const noexcept
241+ {
242+ return const_reverse_iterator(cend());
243+ }
244+
245+ constexpr const_reverse_iterator rend() const noexcept
246+ {
247+ return const_reverse_iterator(begin());
248+ }
249+
250+ constexpr const_reverse_iterator crend() const noexcept
251+ {
252+ return const_reverse_iterator(cbegin());
253+ }
254+
255+ constexpr const T& operator[](std::size_t n) const noexcept
256+ {
257+ assert(n < m_Size);
258+ return m_pData[n];
259+ }
260+
261+ constexpr const T& at(std::size_t n) const
262+ {
263+ if (n < m_Size)
264+ {
265+ return m_pData[n];
266+ }
267+ else
268+ {
269+ throw std::out_of_range("Sirius::ArrayView<T>::at() : out of range");
270+ }
271+ }
272+
273+ constexpr const T& front() const noexcept
274+ {
275+ return *m_pData;
276+ }
277+
278+ constexpr const T& back() const noexcept
279+ {
280+ return m_pData[m_Size - 1];
281+ }
282+
283+ constexpr const T* data() const noexcept
284+ {
285+ return m_pData;
286+ }
287+
288+ constexpr std::size_t size() const noexcept
289+ {
290+ return m_Size;
291+ }
292+
293+ constexpr std::size_t max_size() const noexcept
294+ {
295+ return std::min(
296+ std::numeric_limits<std::ptrdiff_t>::max(),
297+ std::numeric_limits<std::size_t>::max() / sizeof(T)
298+ );
299+ }
300+
301+ constexpr bool empty() const noexcept
302+ {
303+ return m_Size == 0;
304+ }
305+
306+ /* constexpr */ bool operator==(const ArrayView<T>& rhs) const noexcept
307+ {
308+ return std::equal(begin(), end(), rhs.begin(), rhs.end());
309+ }
310+
311+ /* constexpr */ bool operator!=(const ArrayView<T>& rhs) const noexcept
312+ {
313+ return !(*this == rhs);
314+ }
315+
316+ /* constexpr */ bool operator<(const ArrayView<T>& rhs) const noexcept
317+ {
318+ return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
319+ }
320+
321+ /* constexpr */ bool operator>(const ArrayView<T>& rhs) const noexcept
322+ {
323+ return rhs < *this;
324+ }
325+
326+ /* constexpr */ bool operator<=(const ArrayView<T>& rhs) const noexcept
327+ {
328+ return !(*this > rhs);
329+ }
330+
331+ /* constexpr */ bool operator>=(const ArrayView<T>& rhs) const noexcept
332+ {
333+ return !(*this < rhs);
334+ }
335+
336+ constexpr void swap(ArrayView<T>& rhs) noexcept
337+ {
338+ using std::swap;
339+ swap(m_pData, rhs.m_pData);
340+ swap(m_Size, rhs.m_Size);
341+ }
342+
343+ constexpr ArrayView<T> GetSubView(std::size_t offset, std::size_t length) const noexcept
344+ {
345+ return ArrayView<T>(m_pData + offset, length);
346+ }
347+
348+private:
349+ const_pointer m_pData;
350+ size_type m_Size;
351+};
352+
353+template<typename T>
354+constexpr void swap(ArrayView<T>& lhs, ArrayView<T>& rhs) noexcept
355+{
356+ lhs.swap(rhs);
357+}
358+
359+} // namespace Sirius
--- a/include/sirius/event.hpp
+++ b/include/sirius/event.hpp
@@ -92,8 +92,10 @@ public:
9292 void RemoveEventListener(EventListener* pEventListener);
9393 bool HasEventListener(EventListener* pEventListener) const;
9494
95+protected:
96+ UIContext* m_pUIContext; // also used by Widget
97+
9598 private:
96- UIContext* m_pUIContext;
9799 std::vector<EventListener*> m_LocalEventListeners;
98100 };
99101
--- a/include/sirius/geometry.hpp
+++ b/include/sirius/geometry.hpp
@@ -185,6 +185,16 @@ struct Rect
185185 swap(Bottom, rhs.Bottom);
186186 }
187187
188+ T GetWidth() const noexcept
189+ {
190+ return Right - Left;
191+ }
192+
193+ T GetHeight() const noexcept
194+ {
195+ return Bottom - Top;
196+ }
197+
188198 T Left;
189199 T Top;
190200 T Right;
--- a/include/sirius/widget.hpp
+++ b/include/sirius/widget.hpp
@@ -3,6 +3,9 @@
33
44 #pragma once
55
6+#include <vector>
7+
8+#include "sirius/array_view.hpp"
69 #include "sirius/event.hpp"
710 #include "sirius/geometry.hpp"
811
@@ -11,6 +14,16 @@ namespace Sirius
1114
1215 class UIContext;
1316
17+namespace WidgetEvent
18+{
19+
20+//enum : Event::EventKindType
21+//{
22+//
23+//};
24+
25+} // namespace WidgetEvent
26+
1427 class SIRIUS_DLLEXPORT Widget : public EventHandler
1528 {
1629 public:
@@ -18,8 +31,31 @@ public:
1831 {
1932 }
2033
34+ Widget* GetParent() const
35+ {
36+ return m_pParent;
37+ }
38+
39+ ArrayView<Widget*> GetChildren() const
40+ {
41+ return m_Children;
42+ }
43+
44+ Rect<float> GetRect() const
45+ {
46+ return m_Rect;
47+ }
48+
49+ float GetOpacity() const
50+ {
51+ return m_Opacity;
52+ }
53+
2154 private:
55+ Widget* m_pParent = nullptr;
56+ std::vector<Widget*> m_Children;
2257 Rect<float> m_Rect{}; // (0, 0) is the left-top corner of the parent widget
58+ float m_Opacity = 1.0f; // [0.0f, 1.0f]
2359 };
2460
2561 } // namespace Sirius
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -5,6 +5,7 @@ add_library(
55 sirius ${SIRIUS_LIBRARY_TYPE}
66
77 ../include/sirius/animation.hpp
8+ ../include/sirius/array_view.hpp
89 ../include/sirius/event.hpp
910 ../include/sirius/geometry.hpp
1011 ../include/sirius/uicontext.hpp