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

Side by Side Diff: compiled/subscription/Subscription.h

Issue 29548581: Issue 4128, 5138 - Add Parser and Serializer implemented in C++ Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: rebase Created March 7, 2018, 12:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ABP_NS_BEGIN 58 ABP_NS_BEGIN
59 59
60 class Subscription;
61 typedef intrusive_ptr<Subscription> SubscriptionPtr;
62
60 class Subscription : public ref_counted 63 class Subscription : public ref_counted
61 { 64 {
62 public: 65 public:
63 typedef std::vector<FilterPtr> Filters; 66 typedef std::vector<FilterPtr> Filters;
64 67
65 protected: 68 protected:
66 OwnedString mID; 69 OwnedString mID;
67 Filters mFilters; 70 Filters mFilters;
68 71
69 public: 72 public:
70 enum Type 73 enum Type
71 { 74 {
72 UNKNOWN = 0, 75 UNKNOWN = 0,
73 DOWNLOADABLE = 1, 76 DOWNLOADABLE = 1,
74 USERDEFINED = 2 77 USERDEFINED = 2
75 }; 78 };
76 79
77 explicit Subscription(Type type, const String& id); 80 typedef std::pair<OwnedString, OwnedString> KeyValue;
81 typedef std::vector<KeyValue> KeyValues;
82
83 static Subscription* BINDINGS_EXPORTED FromID(const String& id)
84 {
85 return FromProperties(id, KeyValues()).release();
86 }
87 static SubscriptionPtr FromProperties(const KeyValues& properties);
88
78 ~Subscription(); 89 ~Subscription();
79 90
80 Type mType; 91 Type mType;
81 92
82 const BINDINGS_EXPORTED String& GetID() const 93 const BINDINGS_EXPORTED String& GetID() const
83 { 94 {
84 return mID; 95 return mID;
85 } 96 }
86 97
87 SUBSCRIPTION_STRING_PROPERTY(mTitle, SUBSCRIPTION_TITLE, GetTitle, SetTitle); 98 SUBSCRIPTION_STRING_PROPERTY(mTitle, SUBSCRIPTION_TITLE, GetTitle, SetTitle);
88 SUBSCRIPTION_PROPERTY(bool, mDisabled, SUBSCRIPTION_DISABLED, 99 SUBSCRIPTION_PROPERTY(bool, mDisabled, SUBSCRIPTION_DISABLED,
89 GetDisabled, SetDisabled); 100 GetDisabled, SetDisabled);
90 SUBSCRIPTION_PROPERTY(bool, mListed, NONE, GetListed, SetListed); 101 SUBSCRIPTION_PROPERTY(bool, mListed, NONE, GetListed, SetListed);
91 102
92 Filters::size_type BINDINGS_EXPORTED GetFilterCount() const 103 Filters::size_type BINDINGS_EXPORTED GetFilterCount() const
93 { 104 {
94 return mFilters.size(); 105 return mFilters.size();
95 } 106 }
96 107
97 Filter* BINDINGS_EXPORTED FilterAt(Filters::size_type index); 108 Filter* BINDINGS_EXPORTED FilterAt(Filters::size_type index);
98 int BINDINGS_EXPORTED IndexOfFilter(const Filter& filter); 109 int BINDINGS_EXPORTED IndexOfFilter(const Filter& filter);
99 OwnedString BINDINGS_EXPORTED Serialize() const; 110 void AddFilter(Filter& filter);
100 OwnedString BINDINGS_EXPORTED SerializeFilters() const; 111 const Filters& GetFilters() const
101 112 {
102 static Subscription* BINDINGS_EXPORTED FromID(const String& id); 113 return mFilters;
114 }
115 // behaves similar to virtual function
116 OwnedString SerializeProperties() const;
103 117
104 template<typename T> 118 template<typename T>
105 T* As() 119 T* As()
106 { 120 {
107 if (mType != T::classType) 121 if (mType != T::classType)
108 return nullptr; 122 return nullptr;
109 123
110 return static_cast<T*>(this); 124 return static_cast<T*>(this);
111 } 125 }
112 126
113 template<typename T> 127 template<typename T>
114 const T* As() const 128 const T* As() const
115 { 129 {
116 if (mType != T::classType) 130 if (mType != T::classType)
117 return nullptr; 131 return nullptr;
118 132
119 return static_cast<const T*>(this); 133 return static_cast<const T*>(this);
120 } 134 }
135
136 protected:
137 OwnedString DoSerializeProperties() const;
138 explicit Subscription(Type type, const String& id, const KeyValues& properties );
139 static const String* findPropertyValue(const Subscription::KeyValues& properti es, const String& propertyName);
140 template<typename Member>
141 static void parseProperty(const KeyValues& properties, Member& member, const S tring& key)
142 {
143 if (auto strPropValue = findPropertyValue(properties, key))
144 member = lexical_cast<Member>(*strPropValue);
145 }
146 private:
147 static SubscriptionPtr FromProperties(const String& id, const KeyValues& prope rties);
121 }; 148 };
122 149
123 typedef intrusive_ptr<Subscription> SubscriptionPtr; 150 ABP_NS_END
124
125 ABP_NS_END
OLDNEW

Powered by Google App Engine
This is Rietveld