libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/base/functor.hpp
Go to the documentation of this file.
00001 
00011 /*
00012  *  Copyright (C) 2011 Juan Pedro BolĂ­var Puente
00013  *
00014  *  This file is part of Psychosynth.
00015  *   
00016  *  Psychosynth is free software: you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License as published by
00018  *  the Free Software Foundation, either version 3 of the License, or
00019  *  (at your option) any later version.
00020  *
00021  *  Psychosynth is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  You should have received a copy of the GNU General Public License
00027  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028  *
00029  */
00030 
00031 #ifndef PSYNTH_GRAPH_FUNCTOR_HPP_
00032 #define PSYNTH_GRAPH_FUNCTOR_HPP_
00033 
00034 namespace psynth
00035 {
00036 namespace base
00037 {
00038 
00043 template <typename Ret, typename... Args>
00044 struct dynamic_functor
00045 {
00046     virtual ~dynamic_functor () {};
00047     virtual Ret operator () (Args... args) = 0;
00048 };
00049 
00050 template <typename Fn, typename Ret, typename... Args>
00051 struct fn_dynamic_functor : dynamic_functor <Ret, Args...>
00052 {
00053     fn_dynamic_functor (const Fn& f)
00054         : _fn (f) {}
00055 
00056     fn_dynamic_functor (Fn&& f)
00057         : _fn (std::move (f)) {}
00058 
00059     fn_dynamic_functor (const fn_dynamic_functor&) = default;
00060     fn_dynamic_functor (fn_dynamic_functor&&) = default;
00061     
00062     fn_dynamic_functor& operator= (const fn_dynamic_functor&) = default;
00063 
00064     Ret operator () (Args... args)
00065     {
00066         return _fn (std::forward<Args> (args)...);
00067     }
00068     
00069 private:
00070     Fn _fn;
00071 };
00072 
00073 } /* namespace base */
00074 } /* namespace psynth */
00075 
00076 #endif /* PSYNTH_GRAPH_FUNCTOR_HPP_ */