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 "../Utils/membind.hh"
29 #include "../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)
38 : handle_(handle), maxSize_(maxSize), predicate_(predicate), callback_(cb),
39 errno_(0), complete_(false)
41 // Here we add a *static* member taking a *smart* pointer as first
42 // argumnet instead of a simple bound-member as callback to the
43 // scheduler. This ensures, that the refcount is at least 1 as
44 // long as the helper is registered with the scheduler.
45 senf::Scheduler::instance()
46 .add(handle,boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1),
47 senf::Scheduler::EV_READ);
50 template <class Handle>
51 prefix_ void senf::ReadHelper<Handle>::revoke()
53 ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
54 senf::Scheduler::instance()
55 .remove(handle_,senf::Scheduler::EV_READ);
58 template <class Handle>
60 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle, int event)
62 // since we have a 'ptr' argument, the instance cannot be deleted
63 // before this method returns
64 return helper->process(handle,event);
67 template <class Handle>
68 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
71 if (event != senf::Scheduler::EV_READ)
72 throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
74 handle.read(rcv, maxSize_ - data_.size());
76 std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
77 if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
79 if (n < data_.size()) {
80 tail_.assign(data_,n,std::string::npos);
85 catch (senf::SystemException const & ex) {
86 errno_ = ex.errorNumber();
94 template <class Handle>
95 prefix_ void senf::ReadHelper<Handle>::done()
101 template <class Handle>
102 template <class Predicate>
103 prefix_ std::string::size_type
104 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
105 operator()(std::string const & data)
107 return predicate(data);
110 ///////////////////////////////ct.e////////////////////////////////////////
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // comment-column: 40