singleton.h
Go to the documentation of this file.
// -*- C++ -*-
#ifndef WIBBLE_SINGLETON_H
#define WIBBLE_SINGLETON_H
/*
* Degenerated container to hold a single value
*
* Copyright (C) 2006 Enrico Zini <enrico@debian.org>
* (C) 2013 Petr Rockai <me@mornfall.net>
*
* 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 <cstddef>
#include <iterator>
namespace wibble {
template<typename T>
class Singleton
{
protected:
T value;
public:
typedef T value_type;
class const_iterator : public std::iterator<std::forward_iterator_tag, const T, void, const T*, const T&>
{
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<T>;
};
class iterator : public std::iterator<std::forward_iterator_tag, T, void, T*, T&>
{
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<T>;
};
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<typename T>
Singleton<T> singleton(const T& value)
{
return Singleton<T>(value);
}
}
// vim:set ts=4 sw=4:
#endif
wibble::Singleton::const_iterator::operator*
const T & operator*() const
Definition: singleton.h:50
wibble::iterator
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
wibble::Singleton::begin
iterator begin()
Definition: singleton.h:84
wibble::Singleton::value_type
T value_type
Definition: singleton.h:38
wibble::Singleton::begin
const_iterator begin() const
Definition: singleton.h:86
wibble::Singleton
Definition: singleton.h:32
wibble::Singleton::iterator::operator->
T * operator->()
Definition: singleton.h:70
wibble::Singleton::iterator::operator!=
bool operator!=(const iterator &iter) const
Definition: singleton.h:73
wibble::Singleton::size
size_t size() const
Definition: singleton.h:82
wibble::Singleton::const_iterator::operator++
const_iterator & operator++()
Definition: singleton.h:52
wibble::Singleton::Singleton
Singleton()
Definition: singleton.h:79
wibble::Singleton::end
const_iterator end() const
Definition: singleton.h:87
wibble::Singleton::empty
bool empty() const
Definition: singleton.h:81
wibble::singleton
Singleton< T > singleton(const T &value)
Definition: singleton.h:102
wibble::Singleton::iterator
Definition: singleton.h:59
wibble::Singleton::iterator::operator*
T & operator*()
Definition: singleton.h:69
wibble::Singleton::const_iterator::operator->
const T * operator->() const
Definition: singleton.h:51
wibble::Singleton::insert
iterator insert(iterator, const value_type &v)
Definition: singleton.h:89
wibble::Singleton::const_iterator::const_iterator
const_iterator()
Definition: singleton.h:48
wibble::Singleton::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const
Definition: singleton.h:54
wibble::Singleton::value
T value
Definition: singleton.h:35
wibble::Singleton::iterator::operator++
iterator & operator++()
Definition: singleton.h:71
wibble::Singleton::insert
iterator insert(const value_type &v)
Definition: singleton.h:95
wibble::Singleton::const_iterator
Definition: singleton.h:40
wibble
Definition: amorph.h:17
wibble::Singleton::const_iterator::const_iterator
const_iterator(const T *value)
Definition: singleton.h:45
wibble::Singleton::const_iterator::operator==
bool operator==(const const_iterator &iter) const
Definition: singleton.h:53
wibble::Singleton::iterator::operator==
bool operator==(const iterator &iter) const
Definition: singleton.h:72
wibble::Singleton::end
iterator end()
Definition: singleton.h:85
wibble::Singleton::iterator::iterator
iterator()
Definition: singleton.h:67
wibble::Singleton::Singleton
Singleton(const T &value)
Definition: singleton.h:78
wibble::Singleton::iterator::iterator
iterator(T *value)
Definition: singleton.h:64