wibble
0.1.28
Main Page
Namespaces
Classes
Files
File List
File Members
wibble
singleton.h
Go to the documentation of this file.
1
// -*- C++ -*-
2
#ifndef WIBBLE_SINGLETON_H
3
#define WIBBLE_SINGLETON_H
4
5
/*
6
* Degenerated container to hold a single value
7
*
8
* Copyright (C) 2006 Enrico Zini <enrico@debian.org>
9
*
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
14
*
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
19
*
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
*/
24
25
#include <cstddef>
26
#include <iterator>
27
28
namespace
wibble {
29
30
template
<
typename
T>
31
class
Singleton
32
{
33
protected
:
34
T
value
;
35
36
public
:
37
typedef
T
value_type
;
38
39
class
const_iterator
:
public
std::iterator
<std::forward_iterator_tag, const T, void, const T*, const T&>
40
{
41
const
T* value;
42
43
protected
:
44
const_iterator
(
const
T* value) : value(value) {}
45
46
public
:
47
const_iterator
() : value(0) {}
48
49
const
T&
operator*
()
const
{
return
*value; }
50
const
T*
operator->
()
const
{
return
value; }
51
const_iterator
&
operator++
() { value = 0;
return
*
this
; }
52
bool
operator==
(
const
const_iterator
& iter)
const
{
return
value == iter.value; }
53
bool
operator!=
(
const
const_iterator
& iter)
const
{
return
value != iter.value; }
54
55
friend
class
Singleton
<T>;
56
};
57
58
class
iterator
:
public
std::iterator
<std::forward_iterator_tag, T, void, T*, T&>
59
{
60
T* value;
61
62
protected
:
63
iterator
(T* value) : value(value) {}
64
65
public
:
66
iterator
() : value(0) {}
67
68
T&
operator*
() {
return
*value; }
69
T*
operator->
() {
return
value; }
70
iterator
&
operator++
() { value = 0;
return
*
this
; }
71
bool
operator==
(
const
iterator
& iter)
const
{
return
value == iter.value; }
72
bool
operator!=
(
const
iterator
& iter)
const
{
return
value != iter.value; }
73
74
friend
class
Singleton
<T>;
75
};
76
77
explicit
Singleton
(
const
T&
value
) : value(value) {}
78
79
bool
empty
()
const
{
return
false
; }
80
size_t
size
()
const
{
return
1; }
81
82
iterator
begin
() {
return
iterator
(&
value
); }
83
iterator
end
() {
return
iterator
(); }
84
const_iterator
begin
()
const
{
return
const_iterator
(&
value
); }
85
const_iterator
end
()
const
{
return
const_iterator
(); }
86
};
87
88
template
<
typename
T>
89
Singleton<T>
singleton
(
const
T& value)
90
{
91
return
Singleton<T>
(value);
92
}
93
94
}
95
96
// vim:set ts=4 sw=4:
97
#endif
Generated on Thu Jun 14 2012 18:39:42 for wibble by
1.8.1