ALPSCore reference
util.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
10 #pragma once
11 
12 #include <alps/alea/core.hpp>
13 
14 namespace alps { namespace alea { namespace internal {
15 
16 template <typename Acc>
17 inline void check_valid(const Acc &acc)
18 {
19  if (!acc.valid())
21 }
22 
23 
24 template <typename T, typename... Args>
25 T call_vargs(std::function<T(Args...)> func, const T *args);
26 
27 template <typename T, typename... Args>
28 T call_vargs(std::function<T(T, Args...)> func, const T *args)
29 {
30  // use currying to transform multi-argument function to hierarchy
31  const T head = *args;
32  std::function<T(Args...)> closure =
33  [=](Args... tail) -> T { return func(head, tail...); };
34  return call_vargs(closure, ++args);
35 }
36 
37 template <typename T>
38 T call_vargs(std::function<T()> func, const T *)
39 {
40  // unravel the currying hierarchy
41  return func();
42 }
43 
44 template <typename Acc>
46 {
48  acc.finalize_to(result);
49  return result;
50 }
51 
52 template <typename Acc>
53 typename traits<Acc>::result_type result(const Acc &acc)
54 {
56  Acc copy = acc;
57  copy.finalize_to(result);
58  return result;
59 }
60 
61 }}} /* namespace alps::alea::internal */
void check_valid(const Acc &acc)
Definition: util.hpp:17
T call_vargs(std::function< T(Args...)> func, const T *args)
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45