Remove PolicyBucket() constructor
[platform/core/security/cynara.git] / src / common / types / PolicyBucket.cpp
1 /*
2  * Copyright (c) 2014 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        src/common/types/PolicyBucket.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Implementation of Cynara::PolicyBucket methods
21  */
22
23 #include <sstream>
24
25 #include <types/PolicyCollection.h>
26 #include <types/PolicyKeyHelpers.h>
27
28 #include "PolicyBucket.h"
29
30 namespace Cynara {
31
32 PolicyBucket PolicyBucket::filtered(const PolicyKey &key) const {
33     PolicyBucket result(m_id + "_filtered");
34
35     const auto &policies = m_policyCollection;
36     const auto variants = PolicyKeyHelpers::keyVariants(key);
37
38     for (const auto &variant : variants) {
39         const auto policyIter = policies.find(variant);
40         if (policyIter != policies.end()) {
41             result.m_policyCollection[policyIter->first] = policyIter->second;
42         }
43     }
44
45     // Inherit original policy
46     result.setDefaultPolicy(defaultPolicy());
47     return result;
48 }
49
50 void PolicyBucket::insertPolicy(PolicyPtr policy) {
51     const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key());
52     m_policyCollection[gluedKey] = policy;
53 }
54
55 void PolicyBucket::deletePolicy(const PolicyKey &key) {
56     const auto gluedKey = PolicyKeyHelpers::glueKey(key);
57     m_policyCollection.erase(gluedKey);
58 }
59
60 void PolicyBucket::deletePolicy(std::function<bool(PolicyPtr)> predicate) {
61     auto &policies = m_policyCollection;
62
63     for (auto iter = policies.begin(); iter != policies.end(); ) {
64         if (predicate(iter->second)) {
65             policies.erase(iter++);
66         } else {
67             ++iter;
68         }
69     }
70 }
71
72 PolicyMap PolicyBucket::makePolicyMap(const PolicyCollection &policies) {
73     PolicyMap result;
74     for (const auto &policy : policies) {
75         const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key());
76         result[gluedKey] = policy;
77     }
78     return result;
79 }
80
81 }  // namespace Cynara