// $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 ReadHelper non-inline template implementation */ #include "ReadHelper.ih" // Custom includes #include #include #define prefix_ //-///////////////////////////////////////////////////////////////////////////////////////////////// template prefix_ senf::ReadHelper::ReadHelper(Handle handle, std::string::size_type maxSize, InternalPredicate * predicate, Callback cb) : handle_(handle), fde_("senf::ReadHelper", boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1), handle, scheduler::FdEvent::EV_READ), maxSize_(maxSize), predicate_(predicate), callback_(cb), errno_(0), complete_(false) { // Here we add a *static* member taking a *smart* pointer as first // argument instead of a simple bound-member as callback to the // scheduler. This ensures, that the refcount is at least 1 as // long as the helper is registered with the scheduler. } template prefix_ void senf::ReadHelper::revoke() { ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ... fde_.disable(); fde_.action(0); // Remove smart pointer reference to this } template prefix_ void senf::ReadHelper::dispatchProcess(ptr helper, Handle handle, int event) { // since we have a 'ptr' argument, the instance cannot be deleted // before this method returns helper->process(handle,event); } template prefix_ void senf::ReadHelper::process(Handle handle,int event) { try { if (event != scheduler::FdEvent::EV_READ) throw SystemException(EPIPE SENF_EXC_DEBUGINFO); std::string rcv; handle.read(rcv, maxSize_ - data_.size()); data_.append(rcv); std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos; if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) { complete_ = true; if (n < data_.size()) { tail_.assign(data_,n,std::string::npos); data_.erase(n); } } } catch (senf::SystemException const & ex) { errno_ = ex.errorNumber(); done(); return; } if (complete_) done(); } template prefix_ void senf::ReadHelper::done() { revoke(); callback_(ptr(this)); } template template prefix_ std::string::size_type senf::ReadHelper::InternalPredicate::Dispatcher:: operator()(std::string const & data) { return predicate(data); } //-///////////////////////////////////////////////////////////////////////////////////////////////// #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: