winss
not_owning_ptr.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_NOT_OWNING_PTR_HPP_
18 #define LIB_WINSS_NOT_OWNING_PTR_HPP_
19 
20 #include <stdexcept>
21 
22 namespace winss {
32 template <class T>
33 class NotOwningPtr {
34  private:
35  T* p;
37  public:
41  explicit NotOwningPtr(T* p) : p(p) {
42  if (p == nullptr) {
43  throw std::invalid_argument("cannot own a null pointer");
44  }
45  }
46 
50  template <class V>
51  NotOwningPtr(const NotOwningPtr<V>& ptr) : p(ptr.Get()) {}
52 
58  T* Get() {
59  return p;
60  }
61 
67  T* Get() const {
68  return p;
69  }
70 
76  T& operator*() {
77  return *p;
78  }
79 
85  T& operator*() const {
86  return *p;
87  }
88 
94  T* operator->() {
95  return p;
96  }
97 
103  T* operator->() const {
104  return p;
105  }
106 
113  bool operator==(const NotOwningPtr& ptr) const {
114  return p == ptr.p;
115  }
116 
123  bool operator!=(const NotOwningPtr& ptr) const {
124  return p != ptr.p;
125  }
126 
134  p = ptr.p;
135  return *this;
136  }
137 };
138 
146 template <class T>
148  return NotOwningPtr<T>(p);
149 }
150 } // namespace winss
151 
152 #endif // LIB_WINSS_NOT_OWNING_PTR_HPP_
T * Get()
Gets the not owned pointer.
Definition: not_owning_ptr.hpp:58
T * operator->() const
Gets the not owned pointer as a constant.
Definition: not_owning_ptr.hpp:103
NotOwningPtr(const NotOwningPtr< V > &ptr)
Copy constructor to allow copying the pointer.
Definition: not_owning_ptr.hpp:51
bool operator==(const NotOwningPtr &ptr) const
Checks equality of this not owned pointer and another.
Definition: not_owning_ptr.hpp:113
NotOwningPtr(T *p)
Create a new NotOwningPtr with a pointer to the actual object.
Definition: not_owning_ptr.hpp:41
Definition: case_ignore.hpp:23
T * Get() const
Gets the not owned pointer as a constant.
Definition: not_owning_ptr.hpp:67
bool operator!=(const NotOwningPtr &ptr) const
Checks inequality of this not owned pointer and another.
Definition: not_owning_ptr.hpp:123
T & operator*()
Gets the dereferenced object.
Definition: not_owning_ptr.hpp:76
T & operator*() const
Gets the dereferenced object as a constant.
Definition: not_owning_ptr.hpp:85
T * operator->()
Gets the not owned pointer.
Definition: not_owning_ptr.hpp:94
A container for pointers where the lifetime should be owned by the caller.
Definition: not_owning_ptr.hpp:33
NotOwningPtr< T > NotOwned(T *p)
Helper method to make it easier to create a not owned pointer.
Definition: not_owning_ptr.hpp:147
NotOwningPtr & operator=(const NotOwningPtr &ptr)
Assigns the pointer to another pointer.
Definition: not_owning_ptr.hpp:133