sbPropertyBagUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef SB_PROPERTY_BAG_UTILS_H__
28 #define SB_PROPERTY_BAG_UTILS_H__
29 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Property bag utilities defs.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Property bag utilities imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Songbird imports.
50 #include <sbVariantUtils.h>
51 
52 // Mozilla imports.
53 #include <nsIWritablePropertyBag.h>
54 #include <nsIWritablePropertyBag2.h>
55 
56 
57 //------------------------------------------------------------------------------
58 //
59 // Property bag helper classes.
60 //
61 //------------------------------------------------------------------------------
62 
69 // Forward declarations.
71 
73 {
74  //----------------------------------------------------------------------------
75  //
76  // Public interface.
77  //
78  //----------------------------------------------------------------------------
79 
80 public:
81 
91  sbPropertyHelper(nsIPropertyBag* aPropertyBag,
92  const nsAString& aKey,
93  nsresult* aRV = nsnull) :
94  mPropertyBag(aPropertyBag),
95  mKey(aKey),
96  mRV(aRV),
97  mInternalRV(NS_OK)
98  {
99  // Use internal return value if none specified.
100  if (!mRV)
101  mRV = &mInternalRV;
102  }
103 
104 
111  explicit sbPropertyHelper(nsresult* aRV = nsnull) :
112  mRV(aRV),
113  mInternalRV(NS_OK)
114  {
115  // Use internal return value if none specified.
116  if (!mRV)
117  mRV = &mInternalRV;
118  }
119 
120 
126  template<class T>
127  operator T() const
128  {
129  // Validate state.
130  NS_ENSURE_TRUE(mPropertyBag, T());
131 
132  // Get the value as a variant.
133  nsCOMPtr<nsIVariant> value;
134  *mRV = mPropertyBag->GetProperty(mKey, getter_AddRefs(value));
135  NS_ENSURE_SUCCESS(*mRV, T());
136 
137  // Return the value.
138  return sbVariantHelper(value, mRV);
139  }
140 
141 
149  template <class T>
151  {
152  // Validate state.
153  NS_ENSURE_TRUE(mPropertyBag, *this);
154 
155  // Get a writable property bag.
156  nsCOMPtr<nsIWritablePropertyBag>
157  propertyBag = do_QueryInterface(mPropertyBag, mRV);
158  NS_ENSURE_SUCCESS(*mRV, *this);
159 
160  // Set the property value.
161  *mRV = propertyBag->SetProperty(mKey, sbNewVariant(aValue));
162  NS_ENSURE_SUCCESS(*mRV, *this);
163 
164  return *this;
165  }
166 
167 
176  {
177  // Validate state.
178  NS_ENSURE_TRUE(mPropertyBag, *this);
179 
180  // Get a writable property bag.
181  nsCOMPtr<nsIWritablePropertyBag>
182  propertyBag = do_QueryInterface(mPropertyBag, mRV);
183  NS_ENSURE_SUCCESS(*mRV, *this);
184 
185  // Set the property value.
186  *mRV = propertyBag->SetProperty(mKey, aValue);
187  NS_ENSURE_SUCCESS(*mRV, *this);
188 
189  return *this;
190  }
191 
192 
201  sbPropertyHelper operator[](const char* aKey)
202  {
203  // Validate state.
204  NS_ENSURE_TRUE(mPropertyBag, sbPropertyHelper(mRV));
205 
206  // Get the property bag.
207  nsCOMPtr<nsIPropertyBag2>
208  propertyBag = do_QueryInterface(mPropertyBag, mRV);
209  NS_ENSURE_SUCCESS(*mRV, sbPropertyHelper(mRV));
210 
211  // Get the value property bag.
212  nsCOMPtr<nsIPropertyBag> valuePropertyBag;
213  *mRV = propertyBag->GetPropertyAsInterface(mKey,
214  NS_GET_IID(nsIPropertyBag),
215  getter_AddRefs(valuePropertyBag));
216  NS_ENSURE_SUCCESS(*mRV, sbPropertyHelper(mRV));
217 
218  // Return the value.
219  return sbPropertyHelper(valuePropertyBag, sbAutoString(aKey), mRV);
220  }
221 
227  nsresult rv()
228  {
229  return *mRV;
230  }
231 
232 
233  //----------------------------------------------------------------------------
234  //
235  // Protected interface.
236  //
237  //----------------------------------------------------------------------------
238 
239 private:
240 
241  //
242  // mPropertyBag Bag containing property.
243  // mKey Property key.
244  // mRV Pointer to return value.
245  // mInternalRV Internal return value storage.
246  //
247 
248  nsCOMPtr<nsIPropertyBag> mPropertyBag;
249  nsString mKey;
250  nsresult* mRV;
251  nsresult mInternalRV;
252 };
253 
254 
262 {
263  //----------------------------------------------------------------------------
264  //
265  // Public interface.
266  //
267  //----------------------------------------------------------------------------
268 
269 public:
270 
271  // Forward nsIPropertyBag methods.
272  NS_FORWARD_SAFE_NSIPROPERTYBAG(mPropertyBag)
273  NS_FORWARD_SAFE_NSIPROPERTYBAG2(mPropertyBag2)
274  NS_FORWARD_SAFE_NSIWRITABLEPROPERTYBAG(mWritablePropertyBag)
275  NS_FORWARD_SAFE_NSIWRITABLEPROPERTYBAG2(mWritablePropertyBag2)
276 
277 
278 
284  sbPropertyBagHelper(nsresult* aRV = nsnull) :
285  mRV(aRV),
286  mInternalRV(NS_OK)
287  {
288  // Use internal return value if none specified.
289  if (!mRV)
290  mRV = &mInternalRV;
291 
292  // Create property bag.
293  mPropertyBag =
294  do_CreateInstance("@songbirdnest.com/moz/xpcom/sbpropertybag;1", mRV);
295  NS_ENSURE_SUCCESS(*mRV, /* void */);
296  mPropertyBag2 = do_QueryInterface(mPropertyBag);
297  mWritablePropertyBag = do_QueryInterface(mPropertyBag);
298  mWritablePropertyBag2 = do_QueryInterface(mPropertyBag);
299  }
300 
301 
311  nsresult* aRV = nsnull) :
312  mRV(aRV),
313  mInternalRV(NS_OK)
314  {
315  // Use internal return value if none specified.
316  if (!mRV)
317  mRV = &mInternalRV;
318 
319  // Get the property bag.
320  mPropertyBag = do_QueryInterface(aPropertyBag, mRV);
321  NS_ENSURE_SUCCESS(*mRV, /* void */);
322  mPropertyBag2 = do_QueryInterface(aPropertyBag);
323  mWritablePropertyBag = do_QueryInterface(mPropertyBag);
324  mWritablePropertyBag2 = do_QueryInterface(mPropertyBag);
325  }
326 
327 
331  nsIPropertyBag* GetBag()
332  {
333  return mPropertyBag;
334  }
335 
336  operator nsIPropertyBag*() const
337  {
338  return mPropertyBag;
339  }
340 
341  operator nsIPropertyBag2*() const
342  {
343  return mPropertyBag2;
344  }
345 
346  operator nsIWritablePropertyBag*() const
347  {
348  return mWritablePropertyBag;
349  }
350 
351  operator nsIWritablePropertyBag2*() const
352  {
353  return mWritablePropertyBag2;
354  }
355 
356 
364  sbPropertyHelper Get(const char* aKey) const
365  {
366  return sbPropertyHelper(mPropertyBag, sbAutoString(aKey), mRV);
367  }
368 
369 
377  sbPropertyHelper operator[](const char* aKey) const
378  {
379  return Get(aKey);
380  }
381 
382  sbPropertyHelper operator[](const nsACString & aKey) const
383  {
384  return Get(aKey.BeginReading());
385  }
394  {
395  return this;
396  }
397 
399  {
400  return *this;
401  }
402 
403 
411  template <class T>
412  nsresult Set(char* aKey,
413  T aValue)
414  {
415  // Validate state and arguments.
416  NS_ENSURE_STATE(mWritablePropertyBag);
417  NS_ENSURE_ARG_POINTER(aKey);
418 
419  // Set the property.
420  *mRV = mWritablePropertyBag->SetProperty(sbAutoString(aKey),
421  sbNewVariant(aValue));
422  NS_ENSURE_SUCCESS(*mRV, *mRV);
423 
424  return NS_OK;
425  }
426 
427 
435  PRBool HasKey(const char* aKey)
436  {
437  // Validate state.
438  NS_ENSURE_TRUE(mPropertyBag2, PR_FALSE);
439 
440  // Check for key.
441  PRBool hasKey;
442  *mRV = mPropertyBag2->HasKey(sbAutoString(aKey), &hasKey);
443  NS_ENSURE_SUCCESS(*mRV, PR_FALSE);
444 
445  return hasKey;
446  }
447 
448 
454  nsresult rv()
455  {
456  return *mRV;
457  }
458 
459 
460  //----------------------------------------------------------------------------
461  //
462  // Private interface.
463  //
464  //----------------------------------------------------------------------------
465 
466 private:
467 
468  //
469  // mPropertyBag Bag containing property.
470  // mPropertyBag2
471  // mWritablePropertyBag
472  // mWritablePropertyBag2
473  // mRV Pointer to return value.
474  // mInternalRV Internal return value storage.
475  //
476 
477  nsCOMPtr<nsIPropertyBag> mPropertyBag;
478  nsCOMPtr<nsIPropertyBag2> mPropertyBag2;
479  nsCOMPtr<nsIWritablePropertyBag>
480  mWritablePropertyBag;
481  nsCOMPtr<nsIWritablePropertyBag2>
482  mWritablePropertyBag2;
483  nsresult* mRV;
484  nsresult mInternalRV;
485 };
486 
487 
488 #endif /* SB_PROPERTY_BAG_UTILS_H__ */
489 
sbPropertyHelper & operator=(T aValue)
sbPropertyHelper operator[](const char *aKey) const
return NS_OK
sbPropertyBagHelper(nsISupports *aPropertyBag, nsresult *aRV=nsnull)
onPageChanged aValue
Definition: FeedWriter.js:1395
sbPropertyBagHelper & operator*()
sbPropertyHelper(nsIPropertyBag *aPropertyBag, const nsAString &aKey, nsresult *aRV=nsnull)
Songbird Variant Utility Definitions.
sbPropertyHelper & operator=(nsIVariant *aValue)
sbPropertyHelper operator[](const char *aKey)
sbPropertyBagHelper * operator->()
nsresult Set(char *aKey, T aValue)
sbPropertyHelper(nsresult *aRV=nsnull)
PRBool HasKey(const char *aKey)
countRef value
Definition: FeedWriter.js:1423
sbPropertyHelper Get(const char *aKey) const
sbPropertyHelper operator[](const nsACString &aKey) const
nsIPropertyBag * GetBag()