qprotobuflazymessagepointer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once //QProtobufLazyMessagePointer
  26. #include "qtprotobufglobal.h"
  27. #include <QObject>
  28. #if defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
  29. # include <QQmlEngine>
  30. #endif
  31. #include <memory>
  32. #include <type_traits>
  33. template <typename T>
  34. class QProtobufLazyMessagePointer {//TODO: final?
  35. public:
  36. QProtobufLazyMessagePointer(T *p = nullptr) : m_ptr(p) {}
  37. virtual ~QProtobufLazyMessagePointer() {
  38. checkAndRelease();
  39. QObject::disconnect(m_destroyed);
  40. }
  41. typename std::add_lvalue_reference<T>::type operator *() const {
  42. if (m_ptr == nullptr) {
  43. m_ptr.reset(new T);
  44. }
  45. return *m_ptr;
  46. }
  47. T *operator->() const noexcept {
  48. if (m_ptr == nullptr) {
  49. m_ptr.reset(new T);
  50. }
  51. m_ptr.operator->();
  52. }
  53. T *get() const {
  54. if (m_ptr == nullptr) {
  55. m_ptr.reset(new T);
  56. }
  57. return m_ptr.get();
  58. }
  59. bool operator ==(const QProtobufLazyMessagePointer &other) const {
  60. return (m_ptr == nullptr && other.m_ptr == nullptr)
  61. || (other.m_ptr == nullptr && *m_ptr == T{})
  62. || (m_ptr == nullptr && *other.m_ptr == T{})
  63. || (m_ptr != nullptr && other.m_ptr != nullptr && *m_ptr == *other.m_ptr);
  64. }
  65. bool operator !=(const QProtobufLazyMessagePointer &other) const {
  66. return !operator ==(other);
  67. }
  68. void reset(T *p) const {
  69. checkAndRelease();
  70. QObject::disconnect(m_destroyed);
  71. m_destroyed = QObject::connect(p, &QObject::destroyed, [this] {
  72. QObject::disconnect(m_destroyed);
  73. m_ptr.release();
  74. });
  75. m_ptr.reset(p);
  76. }
  77. QProtobufLazyMessagePointer(QProtobufLazyMessagePointer &&other) : m_ptr(std::move(other.m_ptr)) {}
  78. QProtobufLazyMessagePointer &operator =(QProtobufLazyMessagePointer &&other) {
  79. m_ptr = std::move(other.m_ptr);
  80. }
  81. explicit operator bool() const noexcept {
  82. return m_ptr.operator bool();
  83. }
  84. private:
  85. void checkAndRelease() const {
  86. #if defined(QT_QML_LIB)
  87. bool qmlCheck = QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership;
  88. #else
  89. bool qmlCheck = true;
  90. #endif
  91. if (m_ptr != nullptr && qmlCheck) {
  92. m_ptr.release();//In case if object owned by QML it's qml responsibility to clean it up
  93. QObject::disconnect(m_destroyed);
  94. }
  95. }
  96. QProtobufLazyMessagePointer(const QProtobufLazyMessagePointer&) = delete;
  97. QProtobufLazyMessagePointer &operator =(const QProtobufLazyMessagePointer&) = delete;
  98. mutable std::unique_ptr<T> m_ptr;
  99. mutable QMetaObject::Connection m_destroyed;
  100. };