Bug 716236 - Fix indentation in GuardObjects.h and allow its inclusion into C files; r=Waldo
authorMs2ger <ms2ger@gmail.com>
Sun, 15 Jan 2012 09:13:07 +0100
changeset 84452 6701b741568dcff934d9170db50c81c5a3f8a2d2
parent 84451 21c84409902e21e1677b170053a3bca3885d8ea8
child 84453 b2ef431401ff181a07e13e0bcd5ad2b26f418ae9
push id21852
push userMs2ger@gmail.com
push dateSun, 15 Jan 2012 08:23:22 +0000
treeherdermozilla-central@d7984d345c24 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs716236
milestone12.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 716236 - Fix indentation in GuardObjects.h and allow its inclusion into C files; r=Waldo
mfbt/GuardObjects.h
--- a/mfbt/GuardObjects.h
+++ b/mfbt/GuardObjects.h
@@ -33,130 +33,146 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+/* Implementation of macros to ensure correct use of RAII Auto* objects. */
+
 #ifndef mozilla_GuardObjects_h
 #define mozilla_GuardObjects_h
 
 #include "mozilla/Assertions.h"
 
+#ifdef __cplusplus
+
+#ifdef DEBUG
+
 namespace mozilla {
-  /**
-   * The following classes are designed to cause assertions to detect
-   * inadvertent use of guard objects as temporaries.  In other words,
-   * when we have a guard object whose only purpose is its constructor and
-   * destructor (and is never otherwise referenced), the intended use
-   * might be:
-   *     AutoRestore savePainting(mIsPainting);
-   * but is is easy to accidentally write:
-   *     AutoRestore(mIsPainting);
-   * which compiles just fine, but runs the destructor well before the
-   * intended time.
-   *
-   * They work by adding (#ifdef DEBUG) an additional parameter to the
-   * guard object's constructor, with a default value, so that users of
-   * the guard object's API do not need to do anything.  The default value
-   * of this parameter is a temporary object.  C++ (ISO/IEC 14882:1998),
-   * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
-   * guarantee that temporaries are destroyed in the reverse of their
-   * construction order, but I actually can't find a statement that that
-   * is true in the general case (beyond the two specific cases mentioned
-   * there).  However, it seems to be true.
-   *
-   * These classes are intended to be used only via the macros immediately
-   * below them:
-   *   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
-   *     variable, and should be put where a declaration of a private
-   *     member variable would be placed.
-   *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
-   *     parameters to each constructor of the guard object; it declares
-   *     (ifdef DEBUG) an additional parameter.  (But use the *_ONLY_PARAM
-   *     variant for constructors that take no other parameters.)
-   *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
-   *     the implementation of such constructors when they are not inline.
-   *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
-   *     the implementation of such constructors to pass the parameter to
-   *     a base class that also uses these macros
-   *   MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
-   *     constructor.  It uses the parameter declared by
-   *     MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
-   *
-   * For more details, and examples of using these macros, see
-   * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
-   */
-#ifdef DEBUG
-  class GuardObjectNotifier
-  {
+namespace detail {
+/*
+ * The following classes are designed to cause assertions to detect
+ * inadvertent use of guard objects as temporaries. In other words,
+ * when we have a guard object whose only purpose is its constructor and
+ * destructor (and is never otherwise referenced), the intended use
+ * might be:
+ *
+ *     AutoRestore savePainting(mIsPainting);
+ *
+ * but is is easy to accidentally write:
+ *
+ *     AutoRestore(mIsPainting);
+ *
+ * which compiles just fine, but runs the destructor well before the
+ * intended time.
+ *
+ * They work by adding (#ifdef DEBUG) an additional parameter to the
+ * guard object's constructor, with a default value, so that users of
+ * the guard object's API do not need to do anything. The default value
+ * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
+ * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
+ * guarantee that temporaries are destroyed in the reverse of their
+ * construction order, but I actually can't find a statement that that
+ * is true in the general case (beyond the two specific cases mentioned
+ * there). However, it seems to be true.
+ *
+ * These classes are intended to be used only via the macros immediately
+ * below them:
+ *
+ *   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
+ *     variable, and should be put where a declaration of a private
+ *     member variable would be placed.
+ *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
+ *     parameters to each constructor of the guard object; it declares
+ *     (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
+ *     variant for constructors that take no other parameters.)
+ *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
+ *     the implementation of such constructors when they are not inline.
+ *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
+ *     the implementation of such constructors to pass the parameter to
+ *     a base class that also uses these macros
+ *   MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
+ *     constructor. It uses the parameter declared by
+ *     MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
+ *
+ * For more details, and examples of using these macros, see
+ * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
+ */
+class GuardObjectNotifier
+{
   private:
-    bool* mStatementDone;
+    bool* statementDone;
+
   public:
-    GuardObjectNotifier() : mStatementDone(NULL) {}
+    GuardObjectNotifier() : statementDone(NULL) {}
 
     ~GuardObjectNotifier() {
-      *mStatementDone = true;
+        *statementDone = true;
     }
 
-    void SetStatementDone(bool *aStatementDone) {
-      mStatementDone = aStatementDone;
+    void setStatementDone(bool* statementIsDone) {
+        statementDone = statementIsDone;
     }
-  };
+};
 
-  class GuardObjectNotificationReceiver
-  {
+class GuardObjectNotificationReceiver
+{
   private:
-    bool mStatementDone;
+    bool statementDone;
+
   public:
-    GuardObjectNotificationReceiver() : mStatementDone(false) {}
+    GuardObjectNotificationReceiver() : statementDone(false) {}
 
     ~GuardObjectNotificationReceiver() {
-      /*
-       * Assert that the guard object was not used as a temporary.
-       * (Note that this assert might also fire if Init is not called
-       * because the guard object's implementation is not using the
-       * above macros correctly.)
-       */
-      MOZ_ASSERT(mStatementDone);
+        /*
+         * Assert that the guard object was not used as a temporary.
+         * (Note that this assert might also fire if init is not called
+         * because the guard object's implementation is not using the
+         * above macros correctly.)
+         */
+        MOZ_ASSERT(statementDone);
     }
 
-    void Init(const GuardObjectNotifier &aNotifier) {
-      /*
-       * aNotifier is passed as a const reference so that we can pass a
-       * temporary, but we really intend it as non-const
-       */
-      const_cast<GuardObjectNotifier&>(aNotifier).
-          SetStatementDone(&mStatementDone);
+    void init(const GuardObjectNotifier& constNotifier) {
+        /*
+         * constNotifier is passed as a const reference so that we can pass a
+         * temporary, but we really intend it as non-const.
+         */
+        GuardObjectNotifier& notifier = const_cast<GuardObjectNotifier&>(constNotifier);
+        notifier.setStatementDone(&statementDone);
     }
-  };
+};
+
+} /* namespace detail */
+} /* namespace mozilla */
+
+#endif /* DEBUG */
 
-  #define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
-      mozilla::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
-      , const mozilla::GuardObjectNotifier& _notifier = \
-                mozilla::GuardObjectNotifier()
-  #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
-      const mozilla::GuardObjectNotifier& _notifier = \
-              mozilla::GuardObjectNotifier()
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
-      , const mozilla::GuardObjectNotifier& _notifier
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
-      , _notifier
-  #define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
-      PR_BEGIN_MACRO _mCheckNotUsedAsTemporary.Init(_notifier); PR_END_MACRO
+#ifdef DEBUG
+#  define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
+     mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
+     , const mozilla::detail::GuardObjectNotifier& _notifier = \
+         mozilla::detail::GuardObjectNotifier()
+#  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
+     const mozilla::detail::GuardObjectNotifier& _notifier = \
+         mozilla::detail::GuardObjectNotifier()
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
+     , const mozilla::detail::GuardObjectNotifier& _notifier
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
+     , _notifier
+#  define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
+     do { _mCheckNotUsedAsTemporary.init(_notifier); } while (0)
+#else
+#  define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
+#  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
+#  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
+#  define MOZ_GUARD_OBJECT_NOTIFIER_INIT do { } while (0)
+#endif
 
-#else /* defined(DEBUG) */
-
-  #define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
-  #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
-  #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
-  #define MOZ_GUARD_OBJECT_NOTIFIER_INIT PR_BEGIN_MACRO PR_END_MACRO
-
-#endif /* !defined(DEBUG) */
-
-} // namespace mozilla
+#endif /* __cplusplus */
 
 #endif /* mozilla_GuardObjects_h */