root/tlate/sequential_value.hh

Revision 868b365c127bbf4f0b52f22da03e32a94a4f6653, 4.0 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 3 years ago)

Update the project blurb

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#ifndef GUARD_TLATE_SEQUENTIAL_VALUE_HH
21#define GUARD_TLATE_SEQUENTIAL_VALUE_HH
22
23#include "value.hh"
24
25namespace tlate
26{
27        class sequential_value : public value
28        {
29        protected:
30                class virtual_iterator
31                {
32                public:
33                        virtual ~virtual_iterator() {}
34                        virtual virtual_iterator *clone() const = 0;
35                        virtual value::const_ptr get() const = 0;
36                        virtual void next() = 0;
37                        virtual bool eq(const virtual_iterator &) const = 0;
38                };
39        public:
40                typedef boost::shared_ptr<sequential_value> ptr;
41                typedef boost::shared_ptr<const sequential_value> const_ptr;
42
43                class const_iterator
44                {
45                        virtual_iterator *it;
46                public:
47                        // transfers ownership of "it"
48                        const_iterator(virtual_iterator *it)
49                                : it(it)
50                                {}
51                        const_iterator(const const_iterator &other)
52                                : it(other.it->clone())
53                                {}
54                        const_iterator &operator=(const const_iterator &other) {
55                                if (this == &other) return *this;
56                                delete it;
57                                it = other.it->clone();
58                                return *this;
59                        }
60                        ~const_iterator() { delete it; }
61                        const value &operator*() const { return *get(); }
62                        const value *operator->() const {
63                                return get().get();
64                        }
65                        value::const_ptr get() const { return it->get(); }
66                        const_iterator &operator++() {
67                                it->next();
68                                return *this;
69                        }
70                        const_iterator operator++(int) {
71                                const_iterator rv(*this);
72                                it->next();
73                                return rv;
74                        }
75                        bool operator==(const const_iterator &other) const {
76                                return it->eq(*other.it);
77                        }
78                        bool operator!=(const const_iterator &other) const {
79                                return !(*this == other);
80                        }
81                };
82
83                sequential_value() {}
84
85                virtual const_iterator begin() const = 0;
86                virtual const_iterator end() const = 0;
87
88                static const_ptr from(value::const_ptr v) {
89                        const_ptr p = boost::dynamic_pointer_cast
90                                <const sequential_value>
91                                (v);
92                        return p;
93                }
94                void print(std::ostream &os) const {
95                        for (const_iterator it = begin(); it != end(); it++) {
96                                os << it.get();
97                        }
98                }
99        };
100};
101
102#endif /* GUARD_TLATE_SEQUENTIAL_VALUE_HH */
Note: See TracBrowser for help on using the browser.