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 "../Utils/Exception.hh"
29 #include "Scheduler.hh"
32 ///////////////////////////////ct.p////////////////////////////////////////
34 template <class Handle>
35 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
37 : handle_(handle), data_(data), callback_(callback),
38 offset_(data_.begin()), errno_(0)
40 senf::Scheduler::instance()
41 .add(handle_, boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
42 senf::Scheduler::EV_WRITE);
45 template <class Handle>
46 prefix_ std::string const & senf::WriteHelper<Handle>::data()
49 if (offset_ > data_.begin()) {
50 data_.erase(data_.begin(),offset_);
51 offset_ = data_.begin();
56 template <class Handle>
57 prefix_ void senf::WriteHelper<Handle>::revoke()
59 ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
60 senf::Scheduler::instance()
61 .remove(handle_, senf::Scheduler::EV_WRITE);
64 template <class Handle>
66 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
67 senf::Scheduler::EventId event)
69 // since we have a 'ptr' argument, the instance cannot be deleted
70 // before this method returns
71 return helper->process(handle,event);
74 template <class Handle>
75 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
76 senf::Scheduler::EventId event)
78 bool complete_ (false);
80 if (event != senf::Scheduler::EV_WRITE)
81 throw SystemException(EPIPE);
82 offset_ = handle.write(std::make_pair(offset_,data_.end()));
83 if (offset_ == data_.end()) {
85 offset_ = data_.begin();
89 catch (senf::SystemException const & ex) {
90 errno_ = ex.errorNumber();
98 template <class Handle>
99 prefix_ void senf::WriteHelper<Handle>::done()
102 callback_(ptr(this));
105 ///////////////////////////////ct.e////////////////////////////////////////
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40