trikRuntime
differentOwnerPointer.h
Go to the documentation of this file.
1 /* Copyright 2016 Yurii Litvinov.
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 
15 #pragma once
16 
17 #include <QtCore/QSharedPointer>
18 
19 namespace trikKernel {
20 
24 template<typename T> class DifferentOwnerPointer
25 {
26 public:
29  : mPointer(other.mPointer)
30  , mPointerGuard(other.mPointerGuard)
31  {
32  }
33 
36  explicit DifferentOwnerPointer(T &foreignObject)
37  : mPointer(&foreignObject)
38  {
39  }
40 
44  explicit DifferentOwnerPointer(const QSharedPointer<T> &sharedObject)
45  : mPointer(sharedObject.data())
46  , mPointerGuard(sharedObject)
47  {
48  }
49 
51  explicit DifferentOwnerPointer(T * const ownObject)
52  : mPointer(ownObject)
53  , mPointerGuard(ownObject)
54  {
55  }
56 
58  inline T *operator->()
59  {
60  return mPointer;
61  }
62 
64  inline T &operator *()
65  {
66  return *mPointer;
67  }
68 
70  inline T *data() const
71  {
72  return mPointer;
73  }
74 
76  inline DifferentOwnerPointer<T> &operator =(const DifferentOwnerPointer<T> &other) = default;
77 
78 private:
80  T *mPointer;
81 
82  QSharedPointer<T> mPointerGuard;
83 };
84 
86 template<typename T>
88 {
89  return DifferentOwnerPointer<T>(foreignObject);
90 }
91 
93 template<typename T>
94 inline DifferentOwnerPointer<T> createDifferentOwnerPointer(const QSharedPointer<T> &sharedObject)
95 {
96  return DifferentOwnerPointer<T>(sharedObject);
97 }
98 
100 template<typename T>
102 {
103  return DifferentOwnerPointer<T>(ownObject);
104 }
105 
106 }
Definition: analogSensor.h:23
T * operator->()
Operator that allows to access members of stored object.
Definition: differentOwnerPointer.h:58
T & operator*()
Provides reference to stored object.
Definition: differentOwnerPointer.h:64
DifferentOwnerPointer< T > & operator=(const DifferentOwnerPointer< T > &other)=default
Assignment operator to provide value semantics.
DifferentOwnerPointer(T *const ownObject)
Constructor which takes raw pointer and takes ownership over it.
Definition: differentOwnerPointer.h:51
DifferentOwnerPointer(const QSharedPointer< T > &sharedObject)
Constructor which takes shared pointer to object and makes DifferentOwnerPointer behave like shared p...
Definition: differentOwnerPointer.h:44
DifferentOwnerPointer< T > createDifferentOwnerPointer(T &foreignObject)
Helper function that creates DifferentOwnerPointer that does not own object.
Definition: differentOwnerPointer.h:87
DifferentOwnerPointer(T &foreignObject)
Constructor which takes a reference to object owned by someone else, so we need only to store the ref...
Definition: differentOwnerPointer.h:36
T * data() const
Returns raw pointer to an object, does not transfer ownership.
Definition: differentOwnerPointer.h:70
DifferentOwnerPointer(const DifferentOwnerPointer< T > &other)
Copy constructor. Preserves ownership semantics of a copied object.
Definition: differentOwnerPointer.h:28
Smart pointer that can hold two different kinds of pointers: where ownership belongs to someone else ...
Definition: differentOwnerPointer.h:24