Stxxl  1.4.0
include/stxxl/bits/io/completion_handler.h
Go to the documentation of this file.
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  *  Copyright (C) 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00010  *
00011  *  Distributed under the Boost Software License, Version 1.0.
00012  *  (See accompanying file LICENSE_1_0.txt or copy at
00013  *  http://www.boost.org/LICENSE_1_0.txt)
00014  **************************************************************************/
00015 
00016 #ifndef STXXL_COMPLETION_HANDLER_HEADER
00017 #define STXXL_COMPLETION_HANDLER_HEADER
00018 
00019 #include <stxxl/bits/namespace.h>
00020 #include <stxxl/bits/compat_unique_ptr.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 
00035 template <typename handler_type>
00036 class completion_handler1 : public completion_handler_impl
00037 {
00038 private:
00039     handler_type handler_;
00040 
00041 public:
00042     completion_handler1(const handler_type & handler__) : handler_(handler__) { }
00043     completion_handler1 * clone() const
00044     {
00045         return new completion_handler1(*this);
00046     }
00047     void operator () (request * req)
00048     {
00049         handler_(req);
00050     }
00051 };
00052 
00053 //! \brief Completion handler class (Loki-style)
00054 
00055 //! In some situations one needs to execute
00056 //! some actions after completion of an I/O
00057 //! request. In these cases one can use
00058 //! an I/O completion handler - a function
00059 //! object that can be passed as a parameter
00060 //! to asynchronous I/O calls \c stxxl::file::aread
00061 //! and \c stxxl::file::awrite .
00062 //! For an example of use see \link mng/test_mng.cpp mng/test_mng.cpp \endlink
00063 class completion_handler
00064 {
00065     compat_unique_ptr<completion_handler_impl>::result sp_impl_;
00066 
00067 public:
00068     completion_handler() :
00069         sp_impl_(static_cast<completion_handler_impl *>(0))
00070     { }
00071 
00072     completion_handler(const completion_handler & obj) :
00073         sp_impl_(obj.sp_impl_.get()->clone())
00074     { }
00075 
00076     template <typename handler_type>
00077     completion_handler(const handler_type & handler__) :
00078         sp_impl_(new completion_handler1<handler_type>(handler__))
00079     { }
00080 
00081     completion_handler & operator = (const completion_handler & obj)
00082     {
00083         sp_impl_.reset(obj.sp_impl_.get()->clone());
00084         return *this;
00085     }
00086     void operator () (request * req)
00087     {
00088         (*sp_impl_)(req);
00089     }
00090 };
00091 
00092 //! \brief Default completion handler class
00093 
00094 struct default_completion_handler
00095 {
00096     //! \brief An operator that does nothing
00097     void operator () (request *) { }
00098 };
00099 
00100 __STXXL_END_NAMESPACE
00101 
00102 #endif // !STXXL_COMPLETION_HANDLER_HEADER
00103 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines