OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Ginsu Authors. All rights reserved. | |
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 C_SALT_PROPERTY_H_ | |
6 #define C_SALT_PROPERTY_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "c_salt/variant.h" | |
11 #include "c_salt/variant_ptrs.h" | |
12 | |
13 namespace c_salt { | |
14 | |
15 // Class group that provides a wrapper for generic properties. A Property can | |
16 // be published to the browser via a ScriptingBridge. A Property maintains a | |
17 // variant value (currently a c_salt::Type), and sends notifications to | |
18 // observers about changes to the value. Properties have a name that used by | |
19 // browser code. You create a Property by setting up a PropertyParameter | |
20 // object. | |
21 | |
22 // Support class for the Named Parameter Idiom. To create a static, | |
23 // immutable Property using this idiom: | |
24 // SharedVariant value(new c_salt::Variant(42)); | |
25 // PropertyAttributes prop_attrib("myProp", value) | |
26 // .set_static() | |
27 // .set_immutable(); | |
28 // Property *property = new Property(prop_attribs); | |
29 // Glossary: | |
30 // mutable means the property value can be changed from the browser. For | |
31 // example, this JavaScript code changes the value of "myProp" to 42: | |
32 // myModule.myProp = 42; | |
33 // immutable means the property is read-only to the browser. Attempting to | |
34 // change an immutable property from the browser has no effect. | |
35 // static means the property was added by the NaCl module code, usually | |
36 // during initialization of a ScriptingBridge. These properties cannot | |
37 // be deleted by the browser. All properties added by NaCl code should | |
38 // static. | |
39 // dynamic means the property was added by the browser. All properties added | |
40 // by the browser are dynamic. For example, if "dynProp" was _not_ | |
41 // added by the NaCl code, then this JavaScript will add a dynamic | |
42 // property and set its value: | |
43 // myModule.dynProp = "hello, world"; | |
44 // Dynamic properties are always mutable. Dynamic properties can be | |
45 // removed by the browser, for example by using the JavaScript delete | |
46 // operator. | |
47 class PropertyAttributes { | |
48 public: | |
49 PropertyAttributes(const std::string& name, const SharedVariant& value); | |
50 PropertyAttributes& set_dynamic(); | |
51 PropertyAttributes& set_static(); | |
52 PropertyAttributes& set_immutable(); | |
53 PropertyAttributes& set_mutable(); | |
54 | |
55 private: | |
56 friend class Property; | |
57 std::string name_; // Must be set in the ctor. | |
58 bool is_static_; // Default is |true|. | |
59 bool is_mutable_; // Default is |true|. | |
60 SharedVariant value_; | |
61 }; | |
62 | |
63 inline PropertyAttributes::PropertyAttributes(const std::string& name, | |
64 const SharedVariant& value) | |
65 : name_(name), | |
66 is_static_(true), | |
67 is_mutable_(true), | |
68 value_(value) { | |
69 } | |
70 | |
71 inline PropertyAttributes& PropertyAttributes::set_dynamic() { | |
72 is_static_ = false; | |
73 return *this; | |
74 } | |
75 | |
76 inline PropertyAttributes& PropertyAttributes::set_static() { | |
77 is_static_ = true; | |
78 return *this; | |
79 } | |
80 | |
81 inline PropertyAttributes& PropertyAttributes::set_immutable() { | |
82 is_mutable_ = false; | |
83 return *this; | |
84 } | |
85 | |
86 inline PropertyAttributes& PropertyAttributes::set_mutable() { | |
87 is_mutable_ = true; | |
88 return *this; | |
89 } | |
90 | |
91 class Property { | |
92 public: | |
93 | |
94 explicit Property(const PropertyAttributes& attributes); | |
95 | |
96 // Get the value of the property. This triggers the observer's | |
97 // WillGetProperty and DidGetProperty protocol methods. | |
98 SharedVariant GetValue() const; | |
99 | |
100 // Set the value of the property. This triggers the observer's | |
101 // WillSetProperty and DidSetProperty protocol methods. | |
102 void SetValue(const SharedVariant& new_value); | |
103 | |
104 // Accessors for various attributes. These cannot be changed during the | |
105 // life of the Property instance. | |
106 const std::string& name() const { | |
107 return name_; | |
108 } | |
109 bool is_mutable() const { | |
110 return is_mutable_; | |
111 } | |
112 bool is_static() const { | |
113 return is_static_; | |
114 } | |
115 | |
116 private: | |
117 std::string name_; | |
118 bool is_static_; | |
119 bool is_mutable_; | |
120 SharedVariant value_; | |
121 | |
122 Property(); // Not implemented, do not use. | |
123 }; | |
124 | |
125 } // namespace c_salt | |
126 | |
127 #endif // C_SALT_PROPERTY_H_ | |
OLD | NEW |