File indexing completed on 2024-05-12 09:55:38
0001 /* 0002 SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com> 0003 SPDX-FileCopyrightText: 2022 Christoph Cullmann <cullmann@kde.org> 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <KTextEditor/Document> 0010 #include <QWidget> 0011 0012 #include <variant> 0013 0014 // TODO: Find a better name for this class 0015 0016 // Just a helper class which we use internally to manage widgets/docs 0017 class DocOrWidget : public std::variant<KTextEditor::Document *, QWidget *> 0018 { 0019 public: 0020 using variant::variant; 0021 0022 auto *doc() const 0023 { 0024 return std::holds_alternative<KTextEditor::Document *>(*this) ? std::get<KTextEditor::Document *>(*this) : nullptr; 0025 } 0026 0027 auto *widget() const 0028 { 0029 return std::holds_alternative<QWidget *>(*this) ? std::get<QWidget *>(*this) : nullptr; 0030 } 0031 0032 QObject *qobject() const 0033 { 0034 return doc() ? static_cast<QObject *>(doc()) : static_cast<QObject *>(widget()); 0035 } 0036 0037 bool operator==(KTextEditor::Document *doc) const 0038 { 0039 return this->doc() == doc; 0040 } 0041 bool operator==(QWidget *w) const 0042 { 0043 return this->widget() == w; 0044 } 0045 0046 static DocOrWidget null() 0047 { 0048 DocOrWidget d = static_cast<KTextEditor::Document *>(nullptr); 0049 return d; 0050 } 0051 0052 bool isNull() const 0053 { 0054 return !qobject(); 0055 } 0056 0057 void clear() 0058 { 0059 *this = null(); 0060 } 0061 }; 0062 Q_DECLARE_METATYPE(DocOrWidget) 0063 0064 namespace std 0065 { 0066 template<> 0067 struct hash<DocOrWidget> { 0068 typedef DocOrWidget argument_type; 0069 typedef std::size_t result_type; 0070 result_type operator()(argument_type const &s) const noexcept 0071 { 0072 result_type const h1(std::hash<void *>{}(s.qobject())); 0073 return h1; 0074 } 0075 }; 0076 }