wibble 0.1.28
singleton.h
Go to the documentation of this file.
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 <iterator>
00027 
00028 namespace wibble {
00029 
00030 template<typename T>
00031 class Singleton
00032 {
00033 protected:
00034     T value;
00035 
00036 public:
00037     typedef T value_type;
00038 
00039     class const_iterator : public std::iterator<std::forward_iterator_tag, const T, void, const T*, const T&>
00040     {
00041         const T* value;
00042 
00043     protected:
00044         const_iterator(const T* value) : value(value) {}
00045 
00046     public:
00047         const_iterator() : value(0) {}
00048 
00049         const T& operator*() const { return *value; }
00050         const T* operator->() const { return value; }
00051         const_iterator& operator++() { value = 0; return *this; }
00052         bool operator==(const const_iterator& iter) const { return value == iter.value; }
00053         bool operator!=(const const_iterator& iter) const { return value != iter.value; }
00054 
00055         friend class Singleton<T>;
00056     };
00057 
00058     class iterator : public std::iterator<std::forward_iterator_tag, T, void, T*, T&>
00059     {
00060         T* value;
00061 
00062     protected:
00063         iterator(T* value) : value(value) {}
00064 
00065     public:
00066         iterator() : value(0) {}
00067 
00068         T& operator*() { return *value; }
00069         T* operator->() { return value; }
00070         iterator& operator++() { value = 0; return *this; }
00071         bool operator==(const iterator& iter) const { return value == iter.value; }
00072         bool operator!=(const iterator& iter) const { return value != iter.value; }
00073 
00074         friend class Singleton<T>;
00075     };
00076 
00077     explicit Singleton(const T& value) : value(value) {}
00078 
00079     bool empty() const { return false; }
00080     size_t size() const { return 1; }
00081 
00082     iterator begin() { return iterator(&value); }
00083     iterator end() { return iterator(); }
00084     const_iterator begin() const { return const_iterator(&value); }
00085     const_iterator end() const { return const_iterator(); }
00086 };
00087 
00088 template<typename T>
00089 Singleton<T> singleton(const T& value)
00090 {
00091     return Singleton<T>(value);
00092 }
00093 
00094 }
00095 
00096 // vim:set ts=4 sw=4:
00097 #endif