a96196f844a9bf1dd62708d8b27e95c3e613edb1
[senf.git] / Packets / ParseListS.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 // Definition of non-inline template functions
24
25 #include "ParseListS.ih"
26
27 // Custom includes
28 #include <algorithm>
29
30 #define prefix_
31 ///////////////////////////////ct.p////////////////////////////////////////
32
33 template <class Parser, class Sentinel, class Iterator, class IPacket>
34 prefix_ unsigned senf::Parse_ListS<Parser,Sentinel,Iterator,IPacket>::bytes()
35     const
36 {
37     iterator i (begin());
38     iterator e (end());
39     size_type s (0);
40     for (; i!=e; ++i)
41         s += i->bytes();
42     // The sentinel is not part of the range
43     // but it's part of the list !!
44     return s+i->bytes();
45 }
46
47 template <class Parser, class Sentinel, class Iterator, class IPacket>
48 prefix_ bool
49 senf::Parse_ListS<Parser,Sentinel,Iterator,IPacket>::check(Iterator const & e)
50     const
51 {
52     byte_iterator i (this->i());
53     for (;;) {
54         value_type v (i);
55         if (!v.check(e)) return false;
56         if (sentinel::check(v)) return true;
57         size_type s (v.bytes());
58         if (s==0) return false;
59         i += s;
60     }
61 }
62
63 template <class Parser, class Sentinel, class Iterator, class IPacket>
64 prefix_ void senf::Parse_ListS<Parser,Sentinel,Iterator,IPacket>::init()
65     const
66 {
67     iterator i (begin());
68     iterator e (end());
69     for (;i!=e; ++i)
70         i->init();
71     // The sentinel is not part of the range
72     // but it's part of the list !!
73     i->init();
74 }
75
76 ///////////////////////////////////////////////////////////////////////////
77 // senf::Parse_ListS_wrapper<Parser,Sentinel,Container>
78
79 template <class Parser, class Sentinel, class Container>
80 template <class Value>
81 prefix_ void
82 senf::Parse_ListS_wrapper<Parser,Sentinel,Container>::insert(iterator pos,
83                                                              Value const & t)
84 {
85     size_type ix (pos.raw()-container_.begin());
86     container_.insert(pos.raw(),t.bytes(),0);
87     Parser(container_.begin()+ix).value(t);
88 }
89
90 template <class Parser, class Sentinel, class Container>
91 template <class Value>
92 prefix_ void
93 senf::Parse_ListS_wrapper<Parser,Sentinel,Container>::insert(iterator pos, size_type n,
94                                                              Value const & t)
95 {
96     size_type ix (pos.raw()-container_.begin());
97     container_.insert(pos.raw(),n*t.bytes(),0);
98     typename Container::iterator i (container_.begin()+ix);
99     for (; n; ++i, --n)
100         Parser(i).value(t);
101 }
102
103 template <class Parser, class Sentinel, class Container>
104 template <class InputIterator>
105 prefix_ void
106 senf::Parse_ListS_wrapper<Parser,Sentinel,Container>::insert(iterator pos,
107                                                              InputIterator f,
108                                                              InputIterator l)
109 {
110     /** \todo Optimize this for random-access and multi-pass iterators */
111     for (;f!=l;++f,++pos) insert(pos,*f);
112 }
113
114 ///////////////////////////////ct.e////////////////////////////////////////
115 #undef prefix_
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // comment-column: 40
126 // End: