Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Memory.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
21#pragma once
22#include <memory>
23
24#ifdef _WIN32
25#pragma warning(disable : 4819)
26#endif
27
28namespace sese {
29
30template<typename T>
31using SharedType = std::shared_ptr<T>;
32
33template<typename T>
34using UniqueType = std::unique_ptr<T>;
35
36template<typename T>
37using WeakType = std::weak_ptr<T>;
38
46template<typename RETURN_TYPE, typename... ARGS>
47inline SharedType<RETURN_TYPE> makeShared(ARGS &&...args) {
48 return std::make_shared<RETURN_TYPE>(args...);
49}
50
58template<typename RETURN_TYPE, typename... ARGS>
59inline SharedType<RETURN_TYPE> makeUnique(ARGS &&...args) {
60 return std::make_unique<RETURN_TYPE>(args...);
61}
62
63#define MAKE_SHARED_PRIVATE(RETURN_TYPE, ...) sese::SharedType<RETURN_TYPE>(new RETURN_TYPE(__VA_ARGS__))
64
65#define MAKE_UNIQUE_PRIVATE(RETURN_TYPE, ...) sese::UniqueType<RETURN_TYPE>(new RETURN_TYPE(__VA_ARGS__))
66
74template<typename RETURN_TYPE, typename INIT_TYPE>
75SharedType<RETURN_TYPE> makeSharedFromList(std::initializer_list<INIT_TYPE> list) {
76 return std::make_shared<RETURN_TYPE>(std::move(list));
77}
78
86template<typename RETURN_TYPE, typename INIT_TYPE>
87UniqueType<RETURN_TYPE> makeUniqueFromList(std::initializer_list<INIT_TYPE> list) {
88 return std::make_unique<RETURN_TYPE>(std::move(list));
89}
90
98template<typename RETURN_TYPE, typename RAW_TYPE>
100 return std::dynamic_pointer_cast<RETURN_TYPE>(raw_type);
101}
102
110template<typename RETURN_TYPE, typename RAW_TYPE>
112 RAW_TYPE *pointer = raw_type.release();
113 RETURN_TYPE *newPointer = dynamic_cast<RETURN_TYPE *>(pointer); // NOLINT
114 return std::unique_ptr<RETURN_TYPE>(newPointer);
115}
116} // namespace sese