Merge branch 'ckm' into tizen
[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             m_applied = true;
46             InternalApply<Args...>();
47         }
48     }
49
50     void Revoke() {
51         if (m_applied) {
52             m_applied = false;
53             InternalRevoke<Args...>();
54         }
55     }
56
57     virtual ~Executor() {
58         try {
59             Revoke();
60         } catch (const DPL::Test::TestException &e) {
61             // This is bad. The rest of test will not work properly!
62             std::cerr << "Error during cleaning up environment. "
63                 "The rest of test will probably fail." << e.GetMessage() << std::endl;
64         }
65     }
66
67 private:
68
69     template <typename First>
70     void InternalApply() {
71         First::Apply();
72     }
73
74     template <typename First, typename Second, typename... Rest>
75     void InternalApply() {
76         First::Apply();
77         InternalApply<Second, Rest...>();
78     }
79
80     template <typename First>
81     void InternalRevoke() {
82         First::Revoke();
83     }
84
85     template <typename First, typename Second, typename... Rest>
86     void InternalRevoke() {
87         InternalRevoke<Second, Rest...>();
88         First::Revoke();
89     }
90
91     bool m_applied;
92 };
93
94 } // namespace ProcessSetings
95