VirtualBox

Changeset 4134

Show
Ignore:
Timestamp:
08/13/07 18:35:46 (1 year ago)
Author:
vboxsync
Message:

Main: Add Utf8StrFmtVA to resolve the dangerous ambiguity of ellipsis and va_list.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/VBox/com/string.h

    r4129 r4134  
    507507     * 
    508508     *  @param format   printf-like format string (in UTF-8 encoding) 
    509      *  @param args     list of arguments for the format string 
    510      */ 
    511     explicit Utf8StrFmt (const char *format, va_list args) { init (format, args); } 
     509     *  @param ...      list of the arguments for the format string 
     510     */ 
     511    explicit Utf8StrFmt (const char *format, ...) 
     512    { 
     513        va_list args; 
     514        va_start (args, format); 
     515        init (format, args); 
     516        va_end (args); 
     517    } 
     518 
     519protected: 
     520 
     521    Utf8StrFmt() {} 
     522 
     523    void init (const char *format, va_list args); 
     524 
     525private: 
     526 
     527    static DECLCALLBACK(size_t) strOutput (void *pvArg, const char *pachChars, 
     528                                           size_t cbChars); 
     529}; 
     530 
     531/** 
     532 *  This class is a vprintf-like formatter for Utf8Str strings. It is 
     533 *  identical to Utf8StrFmt except that its constructor takes a va_list 
     534 *  argument instead of ellipsis. 
     535 * 
     536 *  Note that a separate class is necessary because va_list is defined as 
     537 *  |char *| on most platforms. For this reason, if we had two overloaded 
     538 *  constructors in Utf8StrFmt (one taking ellipsis and another one taking 
     539 *  va_list) then composing a constructor call using exactly two |char *| 
     540 *  arguments would cause the compiler to use the va_list overload instead of 
     541 *  the ellipsis one which is obviously wrong. The compiler would choose 
     542 *  va_list because ellipsis has the lowest rank when it comes to resolving 
     543 *  overloads, as opposed to va_list which is an exact match for |char *|. 
     544 */ 
     545class Utf8StrFmtVA : public Utf8StrFmt 
     546
     547public: 
    512548 
    513549    /** 
     
    516552     * 
    517553     *  @param format   printf-like format string (in UTF-8 encoding) 
    518      *  @param ...      list of the arguments for the format string 
    519      * 
    520      *  @note  Be extremely careful when passing exactly one argument in the 
    521      *         ellipsis. If this is a string the C++ could decide to use the 
    522      *         other constructor since va_list is defined as char * on some 
    523      *         platforms. If unsure, add an extra dummy argument. 
    524      */ 
    525     explicit Utf8StrFmt (const char *format, ...) 
    526     { 
    527         va_list args; 
    528         va_start (args, format); 
    529         init (format, args); 
    530         va_end (args); 
    531     } 
    532  
    533 private: 
    534  
    535     void init (const char *format, va_list args); 
    536  
    537     static DECLCALLBACK(size_t) strOutput (void *pvArg, const char *pachChars, 
    538                                            size_t cbChars); 
     554     *  @param args     list of arguments for the format string 
     555     */ 
     556    Utf8StrFmtVA (const char *format, va_list args) { init (format, args); } 
    539557}; 
    540558 
     
    542560 
    543561#endif 
    544  
  • trunk/src/VBox/Main/ConsoleImpl.cpp

    r4131 r4134  
    67436743    AssertReturnVoid (that); 
    67446744 
    6745     Utf8Str message = Utf8StrFmt (pszFormat, args); 
     6745    Utf8Str message = Utf8StrFmtVA (pszFormat, args); 
    67466746 
    67476747    LogRel (("Console: VM runtime error: fatal=%RTbool, " 
  • trunk/src/VBox/Main/ProgressImpl.cpp

    r4071 r4134  
    839839    va_list args; 
    840840    va_start (args, aText); 
    841     Bstr text = Utf8StrFmt (aText, args); 
     841    Bstr text = Utf8StrFmtVA (aText, args); 
    842842    va_end (args); 
    843843     
  • trunk/src/VBox/Main/include/VirtualBoxBase.h

    r4071 r4134  
    10641064    { 
    10651065        return setErrorInternal (aResultCode, aIID, aComponent, 
    1066                                  Utf8StrFmt (aText, aArgs), 
     1066                                 Utf8StrFmtVA (aText, aArgs), 
    10671067                                 false /* aPreserve */); 
    10681068    } 
     
    10731073    { 
    10741074        return setErrorInternal (aResultCode, aIID, aComponent, 
    1075                                  Utf8StrFmt (aText, aArgs), 
     1075                                 Utf8StrFmtVA (aText, aArgs), 
    10761076                                 true /* aPreserve */); 
    10771077    } 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy