wibble
1.1
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
* (C) 2013 Petr Rockai <me@mornfall.net>
10
*
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2.1 of the License, or (at your option) any later version.
15
*
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
20
*
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
*/
25
26
#include <cstddef>
27
#include <iterator>
28
29
namespace
wibble {
30
31
template
<
typename
T>
32
class
Singleton
33
{
34
protected
:
35
T
value
;
36
37
public
:
38
typedef
T
value_type
;
39
40
class
const_iterator
:
public
std::iterator
<std::forward_iterator_tag, const T, void, const T*, const T&>
41
{
42
const
T* value;
43
44
protected
:
45
const_iterator
(
const
T* value) : value(value) {}
46
47
public
:
48
const_iterator
() : value(0) {}
49
50
const
T&
operator*
()
const
{
return
*value; }
51
const
T*
operator->
()
const
{
return
value; }
52
const_iterator
&
operator++
() { value = 0;
return
*
this
; }
53
bool
operator==
(
const
const_iterator
& iter)
const
{
return
value == iter.value; }
54
bool
operator!=
(
const
const_iterator
& iter)
const
{
return
value != iter.value; }
55
56
friend
class
Singleton
<T>;
57
};
58
59
class
iterator
:
public
std::iterator
<std::forward_iterator_tag, T, void, T*, T&>
60
{
61
T* value;
62
63
protected
:
64
iterator
(T* value) : value(value) {}
65
66
public
:
67
iterator
() : value(0) {}
68
69
T&
operator*
() {
return
*value; }
70
T*
operator->
() {
return
value; }
71
iterator
&
operator++
() { value = 0;
return
*
this
; }
72
bool
operator==
(
const
iterator
& iter)
const
{
return
value == iter.value; }
73
bool
operator!=
(
const
iterator
& iter)
const
{
return
value != iter.value; }
74
75
friend
class
Singleton
<T>;
76
};
77
78
explicit
Singleton
(
const
T&
value
) : value(value) {}
79
Singleton
() :
value
() {}
80
81
bool
empty
()
const
{
return
false
; }
82
size_t
size
()
const
{
return
1; }
83
84
iterator
begin
() {
return
iterator
(&
value
); }
85
iterator
end
() {
return
iterator
(); }
86
const_iterator
begin
()
const
{
return
const_iterator
(&
value
); }
87
const_iterator
end
()
const
{
return
const_iterator
(); }
88
89
iterator
insert
(
iterator
/* position */
,
const
value_type
&v )
90
{
91
value
= v;
92
return
begin
();
93
}
94
95
iterator
insert
(
const
value_type
&v ) {
96
value
= v;
97
return
begin
();
98
}
99
};
100
101
template
<
typename
T>
102
Singleton<T>
singleton
(
const
T& value)
103
{
104
return
Singleton<T>
(value);
105
}
106
107
}
108
109
// vim:set ts=4 sw=4:
110
#endif
Generated on Wed Oct 23 2013 17:14:25 for wibble by
1.8.4