slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
Owner.h
浏览该文件的文档.
1#pragma once
2
3// GSL_OWNER documents a raw handle that must be released by the containing
4// type. Keep a local fallback so the project does not require the full
5// Guidelines Support Library.
6#ifndef GSL_OWNER
7#define GSL_OWNER
8#endif
9
10#include <algorithm>
11#include <memory>
12#include <vector>
13
14namespace z8 {
15template <typename T> class Owner {
16 std::unique_ptr<T> Own;
17
18public:
19 Owner() = default;
20 Owner(const Owner &) = delete;
21 Owner &operator=(const Owner &) = delete;
24
26 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
27 U *set(ArgTys &&...args) {
28 auto Ptr = std::make_unique<U>(std::forward<ArgTys>(args)...);
29 auto *Ref = Ptr.get();
30 Own = std::move(Ptr);
31 return Ref;
32 }
33
34 template <typename U = T,
35 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
36 U *set(std::unique_ptr<U> Ptr) {
37 auto *Ref = Ptr.get();
38 Own = std::move(Ptr);
39 return Ref;
40 }
41
42 T *get() { return Own.get(); }
43 const T *get() const { return Own.get(); }
44 void reset() noexcept { Own.reset(); }
45
46 [[nodiscard]]
47 explicit operator bool() const noexcept {
48 return static_cast<bool>(Own);
49 }
50};
51
52template <typename T> class OwnerArray {
53public:
54 using value_type = T;
55 using pointer = T *;
56 using reference = T &;
57 using const_pointer = const T *;
58 using const_reference = const T &;
59
60private:
61 std::vector<std::unique_ptr<T>> Owns;
62 std::vector<T *> Refs;
63
64public:
65 OwnerArray() = default;
66 OwnerArray(const OwnerArray &) = delete;
67 OwnerArray &operator=(const OwnerArray &) = delete;
70
72 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
73 U *add(ArgTys &&...args) {
74 auto Own = std::make_unique<U>(std::forward<ArgTys>(args)...);
75 auto *Ref = Own.get();
76 Owns.push_back(std::move(Own));
77 Refs.push_back(Ref);
78 return Ref;
79 }
80
81 template <typename U = T,
82 typename = std::enable_if_t<std::is_base_of_v<T, U>>>
83 U *add(std::unique_ptr<U> Own) {
84 auto *Ref = Own.get();
85 Owns.push_back(std::move(Own));
86 Refs.push_back(Ref);
87 return Ref;
88 }
89
90 T &operator[](std::size_t Index) { return *Refs[Index]; }
91 const T &operator[](std::size_t Index) const { return *Refs[Index]; }
92
93 T &at(std::size_t Index) { return *Refs.at(Index); }
94 const T &at(std::size_t Index) const { return *Refs.at(Index); }
95
96 T &front() { return *Refs.front(); }
97 const T &front() const { return *Refs.front(); }
98
99 T &back() { return *Refs.back(); }
100 const T &back() const { return *Refs.back(); }
101
102 [[nodiscard]]
103 std::size_t size() const noexcept {
104 return Refs.size();
105 }
106
107 [[nodiscard]]
109 return Refs.empty();
110 }
111
112 void reserve(std::size_t Size) {
113 Owns.reserve(Size);
114 Refs.reserve(Size);
115 }
116
118 Refs.clear();
119 Owns.clear();
120 }
121
122 [[nodiscard]]
123 const std::vector<T *> &get() const noexcept {
124 return Refs;
125 }
126
127 auto begin() noexcept { return Refs.begin(); }
128 auto end() noexcept { return Refs.end(); }
129
130 auto begin() const noexcept { return Refs.begin(); }
131 auto end() const noexcept { return Refs.end(); }
132
133 auto cbegin() const noexcept { return Refs.cbegin(); }
134 auto cend() const noexcept { return Refs.cend(); }
135};
136} // namespace z8
Definition Owner.h:52
auto begin() const noexcept
Definition Owner.h:130
std::vector< std::unique_ptr< T > > Owns
Definition Owner.h:61
OwnerArray & operator=(const OwnerArray &)=delete
auto begin() noexcept
Definition Owner.h:127
const T & back() const
Definition Owner.h:100
T & at(std::size_t Index)
Definition Owner.h:93
const T & operator[](std::size_t Index) const
Definition Owner.h:91
T & front()
Definition Owner.h:96
auto end() noexcept
Definition Owner.h:128
auto cbegin() const noexcept
Definition Owner.h:133
const std::vector< T * > & get() const noexcept
Definition Owner.h:123
const T & front() const
Definition Owner.h:97
void clear() noexcept
Definition Owner.h:117
void reserve(std::size_t Size)
Definition Owner.h:112
auto cend() const noexcept
Definition Owner.h:134
OwnerArray(OwnerArray &&) noexcept=default
std::size_t size() const noexcept
Definition Owner.h:103
OwnerArray(const OwnerArray &)=delete
T & back()
Definition Owner.h:99
U * add(std::unique_ptr< U > Own)
Definition Owner.h:83
OwnerArray()=default
auto end() const noexcept
Definition Owner.h:131
bool empty() const noexcept
Definition Owner.h:108
std::vector< T * > Refs
Definition Owner.h:62
const T & at(std::size_t Index) const
Definition Owner.h:94
T & operator[](std::size_t Index)
Definition Owner.h:90
U * add(ArgTys &&...args)
Definition Owner.h:73
Definition Owner.h:15
Owner(Owner &&) noexcept=default
Owner()=default
T * get()
Definition Owner.h:42
std::unique_ptr< T > Own
Definition Owner.h:16
Owner(const Owner &)=delete
U * set(ArgTys &&...args)
Definition Owner.h:27
U * set(std::unique_ptr< U > Ptr)
Definition Owner.h:36
void reset() noexcept
Definition Owner.h:44
Owner & operator=(const Owner &)=delete
const T * get() const
Definition Owner.h:43
资源的强类型引用,同时覆盖持久化 ID 与运行时索引两种边界。
Definition ResourceRef.h:36
世界矩阵 描述坐标系,左手坐标系 使用 DirectX12 坐标系的描述
Definition Application.h:15