sbVariantUtils.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_VARIANT_UTILS_H__
28 #define __SB_VARIANT_UTILS_H__
29 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Variant utility defs.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Variant utility imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Mozilla imports.
50 #include <nsComponentManagerUtils.h>
51 #include <nsIVariant.h>
52 #include <sbStringUtils.h>
53 
54 
55 //------------------------------------------------------------------------------
56 //
57 // Variant utility classes.
58 //
59 //------------------------------------------------------------------------------
60 
81 {
82 public:
83 
84  //
85  // Variant constructors.
86  //
87 
89  {
90  nsresult rv;
91  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
92  if (NS_SUCCEEDED(rv))
93  rv = mVariant->SetAsVoid();
94  if (NS_FAILED(rv)) {
95  NS_WARNING("Failed to create new variant.");
96  mVariant = nsnull;
97  }
98  }
99 
101  {
102  nsresult rv;
103  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
104  if (NS_SUCCEEDED(rv)) {
105  if (aValue)
106  rv = mVariant->SetAsISupports(aValue);
107  else
108  rv = mVariant->SetAsEmpty();
109  }
110  if (NS_FAILED(rv)) {
111  NS_WARNING("Failed to create new variant.");
112  mVariant = nsnull;
113  }
114  }
115 
116  sbNewVariant(REFNSIID aIID, nsISupports* aValue)
117  {
118  nsresult rv;
119  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
120  if (NS_SUCCEEDED(rv)) {
121  if (aValue)
122  rv = mVariant->SetAsInterface(aIID, aValue);
123  else
124  rv = mVariant->SetAsEmpty();
125  }
126  if (NS_FAILED(rv)) {
127  NS_WARNING("Failed to create new variant.");
128  mVariant = nsnull;
129  }
130  }
131 
132  sbNewVariant(const nsAString& aValue)
133  {
134  nsresult rv;
135  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
136  if (NS_SUCCEEDED(rv))
137  rv = mVariant->SetAsAString(aValue);
138  if (NS_FAILED(rv)) {
139  NS_WARNING("Failed to create new variant.");
140  mVariant = nsnull;
141  }
142  }
143 
144  sbNewVariant(const nsACString& aValue,
145  PRUint16 aType = nsIDataType::VTYPE_CSTRING)
146  {
147  nsresult rv;
148  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
149  if (NS_SUCCEEDED(rv)) {
150  if (aType == nsIDataType::VTYPE_UTF8STRING)
151  rv = mVariant->SetAsAUTF8String(aValue);
152  else
153  rv = mVariant->SetAsACString(aValue);
154  }
155  if (NS_FAILED(rv)) {
156  NS_WARNING("Failed to create new variant.");
157  mVariant = nsnull;
158  }
159  }
160 
161  sbNewVariant(const char* aValue)
162  {
163  nsresult rv;
164  nsString value;
165  if (aValue)
166  value.AssignLiteral(aValue);
167  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
168  if (NS_SUCCEEDED(rv)) {
169  if (aValue)
170  rv = mVariant->SetAsAString(value);
171  else
172  rv = mVariant->SetAsEmpty();
173  }
174  if (NS_FAILED(rv)) {
175  NS_WARNING("Failed to create new variant.");
176  mVariant = nsnull;
177  }
178  }
179 
181  {
182  nsresult rv;
183  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
184  if (NS_SUCCEEDED(rv))
185  rv = mVariant->SetAsChar(aValue);
186  if (NS_FAILED(rv)) {
187  NS_WARNING("Failed to create new variant.");
188  mVariant = nsnull;
189  }
190  }
191 
193  {
194  nsresult rv;
195  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
196  if (NS_SUCCEEDED(rv))
197  rv = mVariant->SetAsInt16(aValue);
198  if (NS_FAILED(rv)) {
199  NS_WARNING("Failed to create new variant.");
200  mVariant = nsnull;
201  }
202  }
203 
205  {
206  nsresult rv;
207  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
208  if (NS_SUCCEEDED(rv))
209  rv = mVariant->SetAsInt32(aValue);
210  if (NS_FAILED(rv)) {
211  NS_WARNING("Failed to create new variant.");
212  mVariant = nsnull;
213  }
214  }
215 
217  PRUint16 aType = nsIDataType::VTYPE_UINT32)
218  {
219  nsresult rv;
220  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
221  if (NS_SUCCEEDED(rv)) {
222  if (aType == nsIDataType::VTYPE_BOOL)
223  rv = mVariant->SetAsBool(aValue);
224  else
225  rv = mVariant->SetAsUint32(aValue);
226  }
227  if (NS_FAILED(rv)) {
228  NS_WARNING("Failed to create new variant.");
229  mVariant = nsnull;
230  }
231  }
232 
234  {
235  nsresult rv;
236  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
237  if (NS_SUCCEEDED(rv))
238  rv = mVariant->SetAsInt64(aValue);
239  if (NS_FAILED(rv)) {
240  NS_WARNING("Failed to create new variant.");
241  mVariant = nsnull;
242  }
243  }
244 
246  {
247  nsresult rv;
248  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
249  if (NS_SUCCEEDED(rv))
250  rv = mVariant->SetAsUint64(aValue);
251  if (NS_FAILED(rv)) {
252  NS_WARNING("Failed to create new variant.");
253  mVariant = nsnull;
254  }
255  }
256 
258  {
259  nsresult rv;
260  mVariant = do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
261  if (NS_SUCCEEDED(rv))
262  rv = mVariant->SetAsDouble(aValue);
263  if (NS_FAILED(rv)) {
264  NS_WARNING("Failed to create new variant.");
265  mVariant = nsnull;
266  }
267  }
268 
269 
274  nsIVariant* get() const { return mVariant; }
275 
276  operator nsIVariant*() const { return get(); }
277 
278 
279 private:
280  nsCOMPtr<nsIWritableVariant> mVariant;
281 };
282 
283 
290 {
291  //----------------------------------------------------------------------------
292  //
293  // Public interface.
294  //
295  //----------------------------------------------------------------------------
296 
297 public:
298 
306  sbVariantHelper(nsIVariant* aVariant,
307  nsresult* aRV = nsnull) :
308  mVariant(aVariant),
309  mRV(aRV),
310  mInternalRV(NS_OK)
311  {
312  NS_ASSERTION(mVariant, "Null variant");
313  if (!mRV)
314  mRV = &mInternalRV;
315  }
316 
317 
318  //
319  // Operator methods for returning the variant value.
320  //
321 
322  operator PRUint8() const
323  {
324  // Ensure variant is present.
325  if (!mVariant) {
326  NS_WARNING("Null variant");
327  *mRV = NS_ERROR_NULL_POINTER;
328  return 0;
329  }
330 
331  // Get the variant value.
332  PRUint8 value;
333  *mRV = mVariant->GetAsUint8(&value);
334  NS_ENSURE_SUCCESS(*mRV, 0);
335 
336  return value;
337  }
338 
339  // XXXeps nsIVaraint.getAsInt8 returns PRUint8
340 
341  operator PRUint16() const
342  {
343  // Ensure variant is present.
344  if (!mVariant) {
345  NS_WARNING("Null variant");
346  *mRV = NS_ERROR_NULL_POINTER;
347  return 0;
348  }
349 
350  // Get the variant value.
351  PRUint16 value;
352  *mRV = mVariant->GetAsUint16(&value);
353  NS_ENSURE_SUCCESS(*mRV, 0);
354 
355  return value;
356  }
357 
358  operator PRInt16() const
359  {
360  // Ensure variant is present.
361  if (!mVariant) {
362  NS_WARNING("Null variant");
363  *mRV = NS_ERROR_NULL_POINTER;
364  return 0;
365  }
366 
367  // Get the variant value.
368  PRInt16 value;
369  *mRV = mVariant->GetAsInt16(&value);
370  NS_ENSURE_SUCCESS(*mRV, 0);
371 
372  return value;
373  }
374 
375  operator PRUint32() const
376  {
377  // Ensure variant is present.
378  if (!mVariant) {
379  NS_WARNING("Null variant");
380  *mRV = NS_ERROR_NULL_POINTER;
381  return 0;
382  }
383 
384  // Get the variant value.
385  PRUint32 value;
386  *mRV = mVariant->GetAsUint32(&value);
387  NS_ENSURE_SUCCESS(*mRV, 0);
388 
389  return value;
390  }
391 
392  operator PRInt32() const
393  {
394  // Ensure variant is present.
395  if (!mVariant) {
396  NS_WARNING("Null variant");
397  *mRV = NS_ERROR_NULL_POINTER;
398  return 0;
399  }
400 
401  // Get the variant value.
402  PRInt32 value;
403  *mRV = mVariant->GetAsInt32(&value);
404  NS_ENSURE_SUCCESS(*mRV, 0);
405 
406  return value;
407  }
408 
409  operator PRUint64() const
410  {
411  // Ensure variant is present.
412  if (!mVariant) {
413  NS_WARNING("Null variant");
414  *mRV = NS_ERROR_NULL_POINTER;
415  return 0;
416  }
417 
418  // Get the variant value.
419  PRUint64 value;
420  *mRV = mVariant->GetAsUint64(&value);
421  NS_ENSURE_SUCCESS(*mRV, 0);
422 
423  return value;
424  }
425 
426  operator PRInt64() const
427  {
428  // Ensure variant is present.
429  if (!mVariant) {
430  NS_WARNING("Null variant");
431  *mRV = NS_ERROR_NULL_POINTER;
432  return 0;
433  }
434 
435  // Get the variant value.
436  PRInt64 value;
437  *mRV = mVariant->GetAsInt64(&value);
438  NS_ENSURE_SUCCESS(*mRV, 0);
439 
440  return value;
441  }
442 
443  operator nsString() const
444  {
445  // Ensure variant is present.
446  if (!mVariant) {
447  NS_WARNING("Null variant");
448  *mRV = NS_ERROR_NULL_POINTER;
449  return SBVoidString();
450  }
451 
452  // Get the variant value.
453  nsAutoString value;
454  *mRV = mVariant->GetAsAString(value);
455  NS_ENSURE_SUCCESS(*mRV, SBVoidString());
456 
457  return value;
458  }
459 
460  operator nsCString() const
461  {
462  // Ensure variant is present.
463  if (!mVariant) {
464  NS_WARNING("Null variant");
465  *mRV = NS_ERROR_NULL_POINTER;
466  return SBVoidCString();
467  }
468 
469  // Get the variant value.
470  nsCAutoString value;
471  *mRV = mVariant->GetAsACString(value);
472  NS_ENSURE_SUCCESS(*mRV, SBVoidCString());
473 
474  return value;
475  }
476 
477 
483  nsresult rv()
484  {
485  return *mRV;
486  }
487 
488 
489  //----------------------------------------------------------------------------
490  //
491  // Private interface.
492  //
493  //----------------------------------------------------------------------------
494 
495 private:
496 
497  //
498  // mVariant Base variant.
499  // mRV Pointer to return value.
500  // mInternalRV Internal return value storage.
501  //
502 
503  nsCOMPtr<nsIVariant> mVariant;
504  nsresult* mRV;
505  nsresult mInternalRV;
506 };
507 
508 
509 #endif /* __SB_VARIANT_UTILS_H__ */
510 
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
sbNewVariant(PRInt16 aValue)
sbNewVariant(nsISupports *aValue)
sbNewVariant(char aValue)
sbNewVariant(double aValue)
sbNewVariant(REFNSIID aIID, nsISupports *aValue)
sbNewVariant(const char *aValue)
sbNewVariant(const nsACString &aValue, PRUint16 aType=nsIDataType::VTYPE_CSTRING)
sbNewVariant(PRUint32 aValue, PRUint16 aType=nsIDataType::VTYPE_UINT32)
sbNewVariant(PRUint64 aValue)
sbNewVariant(PRInt32 aValue)
countRef value
Definition: FeedWriter.js:1423
sbVariantHelper(nsIVariant *aVariant, nsresult *aRV=nsnull)
sbNewVariant(const nsAString &aValue)
sbNewVariant(PRInt64 aValue)