utils/call_tuple.hh

00001 // This file is a part of Catsfoot.
00002 // Copyright (C) 2011  Uni Research
00003 //
00004 // This program is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef CALL_TUPLE_HH
00018 # define CALL_TUPLE_HH
00019 
00020 # include <tuple>
00021 # include <type_traits>
00022 # include "../type_traits/is_callable.hh"
00023 
00024 namespace catsfoot {
00025   namespace details {
00026     template <typename Parameter, typename... Functions>
00027     struct call_tuple_helper {
00028       typedef
00029       std::tuple<typename std::decay
00030                  <typename is_callable
00031                   <const Functions(const Parameter&)>::result_type>
00032                  ::type...> return_type;
00033 
00034       static return_type
00035       call(const std::tuple<Functions...>&,
00036            const Parameter&,
00037            typename is_callable
00038             <const Functions(const Parameter&)>::result_type... args) {
00039         return std::make_tuple(std::move(args)...);
00040       }
00041 
00042       template <typename... Args>
00043       static return_type
00044       call(const std::tuple<Functions...>& functions,
00045            const Parameter& param,
00046            Args... args) {
00047         return call
00048           (functions, param,
00049            std::move(args)...,
00050            std::move(std::get<sizeof...(Args)>(functions)(param)));
00051       }
00052     };
00053   }
00054 
00060   template <typename Parameter, typename... Functions>
00061   std::tuple<typename std::decay
00062              <typename is_callable
00063               <const Functions(const Parameter&)>::result_type>
00064              ::type...>
00065   call_tuple(const std::tuple<Functions...>& functions,
00066              const Parameter& param) {
00067     return details::call_tuple_helper<Parameter, Functions...>
00068       ::call(functions, param);
00069   }
00070 
00071 }
00072 
00073 #endif