michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #ifndef nsISupportsObsolete_h__ michael@0: #define nsISupportsObsolete_h__ michael@0: michael@0: #include "prcmon.h" michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: michael@0: #define NS_INIT_REFCNT() NS_INIT_ISUPPORTS() michael@0: michael@0: /** michael@0: * Macro to free an array of pointers to nsISupports (or classes michael@0: * derived from it). A convenience wrapper around michael@0: * NS_FREE_XPCOM_POINTER_ARRAY. michael@0: * michael@0: * Note that if you know that none of your nsISupports pointers are michael@0: * going to be 0, you can gain a bit of speed by calling michael@0: * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your michael@0: * free function. michael@0: * michael@0: * @param size Number of elements in the array. If not a constant, this michael@0: * should be a int32_t. Note that this means this macro michael@0: * will not work if size >= 2^31. michael@0: * @param array The array to be freed. michael@0: */ michael@0: #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \ michael@0: NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE) michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* use these functions to associate get/set methods with a michael@0: C++ member variable michael@0: */ michael@0: michael@0: #define NS_METHOD_GETTER(_method, _type, _member) \ michael@0: _method(_type* aResult) \ michael@0: {\ michael@0: if (!aResult) return NS_ERROR_NULL_POINTER; \ michael@0: *aResult = _member; \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: #define NS_METHOD_SETTER(_method, _type, _member) \ michael@0: _method(_type aResult) \ michael@0: { \ michael@0: _member = aResult; \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: /* michael@0: * special for strings to get/set char* strings michael@0: * using PL_strdup and PR_FREEIF michael@0: */ michael@0: #define NS_METHOD_GETTER_STR(_method,_member) \ michael@0: _method(char* *aString) \ michael@0: { \ michael@0: if (!aString) return NS_ERROR_NULL_POINTER; \ michael@0: if (!(*aString = PL_strdup(_member))) \ michael@0: return NS_ERROR_OUT_OF_MEMORY; \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: #define NS_METHOD_SETTER_STR(_method, _member) \ michael@0: _method(const char *aString) \ michael@0: { \ michael@0: if (_member) PR_Free(_member); \ michael@0: if (!aString) \ michael@0: _member = nullptr; \ michael@0: else if (!(_member = PL_strdup(aString))) \ michael@0: return NS_ERROR_OUT_OF_MEMORY; \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: /* Getter/Setter macros. michael@0: Usage: michael@0: NS_IMPL_[CLASS_]GETTER[_](method, [type,] member); michael@0: NS_IMPL_[CLASS_]SETTER[_](method, [type,] member); michael@0: NS_IMPL_[CLASS_]GETSET[_]([class, ]postfix, [type,] member); michael@0: michael@0: where: michael@0: CLASS_ - implementation is inside a class definition michael@0: (otherwise the class name is needed) michael@0: Do NOT use in publicly exported header files, because michael@0: the implementation may be included many times over. michael@0: Instead, use the non-CLASS_ version. michael@0: _ - For more complex (STR, IFACE) data types michael@0: (otherwise the simple data type is needed) michael@0: method - name of the method, such as GetWidth or SetColor michael@0: type - simple data type if required michael@0: member - class member variable such as m_width or mColor michael@0: class - the class name, such as Window or MyObject michael@0: postfix - Method part after Get/Set such as "Width" for "GetWidth" michael@0: michael@0: Example: michael@0: class Window { michael@0: public: michael@0: NS_IMPL_CLASS_GETSET(Width, int, m_width); michael@0: NS_IMPL_CLASS_GETTER_STR(GetColor, m_color); michael@0: NS_IMETHOD SetColor(char *color); michael@0: michael@0: private: michael@0: int m_width; // read/write michael@0: char *m_color; // readonly michael@0: }; michael@0: michael@0: // defined outside of class michael@0: NS_IMPL_SETTER_STR(Window::GetColor, m_color); michael@0: michael@0: Questions/Comments to alecf@netscape.com michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Getter/Setter implementation within a class definition michael@0: */ michael@0: michael@0: /* simple data types */ michael@0: #define NS_IMPL_CLASS_GETTER(_method, _type, _member) \ michael@0: NS_IMETHOD NS_METHOD_GETTER(_method, _type, _member) michael@0: michael@0: #define NS_IMPL_CLASS_SETTER(_method, _type, _member) \ michael@0: NS_IMETHOD NS_METHOD_SETTER(_method, _type, _member) michael@0: michael@0: #define NS_IMPL_CLASS_GETSET(_postfix, _type, _member) \ michael@0: NS_IMPL_CLASS_GETTER(Get##_postfix, _type, _member) \ michael@0: NS_IMPL_CLASS_SETTER(Set##_postfix, _type, _member) michael@0: michael@0: /* strings */ michael@0: #define NS_IMPL_CLASS_GETTER_STR(_method, _member) \ michael@0: NS_IMETHOD NS_METHOD_GETTER_STR(_method, _member) michael@0: michael@0: #define NS_IMPL_CLASS_SETTER_STR(_method, _member) \ michael@0: NS_IMETHOD NS_METHOD_SETTER_STR(_method, _member) michael@0: michael@0: #define NS_IMPL_CLASS_GETSET_STR(_postfix, _member) \ michael@0: NS_IMPL_CLASS_GETTER_STR(Get##_postfix, _member) \ michael@0: NS_IMPL_CLASS_SETTER_STR(Set##_postfix, _member) michael@0: michael@0: /* Getter/Setter implementation outside of a class definition */ michael@0: michael@0: /* simple data types */ michael@0: #define NS_IMPL_GETTER(_method, _type, _member) \ michael@0: NS_IMETHODIMP NS_METHOD_GETTER(_method, _type, _member) michael@0: michael@0: #define NS_IMPL_SETTER(_method, _type, _member) \ michael@0: NS_IMETHODIMP NS_METHOD_SETTER(_method, _type, _member) michael@0: michael@0: #define NS_IMPL_GETSET(_class, _postfix, _type, _member) \ michael@0: NS_IMPL_GETTER(_class::Get##_postfix, _type, _member) \ michael@0: NS_IMPL_SETTER(_class::Set##_postfix, _type, _member) michael@0: michael@0: /* strings */ michael@0: #define NS_IMPL_GETTER_STR(_method, _member) \ michael@0: NS_IMETHODIMP NS_METHOD_GETTER_STR(_method, _member) michael@0: michael@0: #define NS_IMPL_SETTER_STR(_method, _member) \ michael@0: NS_IMETHODIMP NS_METHOD_SETTER_STR(_method, _member) michael@0: michael@0: #define NS_IMPL_GETSET_STR(_class, _postfix, _member) \ michael@0: NS_IMPL_GETTER_STR(_class::Get##_postfix, _member) \ michael@0: NS_IMPL_SETTER_STR(_class::Set##_postfix, _member) michael@0: michael@0: /** michael@0: * IID for the nsIsThreadsafe interface michael@0: * {88210890-47a6-11d2-bec3-00805f8a66dc} michael@0: * michael@0: * This interface is *only* used for debugging purposes to determine if michael@0: * a given component is threadsafe. michael@0: */ michael@0: #define NS_ISTHREADSAFE_IID \ michael@0: { 0x88210890, 0x47a6, 0x11d2, \ michael@0: {0xbe, 0xc3, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} } michael@0: michael@0: #define NS_LOCK_INSTANCE() \ michael@0: PR_CEnterMonitor((void*)this) michael@0: #define NS_UNLOCK_INSTANCE() \ michael@0: PR_CExitMonitor((void*)this) michael@0: michael@0: /** michael@0: * This implements query interface with two assumptions: First, the michael@0: * class in question implements nsISupports and its own interface and michael@0: * nothing else. Second, the implementation of the class's primary michael@0: * inheritance chain leads to its own interface. michael@0: * michael@0: * @param _class The name of the class implementing the method michael@0: * @param _classiiddef The name of the #define symbol that defines the IID michael@0: * for the class (e.g. NS_ISUPPORTS_IID) michael@0: */ michael@0: #if defined(DEBUG) michael@0: #define NS_VERIFY_THREADSAFE_INTERFACE(_iface) \ michael@0: if (nullptr != (_iface)) { \ michael@0: nsISupports* tmp; \ michael@0: static NS_DEFINE_IID(kIsThreadsafeIID, NS_ISTHREADSAFE_IID); \ michael@0: NS_PRECONDITION((NS_OK == _iface->QueryInterface(kIsThreadsafeIID, \ michael@0: (void**)&tmp)), \ michael@0: "Interface is not threadsafe"); \ michael@0: } michael@0: #else michael@0: #define NS_VERIFY_THREADSAFE_INTERFACE(_iface) michael@0: #endif michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: michael@0: michael@0: #endif