Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/subscription/UserDefinedSubscription.cpp

Issue 29385742: Issue 4127 - [emscripten] Convert subscription classes to C++ - Part 2 (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Created March 16, 2017, 6:27 p.m.
Right Patch Set: Rebased Created April 13, 2017, 1:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/subscription/UserDefinedSubscription.h ('k') | test/subscriptionClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <cstdlib>
19
1 #include "UserDefinedSubscription.h" 20 #include "UserDefinedSubscription.h"
2 21
3 int UserDefinedSubscription::filterTypeToDefaults[] = { 22 namespace
4 UserDefinedSubscription::Defaults::BLOCKING, // UNKNOWN 23 {
5 UserDefinedSubscription::Defaults::BLOCKING, // INVALID 24 enum FilterCategory
6 UserDefinedSubscription::Defaults::BLOCKING, // COMMENT 25 {
7 UserDefinedSubscription::Defaults::BLOCKING, // BLOCKING 26 WHITELIST = 1,
8 UserDefinedSubscription::Defaults::WHITELIST, // WHITELIST 27 BLOCKING = 2,
9 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDE 28 ELEMHIDE = 4,
10 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDEEXCEPTION 29 };
11 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDEEMULATION 30
12 }; 31 const FilterCategory filterTypeToCategory[] = {
32 FilterCategory::BLOCKING, // UNKNOWN
33 FilterCategory::BLOCKING, // INVALID
34 FilterCategory::BLOCKING, // COMMENT
35 FilterCategory::BLOCKING, // BLOCKING
36 FilterCategory::WHITELIST, // WHITELIST
37 FilterCategory::ELEMHIDE, // ELEMHIDE
38 FilterCategory::ELEMHIDE, // ELEMHIDEEXCEPTION
39 FilterCategory::ELEMHIDE, // ELEMHIDEEMULATION
40 };
41
42 static_assert(
43 sizeof(filterTypeToCategory) / sizeof(filterTypeToCategory[0]) == Filter::Ty pe::VALUE_COUNT,
44 "Unexpected number of filter types, was a new type added?"
45 );
46 }
13 47
14 UserDefinedSubscription::UserDefinedSubscription(const String& id) 48 UserDefinedSubscription::UserDefinedSubscription(const String& id)
15 : Subscription(Type::USERDEFINED, id), mDefaults(0) 49 : Subscription(Type::USERDEFINED, id), mDefaults(0)
16 { 50 {
17 } 51 }
18 52
19 bool UserDefinedSubscription::IsDefaultFor(const Filter* filter) const 53 bool UserDefinedSubscription::IsDefaultFor(const Filter* filter) const
20 { 54 {
21 return mDefaults & filterTypeToDefaults[filter->mType]; 55 if (filter->mType >= Filter::Type::VALUE_COUNT)
56 {
57 assert(false, "Filter type exceeds valid range");
58 abort();
59 }
60 return mDefaults & filterTypeToCategory[filter->mType];
22 } 61 }
23 62
24 void UserDefinedSubscription::MakeDefaultFor(const Filter* filter) 63 void UserDefinedSubscription::MakeDefaultFor(const Filter* filter)
25 { 64 {
26 mDefaults |= filterTypeToDefaults[filter->mType]; 65 if (filter->mType >= Filter::Type::VALUE_COUNT)
66 {
67 assert(false, "Filter type exceeds valid range");
68 abort();
69 }
70 mDefaults |= filterTypeToCategory[filter->mType];
27 } 71 }
28 72
29 void UserDefinedSubscription::InsertFilterAt(Filter* filter, unsigned pos) 73 void UserDefinedSubscription::InsertFilterAt(Filter* filter, unsigned pos)
30 { 74 {
31 if (pos >= mFilters.size()) 75 if (pos >= mFilters.size())
32 mFilters.emplace_back(filter); 76 mFilters.emplace_back(filter);
33 else 77 else
34 mFilters.emplace(mFilters.begin() + pos, filter); 78 mFilters.emplace(mFilters.begin() + pos, filter);
35 } 79 }
36 80
37 bool UserDefinedSubscription::RemoveFilterAt(unsigned pos) 81 bool UserDefinedSubscription::RemoveFilterAt(unsigned pos)
38 { 82 {
39 if (pos >= mFilters.size()) 83 if (pos >= mFilters.size())
40 return false; 84 return false;
41 85
42 mFilters.erase(mFilters.begin() + pos); 86 mFilters.erase(mFilters.begin() + pos);
43 return true; 87 return true;
44 } 88 }
45 89
46 OwnedString UserDefinedSubscription::Serialize() const 90 OwnedString UserDefinedSubscription::Serialize() const
47 { 91 {
48 OwnedString result(Subscription::Serialize()); 92 OwnedString result(Subscription::Serialize());
49 if (mDefaults) 93 if (mDefaults)
50 { 94 {
51 result.append(u"defaults="_str); 95 result.append(u"defaults="_str);
52 if (mDefaults & Defaults::BLOCKING) 96 if (mDefaults & FilterCategory::BLOCKING)
53 result.append(u" blocking"_str); 97 result.append(u" blocking"_str);
54 if (mDefaults & Defaults::WHITELIST) 98 if (mDefaults & FilterCategory::WHITELIST)
55 result.append(u" whitelist"_str); 99 result.append(u" whitelist"_str);
56 if (mDefaults & Defaults::ELEMHIDE) 100 if (mDefaults & FilterCategory::ELEMHIDE)
57 result.append(u" elemhide"_str); 101 result.append(u" elemhide"_str);
58 result.append(u'\n'); 102 result.append(u'\n');
59 } 103 }
60 return result; 104 return result;
61 } 105 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld