Stxxl  1.2.1
completion_handler.h
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 <dementiev@mpi-sb.mpg.de>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_COMPLETION_HANDLER_HEADER
16 #define STXXL_COMPLETION_HANDLER_HEADER
17 
18 #include <memory>
19 
20 #include <stxxl/bits/namespace.h>
21 
22 
23 __STXXL_BEGIN_NAMESPACE
24 
25 class request;
26 
27 class completion_handler_impl
28 {
29 public:
30  virtual void operator () (request *) = 0;
31  virtual completion_handler_impl * clone() const = 0;
32  virtual ~completion_handler_impl() { }
33 };
34 
36 
46 {
47 public:
48  completion_handler() : sp_impl_(0) { }
49  completion_handler(const completion_handler & obj) : sp_impl_(obj.sp_impl_.get()->clone()) { }
50  completion_handler & operator = (const completion_handler & obj)
51  {
52  completion_handler copy(obj);
53  completion_handler_impl * p = sp_impl_.release();
54  sp_impl_.reset(copy.sp_impl_.release());
55  copy.sp_impl_.reset(p);
56  return *this;
57  }
58  void operator () (request * req)
59  {
60  (*sp_impl_)(req);
61  }
62  template <typename handler_type>
63  completion_handler(const handler_type & handler__);
64 
65 private:
66  std::auto_ptr<completion_handler_impl> sp_impl_;
67 };
68 
69 template <typename handler_type>
70 class completion_handler1 : public completion_handler_impl
71 {
72 private:
73  handler_type handler_;
74 
75 public:
76  completion_handler1(const handler_type & handler__) : handler_(handler__) { }
77  completion_handler1 * clone() const
78  {
79  return new completion_handler1(*this);
80  }
81  void operator () (request * req)
82  {
83  handler_(req);
84  }
85 };
86 
87 template <typename handler_type>
88 completion_handler::completion_handler(const handler_type & handler__) :
89  sp_impl_(new completion_handler1<handler_type>(handler__))
90 { }
91 
92 __STXXL_END_NAMESPACE
93 
94 #endif // !STXXL_COMPLETION_HANDLER_HEADER