aboutsummaryrefslogtreecommitdiff
path: root/odb/details/function-wrapper.txx
blob: 0ece2445753762eb972db0308776cdaee7581fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// file      : odb/details/function-wrapper.txx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <utility> // std::swap, std::move

namespace odb
{
  namespace details
  {
#ifdef ODB_CXX11
    template <typename F>
    struct caller_impl;

#ifdef ODB_CXX11_VARIADIC_TEMPLATE
    template <typename R, typename... A>
    struct caller_impl<R (A...)>
    {
      static R
      function (const void* f, A... a)
      {
        return (*static_cast<const std::function<R (A...)>*> (f)) (a...);
      }
    };
#else
    template <typename R, typename A1>
    struct caller_impl<R (A1)>
    {
      static R
      function (const void* f, A1 a1)
      {
        return (*static_cast<const std::function<R (A1)>*> (f)) (a1);
      }
    };

    template <typename R, typename A1, typename A2>
    struct caller_impl<R (A1, A2)>
    {
      static R
      function (const void* f, A1 a1, A2 a2)
      {
        return (*static_cast<const std::function<R (A1, A2)>*> (f)) (a1, a2);
      }
    };
#endif

    template <typename F>
    void
    deleter_impl (const void* f)
    {
      delete static_cast<const std::function<F>*> (f);
    }

    template <typename F>
    template <typename F1>
    function_wrapper<F>::
    function_wrapper (
      F1 f,
      typename std::enable_if<!std::is_convertible<F1, F*>::value>::type*)
    {
      std_function_type sf (std::move (f));

      if (F* const* const f = sf.template target<F*> ())
      {
        function = *f;
        deleter = 0;
        std_function = 0;
      }
      else
      {
        function = reinterpret_cast<F*> (&caller_impl<F>::function);
        deleter = &deleter_impl<F>;
        std_function = new std_function_type (std::move (sf));
      }
    }
#endif

    template <typename F>
    void function_wrapper<F>::
    swap (function_wrapper<F>& x)
    {
      std::swap (function, x.function);
      std::swap (deleter, x.deleter);
      std::swap (std_function, x.std_function);
    }
  }
}