File: singleton.h

package info (click to toggle)
libwibble 1.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,040 kB
  • ctags: 2,721
  • sloc: cpp: 14,542; makefile: 196; perl: 87; sh: 26
file content (110 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// -*- 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