39ea4808fee96120dc7af8dc7e4767566af317c4
[senf.git] / Scheduler / ReadHelper.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4
5
6 #ifndef HH_ReadHelper_
7 #define HH_ReadHelper_ 1
8
9 // Custom includes
10 #include <string>
11 #include <boost/function.hpp>
12 #include <boost/intrusive_ptr.hpp>
13 #include <boost/scoped_ptr.hpp>
14
15 #include "Utils/intrusive_refcount.hh"
16 #include "Scheduler.hh"
17
18 //#include "ReadHelper.mpp"
19 ///////////////////////////////hh.p////////////////////////////////////////
20
21 namespace senf {
22
23
24     /** \brief
25
26         \todo Move all not Handle dependent members to a ReadHandleBase class
27      */
28     template <class Handle>
29     class ReadHelper
30         : public senf::intrusive_refcount
31     {
32     public:
33         ///////////////////////////////////////////////////////////////////////////
34         // Types
35         
36         typedef boost::intrusive_ptr<ReadHelper> ptr;
37         typedef boost::function<void (ptr)> Callback;
38
39         ///////////////////////////////////////////////////////////////////////////
40         ///\name Structors and default members
41         ///@{
42
43         static ptr dispatch(Handle handle, std::string::size_type maxSize, 
44                             Callback callback);
45
46         template <class Predicate>
47         static ptr dispatch(Handle handle, std::string::size_type maxSize, Predicate predicate,
48                             Callback callback);
49
50         ///@}
51         ///////////////////////////////////////////////////////////////////////////
52
53         Handle handle() const;
54         unsigned maxSize() const;
55
56         std::string const & data() const;
57         std::string const & tail() const;
58
59         bool complete() const;
60         bool error() const;
61         void throw_error() const;
62
63         void revoke();
64
65     protected:
66
67     private:
68         struct InternalPredicate;
69
70         ReadHelper(Handle handle, unsigned maxSize,  InternalPredicate * predicate, Callback cb);
71
72         static void dispatchProcess(ptr helper, Handle handle, senf::Scheduler::EventId event);
73         void process(Handle handle, senf::Scheduler::EventId event);
74         void done();
75
76         Handle handle_;
77         std::string::size_type maxSize_;
78         boost::scoped_ptr<InternalPredicate> predicate_;
79         Callback callback_;
80
81         std::string data_;
82         std::string tail_;
83         int errno_;
84         bool complete_;
85     };
86
87     struct ReadUntil
88     {
89         ReadUntil(std::string target);
90         std::string::size_type operator()(std::string data);
91         std::string target;
92     };
93
94 }
95
96 ///////////////////////////////hh.e////////////////////////////////////////
97 #include "ReadHelper.cci"
98 #include "ReadHelper.ct"
99 #include "ReadHelper.cti"
100 //#include "ReadHelper.mpp"
101 #endif
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // End: