QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qprotobuflazymessagepointer.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once //QProtobufLazyMessagePointer
27 
28 #include "qtprotobufglobal.h"
29 #include <QQmlEngine>
30 #include <QObject>
31 
32 #include <memory>
33 #include <type_traits>
34 
35 template <typename T>
36 class QProtobufLazyMessagePointer {//TODO: final?
37 public:
38  QProtobufLazyMessagePointer(T *p = nullptr) : m_ptr(p) {}
39 
40  virtual ~QProtobufLazyMessagePointer() {
41  checkAndRelease();
42  QObject::disconnect(m_destroyed);
43  }
44 
45  typename std::add_lvalue_reference<T>::type operator *() const {
46  if (m_ptr == nullptr) {
47  m_ptr.reset(new T);
48  }
49  return *m_ptr;
50  }
51 
52  T *operator->() const noexcept {
53  if (m_ptr == nullptr) {
54  m_ptr.reset(new T);
55  }
56  m_ptr.operator->();
57  }
58 
59  T *get() const {
60  if (m_ptr == nullptr) {
61  m_ptr.reset(new T);
62  }
63  return m_ptr.get();
64  }
65 
66  bool operator ==(const QProtobufLazyMessagePointer &other) const {
67  return (m_ptr == nullptr && other.m_ptr == nullptr)
68  || (other.m_ptr == nullptr && *m_ptr == T{})
69  || (m_ptr == nullptr && *other.m_ptr == T{})
70  || (m_ptr != nullptr && other.m_ptr != nullptr && *m_ptr == *other.m_ptr);
71  }
72 
73  bool operator !=(const QProtobufLazyMessagePointer &other) const {
74  return !operator ==(other);
75  }
76 
77  void reset(T *p) const {
78  checkAndRelease();
79  QObject::disconnect(m_destroyed);
80  m_destroyed = QObject::connect(p, &QObject::destroyed, [this] {
81  QObject::disconnect(m_destroyed);
82  m_ptr.release();
83  });
84 
85  m_ptr.reset(p);
86  }
87 
88  QProtobufLazyMessagePointer(QProtobufLazyMessagePointer &&other) : m_ptr(std::move(other.m_ptr)) {}
90  m_ptr = std::move(other.m_ptr);
91  }
92 
93  explicit operator bool() const noexcept {
94  return m_ptr.operator bool();
95  }
96 
97 private:
98  void checkAndRelease() const {
99  if (m_ptr != nullptr && QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership) {
100  m_ptr.release();//In case if object owned by QML it's qml responsibility to clean it up
101  QObject::disconnect(m_destroyed);
102  }
103  }
104 
106  QProtobufLazyMessagePointer &operator =(const QProtobufLazyMessagePointer&) = delete;
107  mutable std::unique_ptr<T> m_ptr;
108  mutable QMetaObject::Connection m_destroyed;
109 };
Definition: qprotobuflazymessagepointer.h:36