3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
5 // The contents of this file are subject to the Fraunhofer FOKUS Public License
6 // Version 1.0 (the "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 // http://senf.berlios.de/license.html
10 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
11 // but modifies the Mozilla Public License Version 1.1.
12 // See the full license text for the amendments.
14 // Software distributed under the License is distributed on an "AS IS" basis,
15 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 // for the specific language governing rights and limitations under the License.
18 // The Original Code is Fraunhofer FOKUS code.
20 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
21 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
22 // All Rights Reserved.
28 \brief ReadHelper non-inline template implementation */
30 #include "ReadHelper.ih"
33 #include <boost/bind.hpp>
34 #include <senf/Utils/Exception.hh>
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 template <class Handle>
40 prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
41 InternalPredicate * predicate, Callback cb)
43 fde_("senf::ReadHelper", boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1),
44 handle, scheduler::FdEvent::EV_READ),
45 maxSize_(maxSize), predicate_(predicate), callback_(cb), errno_(0), complete_(false)
47 // Here we add a *static* member taking a *smart* pointer as first
48 // argument instead of a simple bound-member as callback to the
49 // scheduler. This ensures, that the refcount is at least 1 as
50 // long as the helper is registered with the scheduler.
53 template <class Handle>
54 prefix_ void senf::ReadHelper<Handle>::revoke()
56 ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
58 fde_.action(0); // Remove smart pointer reference to this
61 template <class Handle>
63 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle, int event)
65 // since we have a 'ptr' argument, the instance cannot be deleted
66 // before this method returns
67 helper->process(handle,event);
70 template <class Handle>
71 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
74 if (event != scheduler::FdEvent::EV_READ)
75 throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
77 handle.read(rcv, maxSize_ - data_.size());
79 std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
80 if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
82 if (n < data_.size()) {
83 tail_.assign(data_,n,std::string::npos);
88 catch (senf::SystemException const & ex) {
89 errno_ = ex.errorNumber();
97 template <class Handle>
98 prefix_ void senf::ReadHelper<Handle>::done()
101 callback_(ptr(this));
104 template <class Handle>
105 template <class Predicate>
106 prefix_ std::string::size_type
107 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
108 operator()(std::string const & data)
110 return predicate(data);
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40