53e3d8a974f2eda560047b049b08ddaac7b0975a
[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     /** \fixme What, if pos == end() / default constructed iterator ? */
86     size_type ix (pos.raw()-container_.begin());
87     container_.insert(pos.raw(),t.bytes(),0);
88     Parser(container_.begin()+ix).value(t);
89 }
90
91 template <class Parser, class Sentinel, class Container>
92 template <class Value>
93 prefix_ void
94 senf::Parse_ListS_wrapper<Parser,Sentinel,Container>::insert(iterator pos, size_type n,
95                                                                     Value const & t)
96 {
97     size_type ix (pos.raw()-container_.begin());
98     container_.insert(pos.raw(),n*t.bytes(),0);
99     typename Container::iterator i (container_.begin()+ix);
100     for (; n; ++i, --n)
101         Parser(i).value(t);
102 }
103
104 template <class Parser, class Sentinel, class Container>
105 template <class InputIterator>
106 prefix_ void
107 senf::Parse_ListS_wrapper<Parser,Sentinel,Container>::insert(iterator pos,
108                                                                     InputIterator f,
109                                                                     InputIterator l)
110 {
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 // c-file-style: "senf"
121 // End: