VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/components/nsIServiceManagerUtils.h@ 62330

Last change on this file since 62330 was 51167, checked in by vboxsync, 11 years ago

libs/xpcom: Add dummy constructor, destructors for working around Solaris GCC, Sun linker issues.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is XPCOM.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef nsIServiceManagerUtils_h__
39#define nsIServiceManagerUtils_h__
40
41#include "nsIServiceManager.h"
42#include "nsIServiceManagerObsolete.h"
43#include "nsCOMPtr.h"
44
45////////////////////////////////////////////////////////////////////////////
46// Using servicemanager with COMPtrs
47class NS_COM nsGetServiceByCID : public nsCOMPtr_helper
48{
49 public:
50 nsGetServiceByCID( const nsCID& aCID, nsISupports* aServiceManager, nsresult* aErrorPtr )
51 : mCID(aCID),
52 mServiceManager( do_QueryInterface(aServiceManager) ),
53 mErrorPtr(aErrorPtr)
54 {
55 // nothing else to do
56 }
57
58 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
59
60 private:
61 const nsCID& mCID;
62 nsCOMPtr<nsIServiceManager> mServiceManager;
63 nsresult* mErrorPtr;
64};
65
66inline
67const nsGetServiceByCID
68do_GetService( const nsCID& aCID, nsresult* error = 0 )
69{
70 return nsGetServiceByCID(aCID, 0, error);
71}
72
73inline
74const nsGetServiceByCID
75do_GetService( const nsCID& aCID, nsISupports* aServiceManager, nsresult* error = 0 )
76{
77 return nsGetServiceByCID(aCID, aServiceManager, error);
78}
79
80class NS_COM nsGetServiceByContractID : public nsCOMPtr_helper
81{
82 public:
83 nsGetServiceByContractID( const char* aContractID, nsISupports* aServiceManager, nsresult* aErrorPtr )
84 : mContractID(aContractID),
85 mServiceManager( do_QueryInterface(aServiceManager) ),
86 mErrorPtr(aErrorPtr)
87 {
88 // nothing else to do
89 }
90 // Implement a dummy destructor to workaround linking issue on Solaris gcc 4.8.2 (see @bugref{5838})
91 ~nsGetServiceByContractID() {}
92
93 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
94
95 private:
96 const char* mContractID;
97 nsCOMPtr<nsIServiceManager> mServiceManager;
98 nsresult* mErrorPtr;
99};
100
101inline
102const nsGetServiceByContractID
103do_GetService( const char* aContractID, nsresult* error = 0 )
104{
105 return nsGetServiceByContractID(aContractID, 0, error);
106}
107
108inline
109const nsGetServiceByContractID
110do_GetService( const char* aContractID, nsISupports* aServiceManager, nsresult* error = 0 )
111{
112 return nsGetServiceByContractID(aContractID, aServiceManager, error);
113}
114
115class nsGetServiceFromCategory : public nsCOMPtr_helper
116{
117 public:
118 nsGetServiceFromCategory(const char* aCategory, const char* aEntry,
119 nsISupports* aServiceManager,
120 nsresult* aErrorPtr)
121 : mCategory(aCategory),
122 mEntry(aEntry),
123 mServiceManager( do_QueryInterface(aServiceManager) ),
124 mErrorPtr(aErrorPtr)
125 {
126 // nothing else to do
127 }
128
129 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
130 protected:
131 const char* mCategory;
132 const char* mEntry;
133 nsCOMPtr<nsIServiceManager> mServiceManager;
134 nsresult* mErrorPtr;
135};
136
137inline
138const nsGetServiceFromCategory
139do_GetServiceFromCategory( const char* category, const char* entry,
140 nsresult* error = 0)
141{
142 return nsGetServiceFromCategory(category, entry, 0, error);
143}
144
145// type-safe shortcuts for calling |GetService|
146template <class DestinationType>
147inline
148nsresult
149CallGetService( const nsCID &aClass,
150 DestinationType** aDestination)
151{
152 NS_PRECONDITION(aDestination, "null parameter");
153
154 nsCOMPtr<nsIServiceManager> mgr;
155 nsresult rv = NS_GetServiceManager(getter_AddRefs(mgr));
156
157 if (NS_FAILED(rv))
158 return rv;
159
160 return mgr->GetService(aClass,
161 NS_GET_IID(DestinationType),
162 NS_REINTERPRET_CAST(void**, aDestination));
163}
164
165template <class DestinationType>
166inline
167nsresult
168CallGetService( const char *aContractID,
169 DestinationType** aDestination)
170{
171 NS_PRECONDITION(aContractID, "null parameter");
172 NS_PRECONDITION(aDestination, "null parameter");
173
174 nsCOMPtr<nsIServiceManager> mgr;
175 nsresult rv = NS_GetServiceManager(getter_AddRefs(mgr));
176
177 if (NS_FAILED(rv))
178 return rv;
179
180 return mgr->GetServiceByContractID(aContractID,
181 NS_GET_IID(DestinationType),
182 NS_REINTERPRET_CAST(void**, aDestination));
183}
184
185#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette