CKMI: Implementation of ProcessSettings module.
[platform/core/test/security-tests.git] / src / ckm-integration / process-settings / executor.h
1 /*
2  *  Copyright (c) 2015 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       executor.h
18  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #pragma once
22
23 #include <stdlib.h>
24
25 #include <iostream>
26
27 #include <dpl/test/test_exception.h>
28
29 #include <process-settings/policy.h>
30
31 namespace ProcessSettings {
32
33 template <typename PolicyArg, typename... Args>
34 class Executor : public PolicyArg, public Args... {
35 public:
36     template <typename... T>
37     Executor(T&&... t)
38       : PolicyArg(std::forward<T>(t)...)
39       , Args(static_cast<Policy&>(*this))...
40       , m_applied(false)
41     {}
42
43     void Apply() {
44         if (!m_applied)
45             InternalApply<Args...>();
46         m_applied = true;
47     }
48
49     void Revoke() {
50         if (m_applied)
51             InternalRevoke<Args...>();
52         m_applied = false;
53     }
54
55     virtual ~Executor() {
56         try {
57             Revoke();
58         } catch (const DPL::Test::TestException &e) {
59             // This is bad. The rest of test will not work properly!
60             std::cerr << "Error during cleaning up environment. "
61                 "The rest of test will probably fail." << e.GetMessage() << std::endl;
62         }
63     }
64
65 private:
66
67     template <typename First>
68     void InternalApply() {
69         First::Apply();
70     }
71
72     template <typename First, typename Second, typename... Rest>
73     void InternalApply() {
74         First::Apply();
75         InternalApply<Second, Rest...>();
76     }
77
78     template <typename First>
79     void InternalRevoke() {
80         First::Revoke();
81     }
82
83     template <typename First, typename Second, typename... Rest>
84     void InternalRevoke() {
85         InternalRevoke<Second, Rest...>();
86         First::Revoke();
87     }
88
89     bool m_applied;
90 };
91
92 } // namespace ProcessSetings
93