3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief ReadHelper non-inline template implementation */
24 #include "ReadHelper.ih"
28 #include <boost/bind.hpp>
29 #include <senf/Utils/Exception.hh>
30 #include "Scheduler.hh"
33 ///////////////////////////////ct.p////////////////////////////////////////
35 template <class Handle>
36 prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
37 InternalPredicate * predicate, Callback cb)
39 fde_("senf::ReadHelper", boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1),
40 handle, senf::scheduler::FdEvent::EV_READ),
41 maxSize_(maxSize), predicate_(predicate), callback_(cb), errno_(0), complete_(false)
43 // Here we add a *static* member taking a *smart* pointer as first
44 // argumnet instead of a simple bound-member as callback to the
45 // scheduler. This ensures, that the refcount is at least 1 as
46 // long as the helper is registered with the scheduler.
49 template <class Handle>
50 prefix_ void senf::ReadHelper<Handle>::revoke()
52 ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
54 fde_.action(0); // Remove smart pointer reference to this
57 template <class Handle>
59 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle, int event)
61 // since we have a 'ptr' argument, the instance cannot be deleted
62 // before this method returns
63 return helper->process(handle,event);
66 template <class Handle>
67 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
70 if (event != senf::scheduler::FdEvent::EV_READ)
71 throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
73 handle.read(rcv, maxSize_ - data_.size());
75 std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
76 if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
78 if (n < data_.size()) {
79 tail_.assign(data_,n,std::string::npos);
84 catch (senf::SystemException const & ex) {
85 errno_ = ex.errorNumber();
93 template <class Handle>
94 prefix_ void senf::ReadHelper<Handle>::done()
100 template <class Handle>
101 template <class Predicate>
102 prefix_ std::string::size_type
103 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
104 operator()(std::string const & data)
106 return predicate(data);
109 ///////////////////////////////ct.e////////////////////////////////////////
116 // c-file-style: "senf"
117 // indent-tabs-mode: nil
118 // ispell-local-dictionary: "american"
119 // compile-command: "scons -u test"
120 // comment-column: 40