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