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),_1,_2),
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,
61 senf::Scheduler::EventId event)
63 // since we have a 'ptr' argument, the instance cannot be deleted
64 // before this method returns
65 return helper->process(handle,event);
68 template <class Handle>
69 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,
70 senf::Scheduler::EventId event)
72 /** \fixme Move the done() calls to outside the try/catch block */
74 if (event != senf::Scheduler::EV_READ)
75 throw SystemException(EPIPE);
76 std::string rcv (handle.read(maxSize_ - data_.size()));
78 std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
79 if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
81 if (n < data_.size()) {
82 tail_.assign(data_,n,std::string::npos);
88 catch (senf::SystemException const & ex) {
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////////////////////////////////////////