1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_COMMON_PERMISSIONS_BASE_SET_OPERATORS_H_
#define EXTENSIONS_COMMON_PERMISSIONS_BASE_SET_OPERATORS_H_
#include <stddef.h>
#include <iterator>
#include <map>
#include <memory>
#include "base/check.h"
namespace extensions {
// Traits for template paramater of |BaseSetOperators<T>|. Specializations
// should define `ElementType` for the type of elements to store in the set,
// and `EmementIDType` for the type of element identifiers.
template <typename T>
struct BaseSetOperatorsTraits {};
// Set operations shared by `APIPermissionSet` and `ManifestPermissionSet`.
//
// TODO(rpaquay): It would be nice to remove the need for the sub-classes and
// instead directly use this class where needed.
template <typename T>
class BaseSetOperators {
public:
using ElementType = typename BaseSetOperatorsTraits<T>::ElementType;
using ElementIDType = typename BaseSetOperatorsTraits<T>::ElementIDType;
using Map = std::map<ElementIDType, std::unique_ptr<ElementType>>;
class const_iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = const ElementType*;
using difference_type = std::ptrdiff_t;
using pointer = const ElementType**;
using reference = const ElementType*&;
const_iterator(const typename Map::const_iterator& it) : it_(it) {}
const_iterator(const const_iterator& ids_it) : it_(ids_it.it_) {}
const_iterator& operator++() {
++it_;
return *this;
}
const_iterator operator++(int) {
const_iterator tmp(it_++);
return tmp;
}
bool operator==(const const_iterator& rhs) const {
return it_ == rhs.it_;
}
bool operator!=(const const_iterator& rhs) const {
return it_ != rhs.it_;
}
const ElementType* operator*() const {
return it_->second.get();
}
const ElementType* operator->() const {
return it_->second.get();
}
private:
typename Map::const_iterator it_;
};
BaseSetOperators() {
// Ensure `T` is convertible to us, so we can safely downcast when calling
// methods that must exist in `T`.
static_assert(std::is_convertible<T*, BaseSetOperators<T>*>::value,
"U ptr must implicitly convert to T ptr");
}
BaseSetOperators(BaseSetOperators<T>&& other) = default;
BaseSetOperators<T>& operator=(BaseSetOperators<T>&& rhs) = default;
~BaseSetOperators() {}
bool operator==(const BaseSetOperators<T>& rhs) const { return Equal(rhs); }
bool operator!=(const BaseSetOperators<T>& rhs) const { return !Equal(rhs); }
T Clone() const {
T result;
for (const auto* item : *this)
result.insert(item->Clone());
return result;
}
bool Equal(const BaseSetOperators<T>& rhs) const {
const_iterator it = begin();
const_iterator rhs_it = rhs.begin();
const_iterator it_end = end();
const_iterator rhs_it_end = rhs.end();
while (it != it_end && rhs_it != rhs_it_end) {
if (it->id() > rhs_it->id())
return false;
else if (it->id() < rhs_it->id())
return false;
else if (!it->Equal(*rhs_it))
return false;
++it;
++rhs_it;
}
return it == it_end && rhs_it == rhs_it_end;
}
bool Contains(const BaseSetOperators<T>& rhs) const {
const_iterator it1 = begin();
const_iterator it2 = rhs.begin();
const_iterator end1 = end();
const_iterator end2 = rhs.end();
while (it1 != end1 && it2 != end2) {
if (it1->id() > it2->id()) {
return false;
} else if (it1->id() < it2->id()) {
++it1;
} else {
if (!it1->Contains(*it2))
return false;
++it1;
++it2;
}
}
return it2 == end2;
}
static void Difference(const BaseSetOperators<T>& set1,
const BaseSetOperators<T>& set2,
T* set3) {
CHECK(set3);
set3->clear();
const_iterator it1 = set1.begin();
const_iterator it2 = set2.begin();
const const_iterator end1 = set1.end();
const const_iterator end2 = set2.end();
while (it1 != end1 && it2 != end2) {
if (it1->id() < it2->id()) {
set3->insert(it1->Clone());
++it1;
} else if (it1->id() > it2->id()) {
++it2;
} else {
std::unique_ptr<ElementType> p = it1->Diff(*it2);
if (p)
set3->insert(std::move(p));
++it1;
++it2;
}
}
while (it1 != end1) {
set3->insert(it1->Clone());
++it1;
}
}
static void Intersection(const BaseSetOperators<T>& set1,
const BaseSetOperators<T>& set2,
T* set3) {
DCHECK(set3);
set3->clear();
const_iterator it1 = set1.begin();
const_iterator it2 = set2.begin();
const const_iterator end1 = set1.end();
const const_iterator end2 = set2.end();
while (it1 != end1 && it2 != end2) {
if (it1->id() < it2->id()) {
++it1;
} else if (it1->id() > it2->id()) {
++it2;
} else {
std::unique_ptr<ElementType> p = it1->Intersect(*it2);
if (p)
set3->insert(std::move(p));
++it1;
++it2;
}
}
}
static void Union(const BaseSetOperators<T>& set1,
const BaseSetOperators<T>& set2,
T* set3) {
DCHECK(set3);
set3->clear();
const_iterator it1 = set1.begin();
const_iterator it2 = set2.begin();
const const_iterator end1 = set1.end();
const const_iterator end2 = set2.end();
while (true) {
if (it1 == end1) {
while (it2 != end2) {
set3->insert(it2->Clone());
++it2;
}
break;
}
if (it2 == end2) {
while (it1 != end1) {
set3->insert(it1->Clone());
++it1;
}
break;
}
if (it1->id() < it2->id()) {
set3->insert(it1->Clone());
++it1;
} else if (it1->id() > it2->id()) {
set3->insert(it2->Clone());
++it2;
} else {
set3->insert(it1->Union(*it2));
++it1;
++it2;
}
}
}
const_iterator begin() const { return const_iterator(map_.begin()); }
const_iterator end() const { return map_.end(); }
const_iterator find(ElementIDType id) const { return map_.find(id); }
size_t count(ElementIDType id) const { return map_.count(id); }
bool empty() const { return map_.empty(); }
size_t erase(ElementIDType id) { return map_.erase(id); }
void insert(std::unique_ptr<ElementType> item) {
ElementIDType id = item->id();
map_[id] = std::move(item);
}
size_t size() const { return map_.size(); }
const Map& map() const {
return map_;
}
void clear() {
map_.clear();
}
private:
Map map_;
};
} // namespace extensions
#endif // EXTENSIONS_COMMON_PERMISSIONS_BASE_SET_OPERATORS_H_
|