lxcpp: fix cgroup unit tests
[platform/core/security/vasum.git] / server / ipc-callback-wrapper.hpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Mateusz Malicki (m.malicki2@samsung.com)
22  * @brief   IPCSignalWrapper and IPCMethodWrapper classes used to hide IPC specifics
23  */
24
25
26 #ifndef SERVER_IPC_CALLBACK_WRAPPER_HPP
27 #define SERVER_IPC_CALLBACK_WRAPPER_HPP
28
29 #include "api/messages.hpp"
30 #include "api/method-result-builder.hpp"
31 #include "api/ipc-method-result-builder.hpp"
32
33 #include <functional>
34
35 namespace vasum {
36
37 template<typename ArgIn>
38 class IPCSignalWrapper {
39 public:
40     typedef typename std::remove_cv<ArgIn>::type in;
41     typedef std::function<void(const in&)> type;
42
43     static typename ipc::SignalHandler<in>::type
44     getWrapper(const type& callback) {
45         return [callback](const ipc::PeerID, const std::shared_ptr<in>& argIn)
46         {
47             callback(*argIn);
48         };
49     }
50 };
51
52 template<>
53 class IPCSignalWrapper<const api::Void> {
54 public:
55     typedef api::Void in;
56     typedef std::function<void()> type;
57
58     static typename ipc::SignalHandler<in>::type
59     getWrapper(const type& callback) {
60         return [callback](const ipc::PeerID, const std::shared_ptr<in>& /* argIn */)
61         {
62             callback();
63         };
64     }
65 };
66
67 template<typename ArgIn = const api::Void, typename ArgOut = api::Void>
68 class IPCMethodWrapper {
69 public:
70     typedef typename std::remove_cv<ArgIn>::type in;
71     typedef ArgOut out;
72     typedef std::function<void(const in&, api::MethodResultBuilder::Pointer)> type;
73
74     static typename ipc::MethodHandler<out, in>::type
75     getWrapper(const type& callback) {
76         return [callback](const ipc::PeerID,
77                           const std::shared_ptr<in>& argIn,
78                           ipc::MethodResult::Pointer&& argOut)
79         {
80             auto rb = std::make_shared<api::IPCMethodResultBuilder>(argOut);
81             callback(*argIn, rb);
82         };
83     }
84 };
85
86 template<typename ArgOut>
87 class IPCMethodWrapper<ArgOut, typename std::enable_if<!std::is_const<ArgOut>::value, api::Void>::type> {
88 public:
89     typedef api::Void in;
90     typedef ArgOut out;
91     typedef std::function<void(api::MethodResultBuilder::Pointer)> type;
92
93     static typename ipc::MethodHandler<out, in>::type
94     getWrapper(const type& callback) {
95         return [callback](const ipc::PeerID,
96                           const std::shared_ptr<in>& /* argIn */,
97                           ipc::MethodResult::Pointer&& argOut)
98         {
99             auto rb = std::make_shared<api::IPCMethodResultBuilder>(argOut);
100             callback(rb);
101         };
102     }
103 };
104
105 } // namespace vasum
106
107 #endif // SERVER_IPC_CALLBACK_WRAPPER_HPP