1 /* bzflag 2 * Copyright (c) 1993-2021 Tim Riker 3 * 4 * This package is free software; you can redistribute it and/or 5 * modify it under the terms of the license found in the file 6 * named COPYING that should have accompanied this file. 7 * 8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 11 */ 12 13 #ifndef __SINGLETON_H__ 14 #define __SINGLETON_H__ 15 16 /* Singleton template class 17 * 18 * This template is based on an implementation suggested by Martin York in 19 * https://stackoverflow.com/a/1008289/9220132 20 * 21 * This template class pattern provides a traditional Singleton pattern. 22 * Allows you to designate a single-instance global class by inheriting 23 * from the template class. 24 * 25 * Example: 26 * 27 * class Whatever : public Singleton<Whatever> ... 28 * 29 * The class will need to provide an accessible default constructor 30 * 31 */ 32 template < typename T > 33 class Singleton 34 { 35 public: 36 37 /** returns a singleton 38 */ instance()39 static T& instance() 40 { 41 static T private_instance; 42 return private_instance; 43 } 44 45 Singleton(const Singleton &) = delete; 46 Singleton& operator=(const Singleton&) = delete; 47 48 protected: 49 Singleton() = default; 50 ~Singleton() = default; 51 52 }; 53 54 #endif /* __SINGLETON_H__ */ 55 56 57 // Local Variables: *** 58 // mode: C++ *** 59 // tab-width: 4 *** 60 // c-basic-offset: 4 *** 61 // indent-tabs-mode: nil *** 62 // End: *** 63 // ex: shiftwidth=4 tabstop=4 64