Bug 1550422 - P10. Define StaticPrefs setter. r?njn! draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 13 May 2019 21:06:12 +1000
changeset 2008587 7475652b856d0b9a584d454a2fda7e482556b497
parent 2008586 04c90f4ed76f625cc99d4a53f6ed6bb3fa7bb44a
child 2008588 1f51118a5843147af0784215fab26007dcda31e9
push id363925
push userjyavenard@mozilla.com
push dateSat, 18 May 2019 07:53:18 +0000
treeherdertry@5082cd581229 [default view] [failures only]
reviewersnjn
bugs1550422
milestone68.0a1
Bug 1550422 - P10. Define StaticPrefs setter. r?njn! This is used by some gfx code and is required to convert gfxPrefs into StaticPrefs. The setter only modifies the value of the StaticPref in the current process and doesn't propagate to others. Differential Revision: https://phabricator.services.mozilla.com/D31254
modules/libpref/StaticPrefs.h
--- a/modules/libpref/StaticPrefs.h
+++ b/modules/libpref/StaticPrefs.h
@@ -51,34 +51,49 @@ struct IsAtomic : FalseType {};
 template <typename T, MemoryOrdering Order>
 struct IsAtomic<Atomic<T, Order>> : TrueType {};
 
 class StaticPrefs {
 // For a VarCache pref like this:
 //
 //   VARCACHE_PREF("my.varcache", my_varcache, int32_t, 99)
 //
-// we generate a static variable declaration and a getter definition:
+// we generate a static variable declaration, a getter and a setter definition.
+// A StaticPref can be set by using the corresponding Set method. For
+// example, if the accessor is Foo() then calling SetFoo(...) will update
+// the preference and also change the return value of subsequent Foo() calls.
+// Changing StaticPrefs values in one process will not affect the result in
+// other processes.
+// Important Note: The use of the Setter is strongly discouraged.
 //
 //   private:
 //     static int32_t sVarCache_my_varcache;
 //   public:
 //     static int32_t my_varcache() { return sVarCache_my_varcache; }
+//     static void Setmy_varcache(int32_t aValue) {
+//	     sVarCache_my_varcache = aValue;
+//     }
 //
 #define PREF(str, cpp_type, default_value)
-#define VARCACHE_PREF(str, id, cpp_type, default_value)        \
- private:                                                      \
-  static cpp_type sVarCache_##id;                              \
-                                                               \
- public:                                                       \
-  static StripAtomic<cpp_type> id() {                          \
-    MOZ_ASSERT(IsAtomic<cpp_type>::value || NS_IsMainThread(), \
-               "Non-atomic static pref '" str                  \
-               "' being accessed on background thread");       \
-    return sVarCache_##id;                                     \
+#define VARCACHE_PREF(str, id, cpp_type, default_value)            \
+ private:                                                          \
+  static cpp_type sVarCache_##id;                                  \
+                                                                   \
+ public:                                                           \
+  static StripAtomic<cpp_type> id() {                              \
+    MOZ_ASSERT(IsAtomic<cpp_type>::value || NS_IsMainThread(),     \
+               "Non-atomic static pref '" str                      \
+               "' being accessed on background thread by getter"); \
+    return sVarCache_##id;                                         \
+  }                                                                \
+  static void Set##id(StripAtomic<cpp_type> aValue) {              \
+    MOZ_ASSERT(IsAtomic<cpp_type>::value || NS_IsMainThread(),     \
+               "Non-atomic static set pref '" str                  \
+               "' being accessed on background thread by setter"); \
+    sVarCache_##id = aValue;                                       \
   }
 #include "mozilla/StaticPrefList.h"
 #undef PREF
 #undef VARCACHE_PREF
 
  public:
   static void InitAll(bool aIsStartup);
 };