[go: up one dir, main page]

Menu

[e222c1]: / io / completion_handler.h  Maximize  Restore  History

Download this file

86 lines (75 with data), 2.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/***************************************************************************
* completion_handler.h
*
* Mon Apr 21 02:45:45 2003
* Copyright 2003 Roman Dementiev
* dementiev@mpi-sb.mpg.de
* Loki-style completion handler (functors)
****************************************************************************/
#ifndef COMPLETION_HANDLER_HEADER
#define COMPLETION_HANDLER_HEADER
#include "../common/utils.h"
#include "iobase.h"
__STXXL_BEGIN_NAMESPACE
class request;
class completion_handler_impl
{
public:
virtual void operator()(request * ) = 0;
virtual completion_handler_impl * clone() const = 0;
virtual ~completion_handler_impl() {}
};
//! \brief Completion handler class (Loki-style)
//! In some situations one needs to execute
//! some actions after completion of an I/O
//! request. In these cases one can use
//! an I/O completion handler - a function
//! object that can be passed as a parameter
//! to asynchronous I/O calls \c stxxl::file::aread
//! and \c stxxl::file::awrite .
//! For an example of use see \link mng/test.cpp mng/test.cpp \endlink
class completion_handler
{
public:
completion_handler():sp_impl_(0) {}
completion_handler(const completion_handler & obj):sp_impl_(obj.sp_impl_.get()->clone()){}
completion_handler& operator = (const completion_handler & obj)
{
completion_handler copy(obj);
completion_handler_impl* p = sp_impl_.release();
sp_impl_.reset(copy.sp_impl_.release());
copy.sp_impl_.reset(p);
return *this;
}
void operator()(request * req)
{
(*sp_impl_)(req);
}
template <typename handler_type>
completion_handler(const handler_type & handler__);
private:
std::auto_ptr<completion_handler_impl> sp_impl_;
};
template <typename handler_type>
class completion_handler1: public completion_handler_impl
{
private:
handler_type handler_;
public:
completion_handler1(const handler_type & handler__): handler_(handler__) {}
completion_handler1* clone() const
{
return new completion_handler1(*this);
}
void operator()(request * req)
{
handler_(req);
}
};
template <typename handler_type>
completion_handler::completion_handler(const handler_type & handler__):
sp_impl_(new completion_handler1<handler_type>(handler__))
{
}
__STXXL_END_NAMESPACE
#endif