Back to index

enigmail  1.4.3
GuardObjects.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim: set ts=8 sw=4 et tw=99 ft=cpp: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * the Mozilla Foundation.
00020  * Portions created by the Initial Developer are Copyright (C) 2010
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   L. David Baron <dbaron@dbaron.org>, Mozilla Corporation (original author)
00025  *   Ms2ger <ms2ger@gmail.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* Implementation of macros to ensure correct use of RAII Auto* objects. */
00042 
00043 #ifndef mozilla_GuardObjects_h
00044 #define mozilla_GuardObjects_h
00045 
00046 #include "mozilla/Assertions.h"
00047 #include "mozilla/Types.h"
00048 
00049 #ifdef __cplusplus
00050 
00051 #ifdef DEBUG
00052 
00053 namespace mozilla {
00054 namespace detail {
00055 /*
00056  * The following classes are designed to cause assertions to detect
00057  * inadvertent use of guard objects as temporaries. In other words,
00058  * when we have a guard object whose only purpose is its constructor and
00059  * destructor (and is never otherwise referenced), the intended use
00060  * might be:
00061  *
00062  *     AutoRestore savePainting(mIsPainting);
00063  *
00064  * but is is easy to accidentally write:
00065  *
00066  *     AutoRestore(mIsPainting);
00067  *
00068  * which compiles just fine, but runs the destructor well before the
00069  * intended time.
00070  *
00071  * They work by adding (#ifdef DEBUG) an additional parameter to the
00072  * guard object's constructor, with a default value, so that users of
00073  * the guard object's API do not need to do anything. The default value
00074  * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
00075  * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
00076  * guarantee that temporaries are destroyed in the reverse of their
00077  * construction order, but I actually can't find a statement that that
00078  * is true in the general case (beyond the two specific cases mentioned
00079  * there). However, it seems to be true.
00080  *
00081  * These classes are intended to be used only via the macros immediately
00082  * below them:
00083  *
00084  *   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
00085  *     variable, and should be put where a declaration of a private
00086  *     member variable would be placed.
00087  *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
00088  *     parameters to each constructor of the guard object; it declares
00089  *     (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
00090  *     variant for constructors that take no other parameters.)
00091  *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
00092  *     the implementation of such constructors when they are not inline.
00093  *   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
00094  *     the implementation of such constructors to pass the parameter to
00095  *     a base class that also uses these macros
00096  *   MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
00097  *     constructor. It uses the parameter declared by
00098  *     MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
00099  *
00100  * For more details, and examples of using these macros, see
00101  * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
00102  */
00103 class MOZ_EXPORT_API(GuardObjectNotifier)
00104 {
00105   private:
00106     bool* statementDone;
00107 
00108   public:
00109     GuardObjectNotifier() : statementDone(NULL) {}
00110 
00111     ~GuardObjectNotifier() {
00112         *statementDone = true;
00113     }
00114 
00115     void setStatementDone(bool* statementIsDone) {
00116         statementDone = statementIsDone;
00117     }
00118 };
00119 
00120 class MOZ_EXPORT_API(GuardObjectNotificationReceiver)
00121 {
00122   private:
00123     bool statementDone;
00124 
00125   public:
00126     GuardObjectNotificationReceiver() : statementDone(false) {}
00127 
00128     ~GuardObjectNotificationReceiver() {
00129         /*
00130          * Assert that the guard object was not used as a temporary.
00131          * (Note that this assert might also fire if init is not called
00132          * because the guard object's implementation is not using the
00133          * above macros correctly.)
00134          */
00135         MOZ_ASSERT(statementDone);
00136     }
00137 
00138     void init(const GuardObjectNotifier& constNotifier) {
00139         /*
00140          * constNotifier is passed as a const reference so that we can pass a
00141          * temporary, but we really intend it as non-const.
00142          */
00143         GuardObjectNotifier& notifier = const_cast<GuardObjectNotifier&>(constNotifier);
00144         notifier.setStatementDone(&statementDone);
00145     }
00146 };
00147 
00148 } /* namespace detail */
00149 } /* namespace mozilla */
00150 
00151 #endif /* DEBUG */
00152 
00153 #ifdef DEBUG
00154 #  define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
00155      mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
00156 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
00157      , const mozilla::detail::GuardObjectNotifier& _notifier = \
00158          mozilla::detail::GuardObjectNotifier()
00159 #  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
00160      const mozilla::detail::GuardObjectNotifier& _notifier = \
00161          mozilla::detail::GuardObjectNotifier()
00162 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
00163      , const mozilla::detail::GuardObjectNotifier& _notifier
00164 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
00165      , _notifier
00166 #  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT \
00167        _notifier
00168 #  define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
00169      do { _mCheckNotUsedAsTemporary.init(_notifier); } while (0)
00170 #else
00171 #  define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
00172 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
00173 #  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
00174 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
00175 #  define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT
00176 #  define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
00177 #  define MOZ_GUARD_OBJECT_NOTIFIER_INIT do { } while (0)
00178 #endif
00179 
00180 #endif /* __cplusplus */
00181 
00182 #endif /* mozilla_GuardObjects_h */