OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #pragma once | 18 #pragma once |
19 | 19 |
| 20 #include <type_traits> |
20 #include <vector> | 21 #include <vector> |
21 | 22 |
22 #include "../String.h" | 23 #include "../String.h" |
23 #include "../intrusive_ptr.h" | 24 #include "../intrusive_ptr.h" |
24 #include "../debug.h" | 25 #include "../debug.h" |
25 #include "../bindings/runtime.h" | 26 #include "../bindings/runtime.h" |
26 | 27 |
27 class Filter : public ref_counted | 28 class Filter : public ref_counted |
28 { | 29 { |
29 protected: | 30 protected: |
30 OwnedString mText; | 31 OwnedString mText; |
31 | 32 |
32 public: | 33 public: |
33 enum Type | 34 enum Type |
34 { | 35 { |
35 UNKNOWN = 0, | 36 UNKNOWN = 0, |
36 INVALID = 1, | 37 INVALID = 1, |
37 COMMENT = 2, | 38 COMMENT = 2, |
38 BLOCKING = 3, | 39 ACTIVE = 4, |
39 WHITELIST = 4, | 40 REGEXP = ACTIVE | 8, |
40 ELEMHIDE = 5, | 41 BLOCKING = REGEXP | 16, |
41 ELEMHIDEEXCEPTION = 6, | 42 WHITELIST = REGEXP | 32, |
42 ELEMHIDEEMULATION = 7, | 43 ELEMHIDEBASE = ACTIVE | 64, |
43 VALUE_COUNT = 8 | 44 ELEMHIDE = ELEMHIDEBASE | 128, |
| 45 ELEMHIDEEXCEPTION = ELEMHIDEBASE | 256, |
| 46 ELEMHIDEEMULATION = ELEMHIDEBASE | 512 |
44 }; | 47 }; |
45 | 48 |
46 explicit Filter(Type type, const String& text); | 49 explicit Filter(Type type, const String& text); |
47 ~Filter(); | 50 ~Filter(); |
48 | 51 |
49 Type mType; | 52 Type mType; |
50 | 53 |
51 const String& BINDINGS_EXPORTED GetText() const | 54 const String& BINDINGS_EXPORTED GetText() const |
52 { | 55 { |
53 return mText; | 56 return mText; |
54 } | 57 } |
55 | 58 |
56 OwnedString BINDINGS_EXPORTED Serialize() const; | 59 OwnedString BINDINGS_EXPORTED Serialize() const; |
57 | 60 |
58 static Filter* BINDINGS_EXPORTED FromText(DependentString& text); | 61 static Filter* BINDINGS_EXPORTED FromText(DependentString& text); |
| 62 |
| 63 template<typename T, |
| 64 typename std::enable_if<std::is_base_of<Filter, T>::value>::type* = nullptr> |
| 65 T* As() |
| 66 { |
| 67 if ((mType & T::classType) != T::classType) |
| 68 return nullptr; |
| 69 |
| 70 return static_cast<T*>(this); |
| 71 } |
| 72 |
| 73 template<typename T, |
| 74 typename std::enable_if<std::is_base_of<Filter, T>::value>::type* = nullptr> |
| 75 const T* As() const |
| 76 { |
| 77 if ((mType & T::classType) != T::classType) |
| 78 return nullptr; |
| 79 |
| 80 return static_cast<const T*>(this); |
| 81 } |
59 }; | 82 }; |
60 | 83 |
61 typedef intrusive_ptr<Filter> FilterPtr; | 84 typedef intrusive_ptr<Filter> FilterPtr; |
OLD | NEW |