File indexing completed on 2024-05-12 05:42:40
0001 #include <cutehmi/Singleton.hpp> 0002 0003 #include <cutehmi/test/random.hpp> 0004 0005 #include <QtTest/QtTest> 0006 #include <QSignalSpy> 0007 0008 //! [MySingleton class] 0009 class MySingleton: 0010 public cutehmi::Singleton<MySingleton> 0011 { 0012 friend class cutehmi::Singleton<MySingleton>; 0013 0014 public: 0015 void saySomething() 0016 { 0017 qInfo() << "I am not married!"; 0018 } 0019 0020 protected: 0021 MySingleton() = default; 0022 }; 0023 //! [MySingleton class] 0024 0025 //! [QObject singleton] 0026 class QObjectSingleton: 0027 public QObject, 0028 public cutehmi::Singleton<QObjectSingleton> 0029 { 0030 Q_OBJECT 0031 0032 friend class cutehmi::Singleton<QObjectSingleton>; 0033 0034 public: 0035 void sayQObject() 0036 { 0037 qInfo() << "QObject!"; 0038 emit qobjectSaid(); 0039 } 0040 0041 signals: 0042 void qobjectSaid(); 0043 0044 protected: 0045 QObjectSingleton() = default; 0046 }; 0047 //! [QObject singleton] 0048 0049 class snippet_Singleton: 0050 public QObject 0051 { 0052 Q_OBJECT 0053 0054 private slots: 0055 void saySomething(); 0056 0057 void sayQObject(); 0058 }; 0059 0060 void snippet_Singleton::saySomething() 0061 { 0062 //! [Call singleton function] 0063 MySingleton::Instance().saySomething(); 0064 //! [Call singleton function] 0065 } 0066 0067 void snippet_Singleton::sayQObject() 0068 { 0069 QSignalSpy qobjectSaidSpy(& QObjectSingleton::Instance(), & QObjectSingleton::qobjectSaid); 0070 QObjectSingleton::Instance().sayQObject(); 0071 QCOMPARE(qobjectSaidSpy.count(), 1); 0072 } 0073 0074 0075 QTEST_MAIN(snippet_Singleton) 0076 #include "snippet_Singleton.moc" 0077 0078 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved. 0079 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT 0080 //(c)C: This file is a part of CuteHMI. 0081 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 0082 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 0083 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>. 0084 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below. 0085 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 0086 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 0087 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.