i6engine  1.0
weakPtr.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_WEAKPTR_H__
26 #define __I6ENGINE_UTILS_WEAKPTR_H__
27 
29 
30 namespace i6e {
31 namespace utils {
32 
37  template<typename T, typename Base = T>
38  class weakPtr {
39  public:
43  weakPtr() : _sharedPtrWrapper(std::make_shared<sharedPtrWrapper<Base>>(nullptr)), _ptr(nullptr) {
44  }
45 
49  weakPtr(const weakPtr & other) : _sharedPtrWrapper(other._sharedPtrWrapper), _ptr(other._ptr) {
50  }
51 
55  weakPtr(weakPtr && other) : _sharedPtrWrapper(other._sharedPtrWrapper), _ptr(other._ptr) {
56  other._sharedPtrWrapper.reset();
57  other._ptr = nullptr;
58  }
59 
63  weakPtr(const sharedPtr<T, Base> & other) : _sharedPtrWrapper(other._sharedPtrWrapper), _ptr(other._ptr) {
64  }
65 
69  template<typename U>
70  weakPtr(const sharedPtr<U, T> & other) : _sharedPtrWrapper(other._sharedPtrWrapper), _ptr(other._ptr) {
71  }
72 
76  template<typename U, typename V>
77  weakPtr(const sharedPtr<U, V> & other) : _sharedPtrWrapper(other._sharedPtrWrapper), _ptr(other._ptr) {
78  }
79 
84  }
85 
89  const weakPtr & operator=(const weakPtr & other) {
90  if (*this == other) {
91  return *this;
92  }
93  _sharedPtrWrapper = other._sharedPtrWrapper;
94  _ptr = other._ptr;
95 
96  return *this;
97  }
98 
102  const weakPtr & operator=(weakPtr && other) {
103  if (*this == other) {
104  return *this;
105  }
106  _sharedPtrWrapper = other._sharedPtrWrapper;
107  _ptr = other._ptr;
108  other._sharedPtrWrapper.reset();
109  other._ptr = nullptr;
110 
111  return *this;
112  }
113 
117  bool operator==(const weakPtr & other) const {
118  return _ptr == other._ptr;
119  }
120 
124  bool operator==(const T * other) const {
125  return _ptr == other;
126  }
127 
128  bool operator!=(const weakPtr & other) const {
129  return !(*this == other);
130  }
131 
132  bool operator!=(const T * other) const {
133  return !(*this == other);
134  }
135 
136  bool operator<(const weakPtr & other) const {
137  return _ptr < other._ptr;
138  }
139 
143  sharedPtr<T, Base> get() const {
144  auto p = _sharedPtrWrapper.lock();
145  return sharedPtr<T, Base>((p == nullptr) ? nullptr : _ptr, p);
146  }
147 
151  bool expired() const {
152  return _sharedPtrWrapper.expired();
153  }
154 
155  private:
156  std::weak_ptr<sharedPtrWrapper<Base>> _sharedPtrWrapper;
157  T * _ptr;
158  };
159 
160 } /* namespace utils */
161 } /* namespace i6e */
162 
163 #endif /* __I6ENGINE_UTILS_WEAKPTR_H__ */
164 
weakPtr(weakPtr &&other)
move constructor
Definition: weakPtr.h:55
const weakPtr & operator=(const weakPtr &other)
assignment operator
Definition: weakPtr.h:89
A weak pointer observing a sharedPtr.
Definition: sharedPtr.h:53
struct handling threadsafe destruction of the wrapped pointer
Definition: sharedPtr.h:41
bool expired() const
return true, if pointer is invalid, otherwise false
Definition: weakPtr.h:151
weakPtr(const weakPtr &other)
copy constructor copying another weak pointer
Definition: weakPtr.h:49
bool operator<(const weakPtr &other) const
Definition: weakPtr.h:136
weakPtr(const sharedPtr< U, V > &other)
constructor takes sharedPtr with different types
Definition: weakPtr.h:77
bool operator!=(const T *other) const
Definition: weakPtr.h:132
A shared pointer counting references and adds objects being not referenced any more to an internal li...
Definition: sharedPtr.h:50
weakPtr()
default constructor
Definition: weakPtr.h:43
~weakPtr()
destructor, removes sharedPtrWrapper if all weak and sharedPtr are removed
Definition: weakPtr.h:83
bool operator!=(const weakPtr &other) const
Definition: weakPtr.h:128
bool operator==(const weakPtr &other) const
comparison between two weak pointer
Definition: weakPtr.h:117
bool operator==(const T *other) const
comparison between weak and normal pointer
Definition: weakPtr.h:124
sharedPtr< T, U > make_shared(Args &&...)
const weakPtr & operator=(weakPtr &&other)
move assignment operator
Definition: weakPtr.h:102
weakPtr(const sharedPtr< T, Base > &other)
constructor takes sharedPtr with same base class
Definition: weakPtr.h:63
weakPtr(const sharedPtr< U, T > &other)
constructor takes sharedPtr with same base class
Definition: weakPtr.h:70