3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
5 // The contents of this file are subject to the Fraunhofer FOKUS Public License
6 // Version 1.0 (the "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 // http://senf.berlios.de/license.html
10 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
11 // but modifies the Mozilla Public License Version 1.1.
12 // See the full license text for the amendments.
14 // Software distributed under the License is distributed on an "AS IS" basis,
15 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 // for the specific language governing rights and limitations under the License.
18 // The Original Code is Fraunhofer FOKUS code.
20 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
21 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
22 // All Rights Reserved.
28 \brief WriteHelper public header */
30 #ifndef HH_SENF_Scheduler_WriteHelper_
31 #define HH_SENF_Scheduler_WriteHelper_ 1
35 #include <boost/intrusive_ptr.hpp>
36 #include <boost/function.hpp>
37 #include <senf/Utils/intrusive_refcount.hh>
40 //#include "WriteHelper.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 /** \brief Asynchronous writing helper
47 This class provides a simple asynchronous writing facility. This helper will register with
48 the Scheduler to write the requested data. It will stay registered until the data has been
49 completely sent or some error condition is encountered. As soon as the WriteHelper is done,
50 the callback will be called.
52 The WriteHelper accepts the same flexible file handle interfaces as the Scheduler.
54 The callback must take a WriteHelper::ptr argument. Using this WriteHelper instance, the
55 callback can access the state information and check the termination status.
57 \todo Add additional interface to better access the intermediate status (data sent so far)
59 template <class Handle>
61 : public senf::intrusive_refcount
64 //-////////////////////////////////////////////////////////////////////////
67 typedef boost::intrusive_ptr<WriteHelper> ptr; ///< Smart pointer type for this class
68 typedef boost::function<void (ptr)> Callback; ///< Callback type
70 //-////////////////////////////////////////////////////////////////////////
71 ///\name Structors and default members
74 static ptr dispatch(Handle handle, std::string const & data, Callback callback);
75 ///< Register new WriteHelper instance
76 /**< The registered callback will be called after all \a
77 data has been sent or when some error condition is
79 \param[in] handle file descriptor or handle providing
80 the Handle interface defined above.
81 \param[in] data data to send
82 \param[in] callback callback
83 \returns smart pointer to new WriteHelper instance */
86 //-////////////////////////////////////////////////////////////////////////
88 Handle handle() const;
90 std::string const & data() const; ///< Return the data
91 /**< After all data has been sent, this member will return
92 an empty string. Until then, the complete string will
95 bool complete() const; ///< Check whether the write has completed successfully
96 bool error() const; ///< Check for error condition
97 void throw_error() const; ///< If an error occurred, throw it
99 void revoke(); ///< Remove the WriteHelper from the scheduler
104 WriteHelper(Handle handle, std::string const & data, Callback callback);
106 static void dispatchProcess(ptr helper, Handle handle,
107 senf::scheduler::FdEvent::Events event);
108 void process(Handle handle, senf::scheduler::FdEvent::Events event);
112 scheduler::FdEvent fde_;
113 mutable std::string data_;
116 mutable std::string::iterator offset_;
123 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 //#include "WriteHelper.cci"
125 #include "WriteHelper.ct"
126 #include "WriteHelper.cti"
127 //#include "WriteHelper.mpp"
134 // c-file-style: "senf"
135 // indent-tabs-mode: nil
136 // ispell-local-dictionary: "american"
137 // compile-command: "scons -u test"
138 // comment-column: 40