PLaSK library

Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__MEMORY_H
15#define PLASK__MEMORY_H
16
22#include <plask/config.hpp>
23
24#ifdef PLASK_SHARED_PTR_STD
25
26#include <memory>
27 namespace plask {
28 using std::shared_ptr;
29 using std::make_shared;
30 using std::dynamic_pointer_cast;
31 using std::static_pointer_cast;
32 using std::const_pointer_cast;
33 using std::weak_ptr;
34 using std::enable_shared_from_this;
35}
36
37#else // PLASK_SHARED_PTR_STD
38// Use boost::shared_ptr
39
40#define BOOST_BIND_GLOBAL_PLACEHOLDERS
41
42#include <boost/shared_ptr.hpp>
43#include <boost/make_shared.hpp>
44#include <boost/weak_ptr.hpp>
45#include <boost/enable_shared_from_this.hpp>
46 namespace plask {
47 using boost::shared_ptr;
48 using boost::make_shared;
49 using boost::dynamic_pointer_cast;
50 using boost::static_pointer_cast;
51 using boost::const_pointer_cast;
52 using boost::weak_ptr;
53 using boost::enable_shared_from_this;
54}
55
56#endif // PLASK_SHARED_PTR_STD
57
58
59 namespace plask {
69 template <typename T>
70 struct Holder {
71
72 protected:
73
75 T* held;
76
77 public:
78
83 Holder(T* held) noexcept: held(held) {}
84
89 Holder(T& held) noexcept: held(&held) {}
90
95 Holder(const Holder<T>& to_copy): held(to_copy.held->clone()) {}
96
103 Holder(Holder<T>&& to_move) noexcept: held(to_move.held) { to_move.held = nullptr; }
104
109 Holder<T>& operator=(const Holder& to_copy) {
110 if (held == to_copy.held) return *this; //self-assignment protection
111 delete held;
112 held = to_copy.held->clone();
113 return *this;
114 }
115
122 Holder<T>& operator=(Holder&& to_move) noexcept {
123 std::swap(held, to_move.held); // to_move destructor will delete this old held for a moment
124 return *this;
125 }
126
128 ~Holder() { delete held; }
129
130};
131
140 template <typename T>
141 struct HolderRef {
142
143 protected:
144
146 shared_ptr<T> held;
147
148 public:
149
150 HolderRef() {}
151
156 HolderRef(T* held): held(held) {}
157
158 bool isNotNull() const { return held != nullptr; }
159
160 bool isNull() const { return !held; }
161
162 void reset(T* new_held) { this->held.reset(new_held); }
163
164};
165
172 template <typename T>
173 inline shared_ptr<T> getUnique(const shared_ptr<T>& ptr) {
174 return ptr.unique() ? ptr : new T(*ptr);
175}
176
183 template <typename T>
184 inline shared_ptr<T> getUnique(const shared_ptr<const T>& ptr) {
185 return ptr.unique() ? ptr : new T(*ptr);
186}
187
193 template <typename T>
194 inline void makeUnique(shared_ptr<T>& ptr) {
195 ptr = getUnique(ptr);
196}
197
198} // namespace plask
199
200#endif // PLASK__MEMORY_H