summaryrefslogtreecommitdiff
path: root/include/prop_registry.h
blob: 836b98b8c013810b69e95a4aa769aa9e7c2167d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2011 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GESTURES_PROP_REGISTRY_H__
#define GESTURES_PROP_REGISTRY_H__

#include <set>
#include <string>

#include <json/value.h>

#include "include/gestures.h"
#include "include/logging.h"

namespace gestures {

class ActivityLog;
class Property;

class PropRegistry {
 public:
  PropRegistry() : prop_provider_(NULL), activity_log_(NULL) {}

  void Register(Property* prop);
  void Unregister(Property* prop);

  void SetPropProvider(GesturesPropProvider* prop_provider, void* data);
  GesturesPropProvider* PropProvider() const { return prop_provider_; }
  void* PropProviderData() const { return prop_provider_data_; }
  const std::set<Property*>& props() const { return props_; }

  void set_activity_log(ActivityLog* activity_log) {
    activity_log_ = activity_log;
  }
  ActivityLog* activity_log() const { return activity_log_; }

 private:
  GesturesPropProvider* prop_provider_;
  void* prop_provider_data_;
  std::set<Property*> props_;
  ActivityLog* activity_log_;
};

class PropertyDelegate;

class Property {
 public:
  Property(PropRegistry* parent, const char* name,
           PropertyDelegate* delegate)
      : gprop_(NULL), parent_(parent), delegate_(delegate), name_(name) {}

  virtual ~Property() {
    if (parent_)
      parent_->Unregister(this);
  }

  void CreateProp();
  virtual void CreatePropImpl() = 0;
  void DestroyProp();

  // b/253585974
  // delegate used to get passed as a constructor parameter but that
  // led to a chance that the delegate was 'this' and setting the
  // delegate to 'this' in the initializer list allowed the property
  // creation to use 'this' before it was initialized.  This could
  // lead to unexpected behavior and if you were lucky to a crash.
  //
  // Now the delegate is always NULL on initilization of the property
  // instance and after the delegate and property are completely
  // created the user should set the delegate in the constructor
  // body. This will allow access to this in a safe manner.
  void SetDelegate(PropertyDelegate* delegate);

  const char* name() { return name_; }
  // Returns a newly allocated Value object
  virtual Json::Value NewValue() const = 0;
  // Returns true on success
  virtual bool SetValue(const Json::Value& value) = 0;

  static GesturesPropBool StaticHandleGesturesPropWillRead(void* data) {
    GesturesPropBool ret =
        reinterpret_cast<Property*>(data)->HandleGesturesPropWillRead();
    return ret;
  }
  // TODO(adlr): pass on will-read notifications
  virtual GesturesPropBool HandleGesturesPropWillRead() { return 0; }
  static void StaticHandleGesturesPropWritten(void* data) {
    reinterpret_cast<Property*>(data)->HandleGesturesPropWritten();
  }
  virtual void HandleGesturesPropWritten() = 0;

 protected:
  GesturesProp* gprop_;
  PropRegistry* parent_;
  PropertyDelegate* delegate_;

 private:
  const char* name_;
};

class BoolProperty : public Property {
 public:
  BoolProperty(PropRegistry* reg, const char* name, GesturesPropBool val,
               PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), val_(val) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& value);
  virtual void HandleGesturesPropWritten();

  GesturesPropBool val_;
};

class BoolArrayProperty : public Property {
 public:
  BoolArrayProperty(PropRegistry* reg, const char* name, GesturesPropBool* vals,
                    size_t count, PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), vals_(vals), count_(count) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& list);
  virtual void HandleGesturesPropWritten();

  GesturesPropBool* vals_;
  size_t count_;
};

class DoubleProperty : public Property {
 public:
  DoubleProperty(PropRegistry* reg, const char* name, double val,
                 PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), val_(val) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& value);
  virtual void HandleGesturesPropWritten();

  double val_;
};

class DoubleArrayProperty : public Property {
 public:
  DoubleArrayProperty(PropRegistry* reg, const char* name, double* vals,
                      size_t count, PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), vals_(vals), count_(count) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& list);
  virtual void HandleGesturesPropWritten();

  double* vals_;
  size_t count_;
};

class IntProperty : public Property {
 public:
  IntProperty(PropRegistry* reg, const char* name, int val,
              PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), val_(val) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& value);
  virtual void HandleGesturesPropWritten();

  int val_;
};

class IntArrayProperty : public Property {
 public:
  IntArrayProperty(PropRegistry* reg, const char* name, int* vals, size_t count,
                   PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), vals_(vals), count_(count) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& list);
  virtual void HandleGesturesPropWritten();

  int* vals_;
  size_t count_;
};

class StringProperty : public Property {
 public:
  StringProperty(PropRegistry* reg, const char* name, const char* val,
                 PropertyDelegate* delegate = NULL)
      : Property(reg, name, delegate), val_(val) {
    if (parent_)
      parent_->Register(this);
  }
  virtual void CreatePropImpl();
  virtual Json::Value NewValue() const;
  virtual bool SetValue(const Json::Value& value);
  virtual void HandleGesturesPropWritten();

  std::string parsed_val_;
  const char* val_;
};

class PropertyDelegate {
 public:
  virtual void BoolWasWritten(BoolProperty* prop) {};
  virtual void BoolArrayWasWritten(BoolArrayProperty* prop) {};
  virtual void DoubleWasWritten(DoubleProperty* prop) {};
  virtual void DoubleArrayWasWritten(DoubleArrayProperty* prop) {};
  virtual void IntWasWritten(IntProperty* prop) {};
  virtual void IntArrayWasWritten(IntArrayProperty* prop) {};
  virtual void StringWasWritten(StringProperty* prop) {};
};

}  // namespace gestures

#endif  // GESTURES_PROP_REGISTRY_H__