xpcom/base/nsISupportsObsolete.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/base/nsISupportsObsolete.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +
     1.9 +#ifndef nsISupportsObsolete_h__
    1.10 +#define nsISupportsObsolete_h__
    1.11 +
    1.12 +#include "prcmon.h"
    1.13 +
    1.14 +///////////////////////////////////////////////////////////////////////////////
    1.15 +
    1.16 +
    1.17 +#define NS_INIT_REFCNT() NS_INIT_ISUPPORTS()
    1.18 +
    1.19 +/**
    1.20 + * Macro to free an array of pointers to nsISupports (or classes
    1.21 + * derived from it).  A convenience wrapper around
    1.22 + * NS_FREE_XPCOM_POINTER_ARRAY.
    1.23 + *
    1.24 + * Note that if you know that none of your nsISupports pointers are
    1.25 + * going to be 0, you can gain a bit of speed by calling
    1.26 + * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
    1.27 + * free function.
    1.28 + *
    1.29 + * @param size      Number of elements in the array.  If not a constant, this 
    1.30 + *                  should be a int32_t.  Note that this means this macro 
    1.31 + *                  will not work if size >= 2^31.
    1.32 + * @param array     The array to be freed.
    1.33 + */
    1.34 +#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array)                    \
    1.35 +    NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
    1.36 +
    1.37 +
    1.38 +///////////////////////////////////////////////////////////////////////////////
    1.39 +
    1.40 +/* use these functions to associate get/set methods with a
    1.41 +   C++ member variable
    1.42 +*/
    1.43 +
    1.44 +#define NS_METHOD_GETTER(_method, _type, _member) \
    1.45 +_method(_type* aResult) \
    1.46 +{\
    1.47 +    if (!aResult) return NS_ERROR_NULL_POINTER; \
    1.48 +    *aResult = _member; \
    1.49 +    return NS_OK; \
    1.50 +}
    1.51 +    
    1.52 +#define NS_METHOD_SETTER(_method, _type, _member) \
    1.53 +_method(_type aResult) \
    1.54 +{ \
    1.55 +    _member = aResult; \
    1.56 +    return NS_OK; \
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * special for strings to get/set char* strings
    1.61 + * using PL_strdup and PR_FREEIF
    1.62 + */
    1.63 +#define NS_METHOD_GETTER_STR(_method,_member)   \
    1.64 +_method(char* *aString)                         \
    1.65 +{                                               \
    1.66 +    if (!aString) return NS_ERROR_NULL_POINTER; \
    1.67 +    if (!(*aString = PL_strdup(_member)))       \
    1.68 +      return NS_ERROR_OUT_OF_MEMORY;            \
    1.69 +    return NS_OK;                               \
    1.70 +}
    1.71 +
    1.72 +#define NS_METHOD_SETTER_STR(_method, _member) \
    1.73 +_method(const char *aString)                   \
    1.74 +{                                              \
    1.75 +    if (_member) PR_Free(_member);             \
    1.76 +    if (!aString)                              \
    1.77 +      _member = nullptr;                        \
    1.78 +    else if (!(_member = PL_strdup(aString)))  \
    1.79 +      return NS_ERROR_OUT_OF_MEMORY;           \
    1.80 +    return NS_OK;                              \
    1.81 +}
    1.82 +
    1.83 +/* Getter/Setter macros.
    1.84 +   Usage:
    1.85 +   NS_IMPL_[CLASS_]GETTER[_<type>](method, [type,] member);
    1.86 +   NS_IMPL_[CLASS_]SETTER[_<type>](method, [type,] member);
    1.87 +   NS_IMPL_[CLASS_]GETSET[_<type>]([class, ]postfix, [type,] member);
    1.88 +   
    1.89 +   where:
    1.90 +   CLASS_  - implementation is inside a class definition
    1.91 +             (otherwise the class name is needed)
    1.92 +             Do NOT use in publicly exported header files, because
    1.93 +             the implementation may be included many times over.
    1.94 +             Instead, use the non-CLASS_ version.
    1.95 +   _<type> - For more complex (STR, IFACE) data types
    1.96 +             (otherwise the simple data type is needed)
    1.97 +   method  - name of the method, such as GetWidth or SetColor
    1.98 +   type    - simple data type if required
    1.99 +   member  - class member variable such as m_width or mColor
   1.100 +   class   - the class name, such as Window or MyObject
   1.101 +   postfix - Method part after Get/Set such as "Width" for "GetWidth"
   1.102 +   
   1.103 +   Example:
   1.104 +   class Window {
   1.105 +   public:
   1.106 +     NS_IMPL_CLASS_GETSET(Width, int, m_width);
   1.107 +     NS_IMPL_CLASS_GETTER_STR(GetColor, m_color);
   1.108 +     NS_IMETHOD SetColor(char *color);
   1.109 +     
   1.110 +   private:
   1.111 +     int m_width;     // read/write
   1.112 +     char *m_color;   // readonly
   1.113 +   };
   1.114 +
   1.115 +   // defined outside of class
   1.116 +   NS_IMPL_SETTER_STR(Window::GetColor, m_color);
   1.117 +
   1.118 +   Questions/Comments to alecf@netscape.com
   1.119 +*/
   1.120 +
   1.121 +   
   1.122 +/*
   1.123 + * Getter/Setter implementation within a class definition
   1.124 + */
   1.125 +
   1.126 +/* simple data types */
   1.127 +#define NS_IMPL_CLASS_GETTER(_method, _type, _member) \
   1.128 +NS_IMETHOD NS_METHOD_GETTER(_method, _type, _member)
   1.129 +
   1.130 +#define NS_IMPL_CLASS_SETTER(_method, _type, _member) \
   1.131 +NS_IMETHOD NS_METHOD_SETTER(_method, _type, _member)
   1.132 +
   1.133 +#define NS_IMPL_CLASS_GETSET(_postfix, _type, _member) \
   1.134 +NS_IMPL_CLASS_GETTER(Get##_postfix, _type, _member) \
   1.135 +NS_IMPL_CLASS_SETTER(Set##_postfix, _type, _member)
   1.136 +
   1.137 +/* strings */
   1.138 +#define NS_IMPL_CLASS_GETTER_STR(_method, _member) \
   1.139 +NS_IMETHOD NS_METHOD_GETTER_STR(_method, _member)
   1.140 +
   1.141 +#define NS_IMPL_CLASS_SETTER_STR(_method, _member) \
   1.142 +NS_IMETHOD NS_METHOD_SETTER_STR(_method, _member)
   1.143 +
   1.144 +#define NS_IMPL_CLASS_GETSET_STR(_postfix, _member) \
   1.145 +NS_IMPL_CLASS_GETTER_STR(Get##_postfix, _member) \
   1.146 +NS_IMPL_CLASS_SETTER_STR(Set##_postfix, _member)
   1.147 +
   1.148 +/* Getter/Setter implementation outside of a class definition */
   1.149 +
   1.150 +/* simple data types */
   1.151 +#define NS_IMPL_GETTER(_method, _type, _member) \
   1.152 +NS_IMETHODIMP NS_METHOD_GETTER(_method, _type, _member)
   1.153 +
   1.154 +#define NS_IMPL_SETTER(_method, _type, _member) \
   1.155 +NS_IMETHODIMP NS_METHOD_SETTER(_method, _type, _member)
   1.156 +
   1.157 +#define NS_IMPL_GETSET(_class, _postfix, _type, _member) \
   1.158 +NS_IMPL_GETTER(_class::Get##_postfix, _type, _member) \
   1.159 +NS_IMPL_SETTER(_class::Set##_postfix, _type, _member)
   1.160 +
   1.161 +/* strings */
   1.162 +#define NS_IMPL_GETTER_STR(_method, _member) \
   1.163 +NS_IMETHODIMP NS_METHOD_GETTER_STR(_method, _member)
   1.164 +
   1.165 +#define NS_IMPL_SETTER_STR(_method, _member) \
   1.166 +NS_IMETHODIMP NS_METHOD_SETTER_STR(_method, _member)
   1.167 +
   1.168 +#define NS_IMPL_GETSET_STR(_class, _postfix, _member) \
   1.169 +NS_IMPL_GETTER_STR(_class::Get##_postfix, _member) \
   1.170 +NS_IMPL_SETTER_STR(_class::Set##_postfix, _member)
   1.171 +
   1.172 +/**
   1.173 + * IID for the nsIsThreadsafe interface
   1.174 + * {88210890-47a6-11d2-bec3-00805f8a66dc}
   1.175 + *
   1.176 + * This interface is *only* used for debugging purposes to determine if
   1.177 + * a given component is threadsafe.
   1.178 + */
   1.179 +#define NS_ISTHREADSAFE_IID                                                   \
   1.180 +  { 0x88210890, 0x47a6, 0x11d2,                                               \
   1.181 +    {0xbe, 0xc3, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
   1.182 +
   1.183 +#define NS_LOCK_INSTANCE()                                                    \
   1.184 +  PR_CEnterMonitor((void*)this)
   1.185 +#define NS_UNLOCK_INSTANCE()                                                  \
   1.186 +  PR_CExitMonitor((void*)this)
   1.187 +
   1.188 +/**
   1.189 + * This implements query interface with two assumptions: First, the
   1.190 + * class in question implements nsISupports and its own interface and
   1.191 + * nothing else. Second, the implementation of the class's primary
   1.192 + * inheritance chain leads to its own interface.
   1.193 + *
   1.194 + * @param _class The name of the class implementing the method
   1.195 + * @param _classiiddef The name of the #define symbol that defines the IID
   1.196 + * for the class (e.g. NS_ISUPPORTS_IID)
   1.197 + */
   1.198 +#if defined(DEBUG)
   1.199 +#define NS_VERIFY_THREADSAFE_INTERFACE(_iface)                                \
   1.200 + if (nullptr != (_iface)) {                                                   \
   1.201 +   nsISupports* tmp;                                                          \
   1.202 +   static NS_DEFINE_IID(kIsThreadsafeIID, NS_ISTHREADSAFE_IID);               \
   1.203 +   NS_PRECONDITION((NS_OK == _iface->QueryInterface(kIsThreadsafeIID,         \
   1.204 +                                                    (void**)&tmp)),           \
   1.205 +                   "Interface is not threadsafe");                            \
   1.206 + }
   1.207 +#else
   1.208 +#define NS_VERIFY_THREADSAFE_INTERFACE(_iface)
   1.209 +#endif
   1.210 +
   1.211 +////////////////////////////////////////////////////////////////////////////////
   1.212 +
   1.213 +
   1.214 +
   1.215 +#endif

mercurial