2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file src/common/types/PolicyBucket.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Implementation of Cynara::PolicyBucket methods
25 #include <types/PolicyCollection.h>
26 #include <types/PolicyKeyHelpers.h>
28 #include "PolicyBucket.h"
32 PolicyBucket PolicyBucket::filtered(const PolicyKey &key) const {
33 PolicyBucket result(m_id + "_filtered");
35 const auto &policies = m_policyCollection;
36 const auto variants = PolicyKeyHelpers::keyVariants(key);
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;
45 // Inherit original policy
46 result.setDefaultPolicy(defaultPolicy());
50 void PolicyBucket::insertPolicy(PolicyPtr policy) {
51 const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key());
52 m_policyCollection[gluedKey] = policy;
55 void PolicyBucket::deletePolicy(const PolicyKey &key) {
56 const auto gluedKey = PolicyKeyHelpers::glueKey(key);
57 m_policyCollection.erase(gluedKey);
60 void PolicyBucket::deletePolicy(std::function<bool(PolicyPtr)> predicate) {
61 auto &policies = m_policyCollection;
63 for (auto iter = policies.begin(); iter != policies.end(); ) {
64 if (predicate(iter->second)) {
65 policies.erase(iter++);
72 PolicyMap PolicyBucket::makePolicyMap(const PolicyCollection &policies) {
74 for (const auto &policy : policies) {
75 const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key());
76 result[gluedKey] = policy;