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 WriteHelper non-inline template implementation */
24 //#include "WriteHelper.ih"
28 #include <boost/bind.hpp>
29 #include "../Utils/Exception.hh"
30 #include "Scheduler.hh"
33 ///////////////////////////////ct.p////////////////////////////////////////
35 template <class Handle>
36 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
39 fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
40 handle, scheduler::FdEvent::EV_WRITE),
41 data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
44 template <class Handle>
45 prefix_ std::string const & senf::WriteHelper<Handle>::data()
48 if (offset_ > data_.begin()) {
49 data_.erase(data_.begin(),offset_);
50 offset_ = data_.begin();
55 template <class Handle>
56 prefix_ void senf::WriteHelper<Handle>::revoke()
58 ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
60 fde_.action(0); // To remove the smart pointer reference to this
63 template <class Handle>
65 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
66 senf::Scheduler::EventId event)
68 // since we have a 'ptr' argument, the instance cannot be deleted
69 // before this method returns
70 return helper->process(handle,event);
73 template <class Handle>
74 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
75 senf::Scheduler::EventId event)
77 bool complete_ (false);
79 if (event != senf::Scheduler::EV_WRITE)
80 throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
81 offset_ = handle.write(std::make_pair(offset_,data_.end()));
82 if (offset_ == data_.end()) {
84 offset_ = data_.begin();
88 catch (senf::SystemException const & ex) {
89 errno_ = ex.errorNumber();
97 template <class Handle>
98 prefix_ void senf::WriteHelper<Handle>::done()
101 callback_(ptr(this));
104 ///////////////////////////////ct.e////////////////////////////////////////
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40