VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/glue/nsServiceManagerUtils.h@ 6542

Last change on this file since 6542 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 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
91 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
92
93 private:
94 const char* mContractID;
95 nsCOMPtr<nsIServiceManager> mServiceManager;
96 nsresult* mErrorPtr;
97};
98
99inline
100const nsGetServiceByContractID
101do_GetService( const char* aContractID, nsresult* error = 0 )
102{
103 return nsGetServiceByContractID(aContractID, 0, error);
104}
105
106inline
107const nsGetServiceByContractID
108do_GetService( const char* aContractID, nsISupports* aServiceManager, nsresult* error = 0 )
109{
110 return nsGetServiceByContractID(aContractID, aServiceManager, error);
111}
112
113class nsGetServiceFromCategory : public nsCOMPtr_helper
114{
115 public:
116 nsGetServiceFromCategory(const char* aCategory, const char* aEntry,
117 nsISupports* aServiceManager,
118 nsresult* aErrorPtr)
119 : mCategory(aCategory),
120 mEntry(aEntry),
121 mServiceManager( do_QueryInterface(aServiceManager) ),
122 mErrorPtr(aErrorPtr)
123 {
124 // nothing else to do
125 }
126
127 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
128 protected:
129 const char* mCategory;
130 const char* mEntry;
131 nsCOMPtr<nsIServiceManager> mServiceManager;
132 nsresult* mErrorPtr;
133};
134
135inline
136const nsGetServiceFromCategory
137do_GetServiceFromCategory( const char* category, const char* entry,
138 nsresult* error = 0)
139{
140 return nsGetServiceFromCategory(category, entry, 0, error);
141}
142
143// type-safe shortcuts for calling |GetService|
144template <class DestinationType>
145inline
146nsresult
147CallGetService( const nsCID &aClass,
148 DestinationType** aDestination)
149{
150 NS_PRECONDITION(aDestination, "null parameter");
151
152 nsCOMPtr<nsIServiceManager> mgr;
153 nsresult rv = NS_GetServiceManager(getter_AddRefs(mgr));
154
155 if (NS_FAILED(rv))
156 return rv;
157
158 return mgr->GetService(aClass,
159 NS_GET_IID(DestinationType),
160 NS_REINTERPRET_CAST(void**, aDestination));
161}
162
163template <class DestinationType>
164inline
165nsresult
166CallGetService( const char *aContractID,
167 DestinationType** aDestination)
168{
169 NS_PRECONDITION(aContractID, "null parameter");
170 NS_PRECONDITION(aDestination, "null parameter");
171
172 nsCOMPtr<nsIServiceManager> mgr;
173 nsresult rv = NS_GetServiceManager(getter_AddRefs(mgr));
174
175 if (NS_FAILED(rv))
176 return rv;
177
178 return mgr->GetServiceByContractID(aContractID,
179 NS_GET_IID(DestinationType),
180 NS_REINTERPRET_CAST(void**, aDestination));
181}
182
183#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