00001 // -*- C++ -*- 00002 #ifndef WIBBLE_SINGLETON_H 00003 #define WIBBLE_SINGLETON_H 00004 00005 /* 00006 * Degenerated container to hold a single value 00007 * 00008 * Copyright (C) 2006 Enrico Zini <enrico@debian.org> 00009 * 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 00025 #include <cstddef> 00026 #include <bits/stl_iterator_base_types.h> 00027 00028 #include <cstddef> 00029 #include <bits/stl_iterator_base_types.h> 00030 00031 namespace wibble { 00032 00033 template<typename T> 00034 class Singleton 00035 { 00036 protected: 00037 T value; 00038 00039 public: 00040 typedef T value_type; 00041 00042 class const_iterator : public std::iterator<std::forward_iterator_tag, const T, void, const T*, const T&> 00043 { 00044 const T* value; 00045 00046 protected: 00047 const_iterator(const T* value) : value(value) {} 00048 00049 public: 00050 const_iterator() : value(0) {} 00051 00052 const T& operator*() const { return *value; } 00053 const T* operator->() const { return value; } 00054 const_iterator& operator++() { value = 0; return *this; } 00055 bool operator==(const const_iterator& iter) const { return value == iter.value; } 00056 bool operator!=(const const_iterator& iter) const { return value != iter.value; } 00057 00058 friend class Singleton<T>; 00059 }; 00060 00061 class iterator : public std::iterator<std::forward_iterator_tag, T, void, T*, T&> 00062 { 00063 T* value; 00064 00065 protected: 00066 iterator(T* value) : value(value) {} 00067 00068 public: 00069 iterator() : value(0) {} 00070 00071 T& operator*() { return *value; } 00072 T* operator->() { return value; } 00073 iterator& operator++() { value = 0; return *this; } 00074 bool operator==(const iterator& iter) const { return value == iter.value; } 00075 bool operator!=(const iterator& iter) const { return value != iter.value; } 00076 00077 friend class Singleton<T>; 00078 }; 00079 00080 explicit Singleton(const T& value) : value(value) {} 00081 00082 bool empty() const { return false; } 00083 size_t size() const { return 1; } 00084 00085 iterator begin() { return iterator(&value); } 00086 iterator end() { return iterator(); } 00087 const_iterator begin() const { return const_iterator(&value); } 00088 const_iterator end() const { return const_iterator(); } 00089 }; 00090 00091 template<typename T> 00092 Singleton<T> singleton(const T& value) 00093 { 00094 return Singleton<T>(value); 00095 } 00096 00097 } 00098 00099 // vim:set ts=4 sw=4: 00100 #endif