Socket: Fix dynamic_socket_cast() / check_socket_cast() to support crosscasts
[senf.git] / Console / Parse.cci
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 /** \file
24     \brief Parse inline non-template implementation */
25
26 // We do NOT want to include the complete parser definition into every other compilation unit
27 // (disabled) #include "Parse.ih"
28
29 // Custom includes
30 #include "../Utils/senfassert.hh"
31
32 #define prefix_ inline
33 ///////////////////////////////cci.p///////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::console::ArgumentToken
37
38 prefix_ std::string const & senf::console::ArgumentToken::value()
39     const
40 {
41     return token_;
42 }
43
44 prefix_ senf::console::ArgumentToken::TokenType senf::console::ArgumentToken::type()
45     const
46 {
47     return type_;
48 }
49
50 prefix_ bool senf::console::ArgumentToken::is(unsigned tokens)
51     const
52 {
53     return tokens & type_;
54 }
55
56 prefix_ senf::console::ArgumentToken::ArgumentToken(TokenType type, std::string token)
57     : type_(type), token_ (token)
58 {}
59
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::console::ParseCommandInfo
62
63 prefix_ senf::console::ParseCommandInfo::BuiltinCommand
64 senf::console::ParseCommandInfo::builtin()
65     const
66 {
67     return builtin_;
68 }
69
70 prefix_ senf::console::ParseCommandInfo::CommandPathRange
71 senf::console::ParseCommandInfo::commandPath()
72     const
73 {
74     return boost::make_iterator_range(commandPath_.begin(), commandPath_.end());
75 }
76
77 prefix_ senf::console::ParseCommandInfo::ArgumentsRange
78 senf::console::ParseCommandInfo::arguments()
79     const
80 {
81     return boost::make_iterator_range( ArgumentIterator(tokens_.begin()),
82                                        ArgumentIterator(tokens_.end()) );
83 }
84
85 prefix_ senf::console::ParseCommandInfo::TokensRange senf::console::ParseCommandInfo::tokens()
86     const
87 {
88     return boost::make_iterator_range(tokens_.begin(), tokens_.end());
89 }
90
91 ////////////////////////////////////////
92 // private members
93
94 prefix_ void senf::console::ParseCommandInfo::init()
95 {
96     builtin_ = NoBuiltin;
97     commandPath_.clear();
98     tokens_.clear();
99 }
100
101 prefix_ void senf::console::ParseCommandInfo::setBuiltin(BuiltinCommand builtin)
102 {
103     builtin_ = builtin;
104 }
105
106 prefix_ void
107 senf::console::ParseCommandInfo::setCommand(std::vector<std::string> & commandPath)
108 {
109     commandPath_.clear();
110     commandPath_.swap(commandPath);
111 }
112
113 prefix_ void senf::console::ParseCommandInfo::addToken(ArgumentToken const & token)
114 {
115     tokens_.push_back(token);
116 }
117
118 ///////////////////////////////////////////////////////////////////////////
119 // senf::console::ParseCommandInfo::ArgumentIterator
120
121 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::ArgumentIterator()
122 {}
123
124 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::
125 ArgumentIterator(ParseCommandInfo::TokensRange::iterator i)
126     : b_(i), e_(i)
127 {}
128
129 prefix_ senf::console::ParseCommandInfo::ArgumentIterator::reference
130 senf::console::ParseCommandInfo::ArgumentIterator::dereference()
131     const
132 {
133     if (b_ == e_) setRange();
134     return b_->is(ArgumentToken::ArgumentGroupOpen) 
135         ? boost::make_iterator_range(boost::next(b_), boost::prior(e_))
136         : boost::make_iterator_range(b_, e_);
137 }
138
139 prefix_ bool
140 senf::console::ParseCommandInfo::ArgumentIterator::equal(ArgumentIterator const & other)
141     const
142 {
143     return b_ == other.b_;
144 }
145
146 prefix_ void senf::console::ParseCommandInfo::ArgumentIterator::increment()
147 {
148     if (b_ == e_) setRange();
149     b_ = e_;
150 }
151
152 ///////////////////////////////////////////////////////////////////////////
153
154 prefix_ senf::console::CheckedArgumentIteratorWrapper::
155 CheckedArgumentIteratorWrapper(ParseCommandInfo::ArgumentsRange const & range,
156                                std::string const & msg)
157     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
158 {}
159
160 prefix_ senf::console::CheckedArgumentIteratorWrapper::
161 CheckedArgumentIteratorWrapper(ParseCommandInfo::TokensRange const & range,
162                                std::string const & msg)
163     : i_ (range.begin()), e_ (range.end()), msg_ (msg)
164 {}
165
166 prefix_ senf::console::CheckedArgumentIteratorWrapper::~CheckedArgumentIteratorWrapper()
167 {
168     if (i_ != e_ && ! std::uncaught_exception())
169         throw SyntaxErrorException(msg_);
170 }
171
172 prefix_ senf::console::CheckedArgumentIteratorWrapper::operator ParseCommandInfo::ArgumentIterator()
173 {
174     return i_;
175 }
176
177 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::boolean_test()
178     const
179 {
180     return i_ != e_;
181 }
182
183 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::done()
184     const
185 {
186     return i_ == e_;
187 }
188
189 prefix_ void senf::console::CheckedArgumentIteratorWrapper::clear()
190 {
191     i_ = e_;
192 }
193
194 prefix_ senf::console::CheckedArgumentIteratorWrapper::reference
195 senf::console::CheckedArgumentIteratorWrapper::dereference()
196     const
197 {
198     if (i_ == e_)
199         throw SyntaxErrorException(msg_);
200     return *i_;
201 }
202
203 prefix_ void senf::console::CheckedArgumentIteratorWrapper::increment()
204 {
205     if (i_ == e_)
206         throw SyntaxErrorException(msg_);
207     ++ i_;
208 }
209
210 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
211 operator==(ParseCommandInfo::ArgumentIterator const & other)
212     const
213 {
214     return i_ == other;
215 }
216
217 prefix_ bool senf::console::CheckedArgumentIteratorWrapper::
218 operator!=(ParseCommandInfo::ArgumentIterator const & other)
219     const
220 {
221     return i_ != other;
222 }
223
224 prefix_ senf::console::ParseCommandInfo::ArgumentIterator
225 senf::console::CheckedArgumentIteratorWrapper::operator++(int)
226 {
227     ParseCommandInfo::ArgumentIterator i (i_);
228     increment();
229     return i;
230 }
231
232 ///////////////////////////////////////////////////////////////////////////
233 // senf::console::SingleCommandParser
234
235 prefix_ senf::console::CommandParser::Impl & senf::console::CommandParser::impl()
236 {
237     SENF_ASSERT(impl_);
238     return *impl_;
239 }
240
241 ///////////////////////////////cci.e///////////////////////////////////////
242 #undef prefix_
243
244 \f
245 // Local Variables:
246 // mode: c++
247 // fill-column: 100
248 // comment-column: 40
249 // c-file-style: "senf"
250 // indent-tabs-mode: nil
251 // ispell-local-dictionary: "american"
252 // compile-command: "scons -u test"
253 // End: