VirtualBox

Changeset 4134 in vbox for trunk/include/VBox/com


Ignore:
Timestamp:
Aug 13, 2007 4:35:46 PM (18 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
23613
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.

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