// -*- C++ -*- #ifndef WIBBLE_SINGLETON_H #define WIBBLE_SINGLETON_H /* * Degenerated container to hold a single value * * Copyright (C) 2006 Enrico Zini * (C) 2013 Petr Rockai * * This library 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 2.1 of the License, or (at your option) any later version. * * This library 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. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include namespace wibble { template class Singleton { protected: T value; public: typedef T value_type; class const_iterator : public std::iterator { const T* value; protected: const_iterator(const T* value) : value(value) {} public: const_iterator() : value(0) {} const T& operator*() const { return *value; } const T* operator->() const { return value; } const_iterator& operator++() { value = 0; return *this; } bool operator==(const const_iterator& iter) const { return value == iter.value; } bool operator!=(const const_iterator& iter) const { return value != iter.value; } friend class Singleton; }; class iterator : public std::iterator { T* value; protected: iterator(T* value) : value(value) {} public: iterator() : value(0) {} T& operator*() { return *value; } T* operator->() { return value; } iterator& operator++() { value = 0; return *this; } bool operator==(const iterator& iter) const { return value == iter.value; } bool operator!=(const iterator& iter) const { return value != iter.value; } friend class Singleton; }; explicit Singleton(const T& value) : value(value) {} Singleton() : value() {} bool empty() const { return false; } size_t size() const { return 1; } iterator begin() { return iterator(&value); } iterator end() { return iterator(); } const_iterator begin() const { return const_iterator(&value); } const_iterator end() const { return const_iterator(); } iterator insert( iterator /* position */, const value_type &v ) { value = v; return begin(); } iterator insert( const value_type &v ) { value = v; return begin(); } }; template Singleton singleton(const T& value) { return Singleton(value); } } // vim:set ts=4 sw=4: #endif