summaryrefslogtreecommitdiff
path: root/ucoo/utils/function.hh
blob: d8bbefdb30e23f31ef5d5a418294686ade94936d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef ucoo_utils_function_hh
#define ucoo_utils_function_hh
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/common.hh"

namespace ucoo {

/// Type used to compute maximum size of a FunctionStore.
union FunctionStoreTypes
{
    /// A FunctionStore can store a function pointer...
    template<typename Res, typename ...Args>
    using Function = Res (*) (Args...);
    Function<void> function;
    /// ...or a bound member function (object pointer + member function
    /// pointer).
    template<typename T, typename Res, typename ...Args>
    struct BoundMember
    {
        T *object;
        Res (T::*member_pointer) (Args...);
    };
    class SomeClass;
    BoundMember<SomeClass, void> bound_member;
};

/// Store what is needed to call a function.
union FunctionStore
{
    void *access () { return data_; }
    const void *access () const { return data_; }
    template<typename T>
    T &access ()
    {
        static_assert (sizeof (T) <= sizeof (data_),
                       "function pointer too big");
        return *reinterpret_cast<T *> (access ());
    }
    template<typename T>
    const T &access () const
    {
        static_assert (sizeof (T) <= sizeof (data_),
                       "function pointer too big");
        return *reinterpret_cast<const T *> (access ());
    }
  private:
    FunctionStoreTypes unused_;
    char data_[sizeof (FunctionStoreTypes)];
};

template<typename Signature>
class Function;

/// Store a reference to a callable element in an object which type only
/// depends on the callable signature.
///
/// This is a lightweight and limited version of std::function.
template<typename Res, typename ...Args>
class Function<Res (Args...)>
{
  public:
    /// Construct an empty function, can not be called.
    Function () : call_ (nullptr) { }
    /// Construct from a function pointer.
    Function (Res (*function_pointer) (Args...))
    {
        assert (function_pointer);
        using F = FunctionStoreTypes::Function<Res, Args...>;
        F &f = store_.access<F> ();
        f = function_pointer;
        call_ = &Function::call_function<F>;
    }
    /// Construct with a bound member function.
    template<typename T>
    Function (T *object, Res (T::*member_pointer) (Args...))
    {
        assert (object && member_pointer);
        using F = FunctionStoreTypes::BoundMember<T, Res, Args...>;
        F &f = store_.access<F> ();
        f.object = object;
        f.member_pointer = member_pointer;
        call_ = &Function::call_bound_member<F>;
    }
    /// Reset to empty function.
    void reset () { call_ = nullptr; }
    /// Call the stored function.
    Res operator() (Args... args) const
    {
        assert (call_);
        return call_ (args..., store_);
    }
    /// Test whether there is a stored function.
    operator bool () const
    {
        return call_;
    }
  private:
    template<typename F>
    static Res call_function (Args... args, const FunctionStore &store)
    {
        const F &f = store.access<F> ();
        return f (args...);
    }
    template<typename F>
    static Res call_bound_member (Args... args, const FunctionStore &store)
    {
        const F &f = store.access<F> ();
        return (f.object->*f.member_pointer) (args...);
    }
  private:
    Res (*call_) (Args..., const FunctionStore &store);
    FunctionStore store_;
};

} // namespace ucoo

#endif // ucoo_utils_function_hh