1 // Copyright 2011 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef GESTURES_PROP_REGISTRY_H__ 6 #define GESTURES_PROP_REGISTRY_H__ 7 8 #include <set> 9 #include <string> 10 11 #include <json/value.h> 12 13 #include "include/gestures.h" 14 #include "include/logging.h" 15 16 namespace gestures { 17 18 class ActivityLog; 19 class Property; 20 21 class PropRegistry { 22 public: PropRegistry()23 PropRegistry() : prop_provider_(nullptr), activity_log_(nullptr) {} 24 25 void Register(Property* prop); 26 void Unregister(Property* prop); 27 28 void SetPropProvider(GesturesPropProvider* prop_provider, void* data); PropProvider()29 GesturesPropProvider* PropProvider() const { return prop_provider_; } PropProviderData()30 void* PropProviderData() const { return prop_provider_data_; } props()31 const std::set<Property*>& props() const { return props_; } 32 set_activity_log(ActivityLog * activity_log)33 void set_activity_log(ActivityLog* activity_log) { 34 activity_log_ = activity_log; 35 } activity_log()36 ActivityLog* activity_log() const { return activity_log_; } 37 38 private: 39 GesturesPropProvider* prop_provider_; 40 void* prop_provider_data_; 41 std::set<Property*> props_; 42 ActivityLog* activity_log_; 43 }; 44 45 class PropertyDelegate; 46 47 class Property { 48 public: Property(PropRegistry * parent,const char * name)49 Property(PropRegistry* parent, const char* name) 50 : parent_(parent), name_(name) {} 51 ~Property()52 virtual ~Property() { 53 if (parent_) 54 parent_->Unregister(this); 55 } 56 57 void CreateProp(); 58 virtual void CreatePropImpl() = 0; 59 void DestroyProp(); 60 SetDelegate(PropertyDelegate * delegate)61 void SetDelegate(PropertyDelegate* delegate) { 62 delegate_ = delegate; 63 } 64 name()65 const char* name() { return name_; } 66 // Returns a newly allocated Value object 67 virtual Json::Value NewValue() const = 0; 68 // Returns true on success 69 virtual bool SetValue(const Json::Value& value) = 0; 70 StaticHandleGesturesPropWillRead(void * data)71 static GesturesPropBool StaticHandleGesturesPropWillRead(void* data) { 72 GesturesPropBool ret = 73 reinterpret_cast<Property*>(data)->HandleGesturesPropWillRead(); 74 return ret; 75 } 76 // TODO(adlr): pass on will-read notifications HandleGesturesPropWillRead()77 virtual GesturesPropBool HandleGesturesPropWillRead() { return 0; } StaticHandleGesturesPropWritten(void * data)78 static void StaticHandleGesturesPropWritten(void* data) { 79 reinterpret_cast<Property*>(data)->HandleGesturesPropWritten(); 80 } 81 virtual void HandleGesturesPropWritten() = 0; 82 83 protected: 84 GesturesProp* gprop_ = nullptr; 85 PropRegistry* parent_; 86 PropertyDelegate* delegate_ = nullptr; 87 88 private: 89 const char* name_; 90 }; 91 92 class BoolProperty : public Property { 93 public: BoolProperty(PropRegistry * reg,const char * name,GesturesPropBool val)94 BoolProperty(PropRegistry* reg, const char* name, GesturesPropBool val) 95 : Property(reg, name), val_(val) { 96 if (parent_) 97 parent_->Register(this); 98 } 99 virtual void CreatePropImpl(); 100 virtual Json::Value NewValue() const; 101 virtual bool SetValue(const Json::Value& value); 102 virtual void HandleGesturesPropWritten(); 103 104 GesturesPropBool val_; 105 }; 106 107 class BoolArrayProperty : public Property { 108 public: BoolArrayProperty(PropRegistry * reg,const char * name,GesturesPropBool * vals,size_t count)109 BoolArrayProperty(PropRegistry* reg, const char* name, GesturesPropBool* vals, 110 size_t count) 111 : Property(reg, name), vals_(vals), count_(count) { 112 if (parent_) 113 parent_->Register(this); 114 } 115 virtual void CreatePropImpl(); 116 virtual Json::Value NewValue() const; 117 virtual bool SetValue(const Json::Value& list); 118 virtual void HandleGesturesPropWritten(); 119 120 GesturesPropBool* vals_; 121 size_t count_; 122 }; 123 124 class DoubleProperty : public Property { 125 public: DoubleProperty(PropRegistry * reg,const char * name,double val)126 DoubleProperty(PropRegistry* reg, const char* name, double val) 127 : Property(reg, name), val_(val) { 128 if (parent_) 129 parent_->Register(this); 130 } 131 virtual void CreatePropImpl(); 132 virtual Json::Value NewValue() const; 133 virtual bool SetValue(const Json::Value& value); 134 virtual void HandleGesturesPropWritten(); 135 136 double val_; 137 }; 138 139 class DoubleArrayProperty : public Property { 140 public: DoubleArrayProperty(PropRegistry * reg,const char * name,double * vals,size_t count)141 DoubleArrayProperty(PropRegistry* reg, const char* name, double* vals, 142 size_t count) 143 : Property(reg, name), vals_(vals), count_(count) { 144 if (parent_) 145 parent_->Register(this); 146 } 147 virtual void CreatePropImpl(); 148 virtual Json::Value NewValue() const; 149 virtual bool SetValue(const Json::Value& list); 150 virtual void HandleGesturesPropWritten(); 151 152 double* vals_; 153 size_t count_; 154 }; 155 156 class IntProperty : public Property { 157 public: IntProperty(PropRegistry * reg,const char * name,int val)158 IntProperty(PropRegistry* reg, const char* name, int val) 159 : Property(reg, name), val_(val) { 160 if (parent_) 161 parent_->Register(this); 162 } 163 virtual void CreatePropImpl(); 164 virtual Json::Value NewValue() const; 165 virtual bool SetValue(const Json::Value& value); 166 virtual void HandleGesturesPropWritten(); 167 168 int val_; 169 }; 170 171 class IntArrayProperty : public Property { 172 public: IntArrayProperty(PropRegistry * reg,const char * name,int * vals,size_t count)173 IntArrayProperty(PropRegistry* reg, const char* name, int* vals, 174 size_t count) 175 : Property(reg, name), vals_(vals), count_(count) { 176 if (parent_) 177 parent_->Register(this); 178 } 179 virtual void CreatePropImpl(); 180 virtual Json::Value NewValue() const; 181 virtual bool SetValue(const Json::Value& list); 182 virtual void HandleGesturesPropWritten(); 183 184 int* vals_; 185 size_t count_; 186 }; 187 188 class StringProperty : public Property { 189 public: StringProperty(PropRegistry * reg,const char * name,const char * val)190 StringProperty(PropRegistry* reg, const char* name, const char* val) 191 : Property(reg, name), val_(val) { 192 if (parent_) 193 parent_->Register(this); 194 } 195 virtual void CreatePropImpl(); 196 virtual Json::Value NewValue() const; 197 virtual bool SetValue(const Json::Value& value); 198 virtual void HandleGesturesPropWritten(); 199 200 std::string parsed_val_; 201 const char* val_; 202 }; 203 204 class PropertyDelegate { 205 public: BoolWasWritten(BoolProperty * prop)206 virtual void BoolWasWritten(BoolProperty* prop) {}; BoolArrayWasWritten(BoolArrayProperty * prop)207 virtual void BoolArrayWasWritten(BoolArrayProperty* prop) {}; DoubleWasWritten(DoubleProperty * prop)208 virtual void DoubleWasWritten(DoubleProperty* prop) {}; DoubleArrayWasWritten(DoubleArrayProperty * prop)209 virtual void DoubleArrayWasWritten(DoubleArrayProperty* prop) {}; IntWasWritten(IntProperty * prop)210 virtual void IntWasWritten(IntProperty* prop) {}; IntArrayWasWritten(IntArrayProperty * prop)211 virtual void IntArrayWasWritten(IntArrayProperty* prop) {}; StringWasWritten(StringProperty * prop)212 virtual void StringWasWritten(StringProperty* prop) {}; 213 }; 214 215 } // namespace gestures 216 217 #endif // GESTURES_PROP_REGISTRY_H__ 218