Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
Memory.h
Go to the documentation of this file.
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
22#pragma once
23#include <memory>
24
25#ifdef _WIN32
26#pragma warning(disable : 4819)
27#endif
28
29namespace sese {
30
31template<typename T>
32using SharedType = std::shared_ptr<T>;
33
34template<typename T>
35using UniqueType = std::unique_ptr<T>;
36
37template<typename T>
38using WeakType = std::weak_ptr<T>;
39
47template<typename RETURN_TYPE, typename... ARGS>
48inline SharedType<RETURN_TYPE> makeShared(ARGS &&...args) {
49 return std::make_shared<RETURN_TYPE>(args...);
50}
51
59template<typename RETURN_TYPE, typename... ARGS>
60inline SharedType<RETURN_TYPE> makeUnique(ARGS &&...args) {
61 return std::make_unique<RETURN_TYPE>(args...);
62}
63
64#define MAKE_SHARED_PRIVATE(RETURN_TYPE, ...) sese::SharedType<RETURN_TYPE>(new RETURN_TYPE(__VA_ARGS__))
65
66#define MAKE_UNIQUE_PRIVATE(RETURN_TYPE, ...) sese::UniqueType<RETURN_TYPE>(new RETURN_TYPE(__VA_ARGS__))
67
75template<typename RETURN_TYPE, typename INIT_TYPE>
76SharedType<RETURN_TYPE> makeSharedFromList(std::initializer_list<INIT_TYPE> list) {
77 return std::make_shared<RETURN_TYPE>(std::move(list));
78}
79
87template<typename RETURN_TYPE, typename INIT_TYPE>
88UniqueType<RETURN_TYPE> makeUniqueFromList(std::initializer_list<INIT_TYPE> list) {
89 return std::make_unique<RETURN_TYPE>(std::move(list));
90}
91
99template<typename RETURN_TYPE, typename RAW_TYPE>
101 return std::dynamic_pointer_cast<RETURN_TYPE>(raw_type);
102}
103
111template<typename RETURN_TYPE, typename RAW_TYPE>
113 RAW_TYPE *pointer = raw_type.release();
114 RETURN_TYPE *newPointer = dynamic_cast<RETURN_TYPE *>(pointer); // NOLINT
115 return std::unique_ptr<RETURN_TYPE>(newPointer);
116}
117} // namespace sese