Stxxl
1.2.1
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/io/completion_handler.h 00003 * 00004 * Loki-style completion handler (functors) 00005 * 00006 * Part of the STXXL. See http://stxxl.sourceforge.net 00007 * 00008 * Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00009 * 00010 * Distributed under the Boost Software License, Version 1.0. 00011 * (See accompanying file LICENSE_1_0.txt or copy at 00012 * http://www.boost.org/LICENSE_1_0.txt) 00013 **************************************************************************/ 00014 00015 #ifndef STXXL_COMPLETION_HANDLER_HEADER 00016 #define STXXL_COMPLETION_HANDLER_HEADER 00017 00018 #include <memory> 00019 00020 #include <stxxl/bits/namespace.h> 00021 00022 00023 __STXXL_BEGIN_NAMESPACE 00024 00025 class request; 00026 00027 class completion_handler_impl 00028 { 00029 public: 00030 virtual void operator () (request *) = 0; 00031 virtual completion_handler_impl * clone() const = 0; 00032 virtual ~completion_handler_impl() { } 00033 }; 00034 00036 00045 class completion_handler 00046 { 00047 public: 00048 completion_handler() : sp_impl_(0) { } 00049 completion_handler(const completion_handler & obj) : sp_impl_(obj.sp_impl_.get()->clone()) { } 00050 completion_handler & operator = (const completion_handler & obj) 00051 { 00052 completion_handler copy(obj); 00053 completion_handler_impl * p = sp_impl_.release(); 00054 sp_impl_.reset(copy.sp_impl_.release()); 00055 copy.sp_impl_.reset(p); 00056 return *this; 00057 } 00058 void operator () (request * req) 00059 { 00060 (*sp_impl_)(req); 00061 } 00062 template <typename handler_type> 00063 completion_handler(const handler_type & handler__); 00064 00065 private: 00066 std::auto_ptr<completion_handler_impl> sp_impl_; 00067 }; 00068 00069 template <typename handler_type> 00070 class completion_handler1 : public completion_handler_impl 00071 { 00072 private: 00073 handler_type handler_; 00074 00075 public: 00076 completion_handler1(const handler_type & handler__) : handler_(handler__) { } 00077 completion_handler1 * clone() const 00078 { 00079 return new completion_handler1(*this); 00080 } 00081 void operator () (request * req) 00082 { 00083 handler_(req); 00084 } 00085 }; 00086 00087 template <typename handler_type> 00088 completion_handler::completion_handler(const handler_type & handler__) : 00089 sp_impl_(new completion_handler1<handler_type>(handler__)) 00090 { } 00091 00092 __STXXL_END_NAMESPACE 00093 00094 #endif // !STXXL_COMPLETION_HANDLER_HEADER