STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
completion_handler.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/io/completion_handler.h
3  *
4  * Loki-style completion handler (functors)
5  *
6  * Part of the STXXL. See http://stxxl.sourceforge.net
7  *
8  * Copyright (C) 2003 Roman Dementiev <[email protected]>
9  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_IO_COMPLETION_HANDLER_HEADER
17 #define STXXL_IO_COMPLETION_HANDLER_HEADER
18 
19 #include <stxxl/bits/namespace.h>
21 #include <cstdlib>
22 
24 
25 class request;
26 
28 {
29 public:
30  virtual void operator () (request*) = 0;
31  virtual completion_handler_impl * clone() const = 0;
33 };
34 
35 template <typename HandlerType>
37 {
38 private:
39  HandlerType m_handler;
40 
41 public:
42  completion_handler1(const HandlerType& handler)
43  : m_handler(handler)
44  { }
46  {
47  return new completion_handler1(*this);
48  }
49  void operator () (request* req)
50  {
51  m_handler(req);
52  }
53 };
54 
55 //! Completion handler class (Loki-style).
56 //!
57 //! In some situations one needs to execute some actions after completion of an
58 //! I/O request. In these cases one can use an I/O completion handler - a
59 //! function object that can be passed as a parameter to asynchronous I/O calls
60 //! \c stxxl::file::aread and \c stxxl::file::awrite .
62 {
64 
65 public:
66  //! Construct default, no operation completion handler.
68  : m_ptr(static_cast<completion_handler_impl*>(NULL))
69  { }
70 
71  //! Copy constructor.
73  : m_ptr(obj.m_ptr.get() ? obj.m_ptr.get()->clone() : NULL)
74  { }
75 
76  //! Construct a completion handler which calls some function.
77  template <typename HandlerType>
78  completion_handler(const HandlerType& handler)
79  : m_ptr(new completion_handler1<HandlerType>(handler))
80  { }
81 
82  //! Assignment operator
83  completion_handler& operator = (const completion_handler& obj)
84  {
85  m_ptr.reset(obj.m_ptr.get() ? obj.m_ptr.get()->clone() : NULL);
86  return *this;
87  }
88 
89  //! Call the enclosed completion handler.
90  void operator () (request* req)
91  {
92  if (m_ptr.get())
93  (* m_ptr)(req);
94  }
95 };
96 
98 
99 #endif // !STXXL_IO_COMPLETION_HANDLER_HEADER
100 // vim: et:ts=4:sw=4
completion_handler(const HandlerType &handler)
Construct a completion handler which calls some function.
Completion handler class (Loki-style).
completion_handler1 * clone() const
completion_handler(const completion_handler &obj)
Copy constructor.
completion_handler1(const HandlerType &handler)
compat_unique_ptr< completion_handler_impl >::result m_ptr
std::auto_ptr< Type > result
Definition: unique_ptr.h:29
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
completion_handler()
Construct default, no operation completion handler.
Request object encapsulating basic properties like file and offset.
Definition: request.h:38
#define STXXL_END_NAMESPACE
Definition: namespace.h:17