root/tlate/basic_sequential_value.hh

Revision 173e2ce96a7bde630476defe34fae1b5ed1baaba, 3.6 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 16 months ago)

#79: Kill tlate::msg_value precursor and followup lists if they are empty

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, 2011 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_BASIC_SEQUENTIAL_VALUE_HH
21#define GUARD_TLATE_BASIC_SEQUENTIAL_VALUE_HH
22
23#include "sequential_value.hh"
24
25namespace tlate
26{
27        template <typename Seq, typename ValFac>
28        class basic_sequential_value : public sequential_value
29        {
30                class vit : public virtual_iterator
31                {
32                        typename Seq::const_iterator it;
33                        mutable value::const_ptr cur;
34                        ValFac factory;
35                public:
36                        vit(typename Seq::const_iterator it, ValFac factory)
37                                : it(it)
38                                , factory(factory)
39                                {}
40                        virtual_iterator *clone() const {
41                                return new vit(*this);
42                        }
43                        value::const_ptr get() const {
44                                if (!cur) cur = factory(*it);
45                                return cur;
46                        }
47                        void next() { cur.reset(); it++; }
48                        bool eq(const virtual_iterator &o_) const {
49                                const vit *o = dynamic_cast<const vit *>(&o_);
50                                return o ? it == o->it : false;
51                        }
52                };
53               
54                Seq seq;
55                ValFac factory;
56        public:
57                typedef boost::shared_ptr<basic_sequential_value<Seq,ValFac> >
58                ptr;
59                typedef boost::shared_ptr
60                <const basic_sequential_value<Seq,ValFac> >
61                const_ptr;
62
63                basic_sequential_value(Seq seq, ValFac factory)
64                        : seq(seq)
65                        , factory(factory)
66                        {}
67
68                Seq &operator*() { return seq; }
69                const Seq &operator*() const { return seq; }
70                Seq *operator->() { return &seq; }
71                const Seq *operator->() const { return &seq; }
72                Seq &get() { return seq; }
73                const Seq &get() const { return seq; }
74
75                virtual const_iterator begin() const {
76                        return const_iterator(new vit(seq.begin(), factory));
77                }
78                virtual const_iterator end() const {
79                        return const_iterator(new vit(seq.end(), factory));
80                }
81        };
82
83        template <typename Seq, typename ValFac>
84        boost::shared_ptr<basic_sequential_value<Seq,ValFac> >
85        seqval(Seq seq, ValFac vf)
86        {
87                boost::shared_ptr<basic_sequential_value<Seq,ValFac> > rv;
88                if (seq.begin() != seq.end())
89                        rv.reset(new basic_sequential_value<Seq,ValFac>
90                                 (seq, vf));
91                return rv;
92        }
93};
94
95#endif /* GUARD_TLATE_BASIC_SEQUENTIAL_VALUE_HH */
Note: See TracBrowser for help on using the browser.