// $Id$ // // Copyright (C) 2006 Stefan Bund // // The contents of this file are subject to the Fraunhofer FOKUS Public License // Version 1.0 (the "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // http://senf.berlios.de/license.html // // The Fraunhofer FOKUS Public License Version 1.0 is based on, // but modifies the Mozilla Public License Version 1.1. // See the full license text for the amendments. // // Software distributed under the License is distributed on an "AS IS" basis, // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License // for the specific language governing rights and limitations under the License. // // The Original Code is Fraunhofer FOKUS code. // // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. // (registered association), Hansastraße 27 c, 80686 Munich, Germany. // All Rights Reserved. // // Contributor(s): // 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: