File indexing completed on 2024-05-12 04:02:18

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_DEFINITIONREF_P_H
0008 #define KSYNTAXHIGHLIGHTING_DEFINITIONREF_P_H
0009 
0010 #include "definition.h"
0011 
0012 #include <memory>
0013 
0014 namespace KSyntaxHighlighting
0015 {
0016 class Definition;
0017 class DefinitionData;
0018 
0019 /** Weak reference for Definition instances.
0020  *
0021  * This must be used when holding Definition instances
0022  * in objects hold directly or indirectly by Definition
0023  * to avoid reference count loops and thus memory leaks.
0024  *
0025  * This class follows the rule of zero. It is implicitly movable and copyable.
0026  *
0027  * @internal
0028  */
0029 class DefinitionRef
0030 {
0031 public:
0032     DefinitionRef();
0033     explicit DefinitionRef(const Definition &def) noexcept;
0034     DefinitionRef &operator=(const Definition &def) noexcept;
0035 
0036     Definition definition() const;
0037 
0038     /**
0039      * Checks two definition references for equality.
0040      */
0041     bool operator==(const DefinitionRef &other) const;
0042 
0043     /**
0044      * Checks two definition references for inequality.
0045      */
0046     bool operator!=(const DefinitionRef &other) const
0047     {
0048         return !(*this == other);
0049     }
0050 
0051     /**
0052      * Checks two definition for equality.
0053      */
0054     bool operator==(const Definition &other) const;
0055 
0056     /**
0057      * Checks two definition for inequality.
0058      */
0059     bool operator!=(const Definition &other) const
0060     {
0061         return !(*this == other);
0062     }
0063 
0064 private:
0065     friend class DefinitionData;
0066     std::weak_ptr<DefinitionData> d;
0067 };
0068 
0069 }
0070 
0071 #endif