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 public header */
24 #ifndef HH_WriteHelper_
25 #define HH_WriteHelper_ 1
29 #include <boost/intrusive_ptr.hpp>
30 #include <boost/function.hpp>
31 #include "Utils/intrusive_refcount.hh"
32 #include "Scheduler.hh"
34 //#include "WriteHelper.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
39 /** \brief Asyncronous writing helper
41 This class provides a simple asynchronous writing facility. This helper will register with
42 the Scheduler to write the requested data. It will stay registered until the data has been
43 completely sent or some error condition is encountered. As soon as the WriteHelper is done,
44 the callback will be called.
46 The WriteHelper accepts the same flexible file handle interfaces as the Scheduler.
48 The callback must take a WriteHelper::ptr argument. Using this WriteHelper instance, the
49 callback can access the state information and check the termination status.
51 \todo Add additional interface to better access the intermediate status (data sent so far)
53 template <class Handle>
55 : public senf::intrusive_refcount
58 ///////////////////////////////////////////////////////////////////////////
61 typedef boost::intrusive_ptr<WriteHelper> ptr; ///< Smart pointer type for this class
62 typedef boost::function<void (ptr)> Callback; ///< Callback type
64 ///////////////////////////////////////////////////////////////////////////
65 ///\name Structors and default members
68 static ptr dispatch(Handle handle, std::string data, Callback callback);
69 ///< Register new WriteHelper instance
70 /**< The registered callback will be called after all \a
71 data has been sent or when some error condition is
73 \param[in] handle file descriptor or handle providing
74 the Handle interface defined above.
75 \param[in] data data to send
76 \param[in] callback callback
77 \returns smart pointer to new WriteHelper instance */
80 ///////////////////////////////////////////////////////////////////////////
82 Handle handle() const;
84 std::string const & data() const; ///< Return the data
85 /**< After all data has been sent, this member will return
86 an empty string. Until then, the complete string will
89 bool complete() const; ///< Check whether the write has completed successfully
90 bool error() const; ///< Check for error condition
91 void throw_error() const; ///< If an error occurred, throw it
93 void revoke(); ///< Remove the WriteHelper from the scheduler
98 WriteHelper(Handle handle, std::string data, Callback callback);
100 static void dispatchProcess(ptr helper, Handle handle, senf::Scheduler::EventId event);
101 void process(Handle handle, senf::Scheduler::EventId event);
105 mutable std::string data_;
108 mutable std::string::size_type offset_;
115 ///////////////////////////////hh.e////////////////////////////////////////
116 //#include "WriteHelper.cci"
117 #include "WriteHelper.ct"
118 #include "WriteHelper.cti"
119 //#include "WriteHelper.mpp"
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"