Move include files in debian packge into 'senf' subdirectory
[senf.git] / Scheduler / WriteHelper.ct
1 // $Id$
2 //
3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
4 //
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.
9 //
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.
14 //
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.
19 // Copyright (C) 2006
20
21 /** \file
22     \brief WriteHelper non-inline template implementation */
23
24 //#include "WriteHelper.ih"
25
26 // Custom includes
27 #include <errno.h>
28 #include "../Utils/Exception.hh"
29 #include "Scheduler.hh"
30
31 #define prefix_
32 ///////////////////////////////ct.p////////////////////////////////////////
33
34 template <class Handle>
35 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
36                                                       Callback callback)
37     : handle_(handle), data_(data), callback_(callback),
38       offset_(data_.begin()), errno_(0)
39 {
40     senf::Scheduler::instance()
41         .add(handle_, boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
42              senf::Scheduler::EV_WRITE);
43 }
44
45 template <class Handle>
46 prefix_ std::string const & senf::WriteHelper<Handle>::data()
47     const
48 {
49     if (offset_ > data_.begin()) {
50         data_.erase(data_.begin(),offset_);
51         offset_ = data_.begin();
52     }
53     return data_;
54 }
55
56 template <class Handle>
57 prefix_ void senf::WriteHelper<Handle>::revoke()
58 {
59     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
60     senf::Scheduler::instance()
61         .remove(handle_, senf::Scheduler::EV_WRITE);
62 }
63
64 template <class Handle>
65 prefix_ void
66 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
67                                                   senf::Scheduler::EventId event)
68 {
69     // since we have a 'ptr' argument, the instance cannot be deleted
70     // before this method returns
71     return helper->process(handle,event);
72 }
73
74 template <class Handle>
75 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
76                                                        senf::Scheduler::EventId event)
77 {
78     bool complete_ (false);
79     try {
80         if (event != senf::Scheduler::EV_WRITE)
81             throw senf::SystemException(EPIPE);
82         offset_ = handle.write(std::make_pair(offset_,data_.end()));
83         if (offset_ == data_.end()) {
84             data_.erase();
85             offset_ = data_.begin();
86             complete_ = true;
87         }
88     }
89     catch (senf::SystemException const & ex) {
90         errno_ = ex.err;
91         done();
92         return;
93     }
94     if (complete_)
95         done();
96 }
97
98 template <class Handle>
99 prefix_ void senf::WriteHelper<Handle>::done()
100 {
101     revoke();
102     callback_(ptr(this));
103 }
104
105 ///////////////////////////////ct.e////////////////////////////////////////
106 #undef prefix_
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40
117 // End: