net: partial implementation of tcp
[quarnos.git] / libs / delegate.h
blob8faac6a6a560dd7fb540e635a8dde3c11000ebb7
1 /* Quarn OS
3 * Delegates
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _DELEGATE_H_
24 #define _DELEGATE_H_
26 template<typename ret_type, typename arg_type1 = int, typename arg_type2 = int,
27 typename arg_type3 = int, typename arg_type4 = int,
28 typename arg_type5 = int, typename arg_type6 = int,
29 typename arg_type7 = int>
30 class delegate {
31 private:
33 class A {};
35 /* Pointer to object (for methods only) */
36 A *owner;
38 /* Union of different types of pointer to method/function */
39 union {
40 /* Direct call method */
41 ret_type (A::*meth_a0)();
42 ret_type (A::*meth_a1)(arg_type1);
43 ret_type (A::*meth_a2)(arg_type1, arg_type2);
44 ret_type (A::*meth_a3)(arg_type1, arg_type2, arg_type3);
45 ret_type (A::*meth_a4)(arg_type1, arg_type2, arg_type3, arg_type4);
46 ret_type (A::*meth_a5)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5);
47 ret_type (A::*meth_a6)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6);
48 ret_type (A::*meth_a7)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7);
50 /* Direct call function */
51 ret_type (*func_a0)();
52 ret_type (*func_a1)(arg_type1);
53 ret_type (*func_a2)(arg_type1, arg_type2);
54 ret_type (*func_a3)(arg_type1, arg_type2, arg_type3);
55 ret_type (*func_a4)(arg_type1, arg_type2, arg_type3, arg_type4);
56 ret_type (*func_a5)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5);
57 ret_type (*func_a6)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6);
58 ret_type (*func_a7)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7);
59 } ptr;
61 /* Current state of the delegate */
62 enum del_type { del_uninit, del_method, del_function } type;
64 typedef delegate<ret_type, arg_type1, arg_type2, arg_type3,arg_type4, arg_type5, arg_type6, arg_type7> this_delegate;
66 public:
67 delegate();
68 ~delegate();
70 delegate(const delegate<ret_type, arg_type1, arg_type2, arg_type3,arg_type4, arg_type5, arg_type6, arg_type7>&);
71 delegate<ret_type, arg_type1, arg_type2, arg_type3,arg_type4, arg_type5, arg_type6, arg_type7> &operator=(const delegate<ret_type, arg_type1, arg_type2, arg_type3,arg_type4, arg_type5, arg_type6, arg_type7> &);
74 /* Save method pointer for different number of arguments */
75 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3,arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)());
76 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1));
77 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2));
78 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2, arg_type3));
79 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4));
80 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5));
81 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6));
82 template<class T> static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> method(T *obj,ret_type (T::*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7));
84 /* Save function pointer for different number of arguments */
85 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)());
86 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1));
87 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2));
88 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2, arg_type3));
89 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4));
90 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5));
91 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6));
92 static delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> function(ret_type (*_ptr)(arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7));
94 /* Calling function/method with different number of arguments */
95 ret_type call(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4, arg_type5 arg5, arg_type6 arg6, arg_type7 arg7) const;
97 /* Overloaded operators () for different numbers of arguments */
98 ret_type operator()() const;
99 ret_type operator()(arg_type1 arg1) const;
100 ret_type operator()(arg_type1 arg1, arg_type2 arg2) const;
101 ret_type operator()(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3) const;
102 ret_type operator()(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4) const;
103 ret_type operator()(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4, arg_type5 arg5) const;
104 ret_type operator()(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4, arg_type5 arg5, arg_type6 arg6) const;
105 ret_type operator()(arg_type1 arg1, arg_type2 arg2, arg_type3 arg3, arg_type4 arg4, arg_type5 arg5, arg_type6 arg6, arg_type7 arg7) const;
107 bool operator==(const delegate<ret_type, arg_type1, arg_type2, arg_type3, arg_type4, arg_type5, arg_type6, arg_type7> &) const;
109 bool null() const;
112 #include "delegate.cpp"
114 #endif