// $Id$ // // Copyright (C) 2006 Stefan Bund // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the // Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Copyright (C) 2006 /** \file \brief WriteHelper non-inline template implementation */ //#include "WriteHelper.ih" // Custom includes #include #include #define prefix_ //-///////////////////////////////////////////////////////////////////////////////////////////////// template prefix_ senf::WriteHelper::WriteHelper(Handle handle, std::string const & data, Callback callback) : handle_(handle), fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2), handle, scheduler::FdEvent::EV_WRITE), data_(data), callback_(callback), offset_(data_.begin()), errno_(0) {} template prefix_ std::string const & senf::WriteHelper::data() const { if (offset_ > data_.begin()) { data_.erase(data_.begin(),offset_); offset_ = data_.begin(); } return data_; } template prefix_ void senf::WriteHelper::revoke() { ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ... fde_.disable(); fde_.action(0); // To remove the smart pointer reference to this } template prefix_ void senf::WriteHelper::dispatchProcess(ptr helper, Handle handle, scheduler::FdEvent::Events event) { // since we have a 'ptr' argument, the instance cannot be deleted // before this method returns helper->process(handle, event); } template prefix_ void senf::WriteHelper::process(Handle handle, scheduler::FdEvent::Events event) { bool complete_ (false); try { if (event != scheduler::FdEvent::EV_WRITE) throw SystemException(EPIPE SENF_EXC_DEBUGINFO); offset_ = handle.write(std::make_pair(offset_,data_.end())); if (offset_ == data_.end()) { data_.erase(); offset_ = data_.begin(); complete_ = true; } } catch (senf::SystemException const & ex) { errno_ = ex.errorNumber(); done(); return; } if (complete_) done(); } template prefix_ void senf::WriteHelper::done() { revoke(); callback_(ptr(this)); } //-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_ // Local Variables: // mode: c++ // fill-column: 100 // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" // compile-command: "scons -u test" // comment-column: 40 // End: