File indexing completed on 2024-05-05 16:27:23

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 #ifndef KGRAPHVIEWER_SINGLETON_H
0020 #define KGRAPHVIEWER_SINGLETON_H
0021 
0022 /**
0023  * An implementation of the singleton pattern
0024  *
0025  * @short Singleton pattern implementation
0026  * @author Gaƫl de Chalendar <kleag@free.fr>
0027  */
0028 template<typename Object> class Singleton
0029 {
0030 public:
0031     /**
0032      * @brief const singleton accessor
0033      */
0034     static const Object &single();
0035 
0036     /**
0037      * @brief singleton accessor
0038      */
0039     static Object &changeable();
0040 
0041 private:
0042     static Object *s_instance;
0043 };
0044 
0045 template<typename Object> Object *Singleton<Object>::s_instance(nullptr);
0046 
0047 template<typename Object> const Object &Singleton<Object>::single()
0048 {
0049     if (s_instance == nullptr) {
0050         s_instance = new Object();
0051     }
0052     return *s_instance;
0053 }
0054 
0055 template<typename Object> Object &Singleton<Object>::changeable()
0056 {
0057     if (s_instance == nullptr) {
0058         s_instance = new Object();
0059     }
0060     return *s_instance;
0061 }
0062 
0063 #endif