tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / include / dpl / bind.h
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        bind.h
18  * @author      Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file of bind
21  */
22
23 /*
24  * Remarks:
25  * - Current implementation supports binding member functions (methods) only.
26  * - Currently up to 8 std::placeholders are supported.
27  * - Bound delegates are of type std::function<...>. This implies that
28  *   passing arguments at bind-time is not supported (arguments MAY ONLY be
29  *   passed at call-time).
30  *
31  * Usage:
32  * - For usage see tests/core/test_bind.cpp. In general, usage comes down to:
33  *   std::function<ResultType(Args...)> delegate =
34  *      DPL::Bind(&ObjectType::MethodName, &object);
35  */
36
37 #ifndef DPL_BIND_H_
38 #define DPL_BIND_H_
39
40 #include <functional>
41
42 #include <boost/preprocessor/cat.hpp>
43 #include <boost/preprocessor/arithmetic/add.hpp>
44 #include <boost/preprocessor/repetition/repeat.hpp>
45
46 namespace DPL {
47 namespace detail {
48 template<size_t>
49 struct PlaceholdersBindHelper;
50
51 #define DPL_PLACEHOLDERS_LIST_(z, n, t) ,BOOST_PP_CAT(std::placeholders::_,    \
52                                                       BOOST_PP_ADD(n, 1))
53 #define DPL_PLACEHOLDERS_(count) BOOST_PP_REPEAT(count,                        \
54                                                  DPL_PLACEHOLDERS_LIST_,       \
55                                                  count)
56 #define DPL_PLACEHOLDERS_BIND_HELPER_(count)                                   \
57 template<>                                                                     \
58 struct PlaceholdersBindHelper<count>                                           \
59 {                                                                              \
60     template<typename Result, typename Type, typename ...Args>                 \
61     static std::function<Result(Args...)>                                      \
62     bind(Result(Type::*method)(Args...), Type* object) {                       \
63         return std::bind(method, object DPL_PLACEHOLDERS_(count));             \
64     }                                                                          \
65 }
66
67 DPL_PLACEHOLDERS_BIND_HELPER_(0);
68 DPL_PLACEHOLDERS_BIND_HELPER_(1);
69 DPL_PLACEHOLDERS_BIND_HELPER_(2);
70 DPL_PLACEHOLDERS_BIND_HELPER_(3);
71 DPL_PLACEHOLDERS_BIND_HELPER_(4);
72 DPL_PLACEHOLDERS_BIND_HELPER_(5);
73 DPL_PLACEHOLDERS_BIND_HELPER_(6);
74 DPL_PLACEHOLDERS_BIND_HELPER_(7);
75 DPL_PLACEHOLDERS_BIND_HELPER_(8);
76 }
77
78 template<typename Result, typename Type, typename ...Args>
79 std::function<Result(Args...)> Bind(Result(Type::*method)(Args...),
80                                     Type* object)
81 {
82     return detail::PlaceholdersBindHelper<sizeof...(Args)>::bind(method,
83                                                                  object);
84 }
85 }
86
87 #endif // DPL_BIND_H_