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 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 }\ | 48 }\ |
49 } | 49 } |
50 | 50 |
51 #define SUBSCRIPTION_PROPERTY(type, name, topic, getter, setter) \ | 51 #define SUBSCRIPTION_PROPERTY(type, name, topic, getter, setter) \ |
52 static_assert(std::is_arithmetic<type>::value, "SUBSCRIPTION_PROPERTY macro
can only be used with arithmetic types");\ | 52 static_assert(std::is_arithmetic<type>::value, "SUBSCRIPTION_PROPERTY macro
can only be used with arithmetic types");\ |
53 SUBSCRIPTION_PROPERTY_INTERNAL(type, type, name, topic, getter, setter) | 53 SUBSCRIPTION_PROPERTY_INTERNAL(type, type, name, topic, getter, setter) |
54 | 54 |
55 #define SUBSCRIPTION_STRING_PROPERTY(name, topic, getter, setter) \ | 55 #define SUBSCRIPTION_STRING_PROPERTY(name, topic, getter, setter) \ |
56 SUBSCRIPTION_PROPERTY_INTERNAL(OwnedString, const String&, name, topic, gett
er, setter) | 56 SUBSCRIPTION_PROPERTY_INTERNAL(OwnedString, const String&, name, topic, gett
er, setter) |
57 | 57 |
| 58 class Subscription; |
| 59 typedef intrusive_ptr<Subscription> SubscriptionPtr; |
| 60 |
58 class Subscription : public ref_counted | 61 class Subscription : public ref_counted |
59 { | 62 { |
60 public: | 63 public: |
61 typedef std::vector<FilterPtr> Filters; | 64 typedef std::vector<FilterPtr> Filters; |
62 | 65 |
63 protected: | 66 protected: |
64 OwnedString mID; | 67 OwnedString mID; |
65 Filters mFilters; | 68 Filters mFilters; |
66 | 69 |
67 public: | 70 public: |
68 enum Type | 71 enum Type |
69 { | 72 { |
70 UNKNOWN = 0, | 73 UNKNOWN = 0, |
71 DOWNLOADABLE = 1, | 74 DOWNLOADABLE = 1, |
72 USERDEFINED = 2 | 75 USERDEFINED = 2 |
73 }; | 76 }; |
74 | 77 |
75 explicit Subscription(Type type, const String& id); | 78 typedef std::pair<OwnedString, OwnedString> KeyValue; |
| 79 typedef std::vector<KeyValue> KeyValues; |
| 80 |
| 81 static Subscription* BINDINGS_EXPORTED FromID(const String& id) |
| 82 { |
| 83 return FromProperties(id, KeyValues()).release(); |
| 84 } |
| 85 static SubscriptionPtr FromProperties(const KeyValues& properties); |
| 86 |
76 ~Subscription(); | 87 ~Subscription(); |
77 | 88 |
78 Type mType; | 89 Type mType; |
79 | 90 |
80 const BINDINGS_EXPORTED String& GetID() const | 91 const BINDINGS_EXPORTED String& GetID() const |
81 { | 92 { |
82 return mID; | 93 return mID; |
83 } | 94 } |
84 | 95 |
85 SUBSCRIPTION_STRING_PROPERTY(mTitle, SUBSCRIPTION_TITLE, GetTitle, SetTitle); | 96 SUBSCRIPTION_STRING_PROPERTY(mTitle, SUBSCRIPTION_TITLE, GetTitle, SetTitle); |
86 SUBSCRIPTION_PROPERTY(bool, mDisabled, SUBSCRIPTION_DISABLED, | 97 SUBSCRIPTION_PROPERTY(bool, mDisabled, SUBSCRIPTION_DISABLED, |
87 GetDisabled, SetDisabled); | 98 GetDisabled, SetDisabled); |
88 SUBSCRIPTION_PROPERTY(bool, mListed, NONE, GetListed, SetListed); | 99 SUBSCRIPTION_PROPERTY(bool, mListed, NONE, GetListed, SetListed); |
89 | 100 |
90 Filters::size_type BINDINGS_EXPORTED GetFilterCount() const | 101 Filters::size_type BINDINGS_EXPORTED GetFilterCount() const |
91 { | 102 { |
92 return mFilters.size(); | 103 return mFilters.size(); |
93 } | 104 } |
94 | 105 |
95 Filter* BINDINGS_EXPORTED FilterAt(Filters::size_type index); | 106 Filter* BINDINGS_EXPORTED FilterAt(Filters::size_type index); |
96 int BINDINGS_EXPORTED IndexOfFilter(const Filter& filter); | 107 int BINDINGS_EXPORTED IndexOfFilter(const Filter& filter); |
97 OwnedString BINDINGS_EXPORTED Serialize() const; | 108 void AddFilter(Filter& filter); |
98 OwnedString BINDINGS_EXPORTED SerializeFilters() const; | 109 const Filters& GetFilters() const |
99 | 110 { |
100 static Subscription* BINDINGS_EXPORTED FromID(const String& id); | 111 return mFilters; |
| 112 } |
| 113 // behaves similar to virtual function |
| 114 OwnedString SerializeProperties() const; |
101 | 115 |
102 template<typename T> | 116 template<typename T> |
103 T* As(); | 117 T* As(); |
104 }; | |
105 | 118 |
106 typedef intrusive_ptr<Subscription> SubscriptionPtr; | 119 template<typename T> |
| 120 const T* As() const |
| 121 { |
| 122 return const_cast<Subscription*>(this)->As<T>(); |
| 123 } |
| 124 protected: |
| 125 OwnedString DoSerializeProperties() const; |
| 126 explicit Subscription(Type type, const String& id, const KeyValues& properties
); |
| 127 static const String* findPropertyValue(const Subscription::KeyValues& properti
es, const String& propertyName); |
| 128 template<typename Member> |
| 129 static void parseProperty(const KeyValues& properties, Member& member, const S
tring& key) |
| 130 { |
| 131 if (auto strPropValue = findPropertyValue(properties, key)) |
| 132 member = AdaptValue<Member>(*strPropValue); |
| 133 } |
| 134 private: |
| 135 static SubscriptionPtr FromProperties(const String& id, const KeyValues& prope
rties); |
| 136 }; |
OLD | NEW |