VirtualBox

Changeset 2012

Show
Ignore:
Timestamp:
04/10/07 16:50:19 (2 years ago)
Author:
vboxsync
Message:

Added Bstr::alloc() to pair Utf8Str() alloc.

Files:

Legend:

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

    r1962 r2012  
    6868    Bstr (const char *that); 
    6969 
    70     /** 
    71      *  Allocates memory for a new string capable to store \a aSize - 1 
    72      *  characters plus the terminating zero character. If \a aSize is zero, 
    73      *  a null object will be created. 
    74      */ 
    75     Bstr (size_t aSize) : bstr (NULL) 
     70    /** Shortcut that calls #alloc(aSize) right after object creation. */ 
     71    Bstr (size_t aSize) : bstr (NULL) { alloc (aSize); } 
     72 
     73    ~Bstr () { setNull(); } 
     74 
     75    Bstr &operator = (const Bstr &that) { safe_assign (that.bstr); return *this; } 
     76    Bstr &operator = (const BSTR that) { safe_assign (that); return *this; } 
     77 
     78    Bstr &operator = (const Utf8Str &that); 
     79    Bstr &operator = (const char *that); 
     80 
     81    Bstr &setNull() 
     82    { 
     83        if (bstr) 
     84        { 
     85            ::SysFreeString (bstr); 
     86            bstr = NULL; 
     87        } 
     88        return *this; 
     89    } 
     90 
     91    Bstr &setNullIfEmpty() 
     92    { 
     93        if (bstr && *bstr == 0) 
     94        { 
     95            ::SysFreeString (bstr); 
     96            bstr = NULL; 
     97        } 
     98        return *this; 
     99    } 
     100 
     101    /** 
     102     *  Allocates memory for a string capable to store \a aSize - 1 characters 
     103     *  plus the terminating zero character. If \a aSize is zero, or if a 
     104     *  memory allocation error occurs, this object will become null. 
     105     */ 
     106    Bstr &alloc (size_t aSize) 
    76107    { 
    77108        if (aSize) 
    78109        { 
    79             unsigned int size = (unsigned int)aSize; Assert(size == aSize); 
     110            AssertCompile (sizeof (unsigned int) >= sizeof (aSize)); 
     111            unsigned int size = (unsigned int) aSize; 
    80112            bstr = ::SysAllocStringLen (NULL, size - 1); 
    81113            if (bstr) 
    82114                bstr [0] = 0; 
    83115        } 
    84     } 
    85  
    86     ~Bstr () { setNull(); } 
    87  
    88     Bstr &operator = (const Bstr &that) { safe_assign (that.bstr); return *this; } 
    89     Bstr &operator = (const BSTR that) { safe_assign (that); return *this; } 
    90  
    91     Bstr &operator = (const Utf8Str &that); 
    92     Bstr &operator = (const char *that); 
    93  
    94     Bstr &setNull() { 
    95         if (bstr) { 
    96             ::SysFreeString (bstr); 
    97             bstr = NULL; 
    98         } 
    99         return *this; 
    100     } 
    101  
    102     Bstr &setNullIfEmpty() { 
    103         if (bstr && *bstr == 0) { 
    104             ::SysFreeString (bstr); 
    105             bstr = NULL; 
    106         } 
    107         return *this; 
    108     } 
    109  
    110     int compare (const BSTR str) const { 
     116        return *this; 
     117    } 
     118 
     119    int compare (const BSTR str) const 
     120    { 
    111121        return ::RTStrUcs2Cmp ((PRTUCS2) bstr, (PRTUCS2) str); 
    112122    } 
     
    134144    } 
    135145 
    136     int compareIgnoreCase (const BSTR str) const { 
     146    int compareIgnoreCase (const BSTR str) const 
     147    { 
    137148        return ::RTUtf16LocaleICmp (bstr, str); 
    138149    } 
     
    166177     *  the caller. 
    167178     */ 
    168     const Bstr &cloneTo (BSTR *pstr) const { 
    169         if (pstr) { 
     179    const Bstr &cloneTo (BSTR *pstr) const 
     180    { 
     181        if (pstr) 
     182        { 
    170183            *pstr = NULL; 
    171184            raw_copy (*pstr, bstr); 
     
    189202private: 
    190203 
    191     void safe_assign (const BSTR str) { 
    192         if (bstr != str) { 
     204    void safe_assign (const BSTR str) 
     205    { 
     206        if (bstr != str) 
     207        { 
    193208            setNull(); 
    194209            raw_copy (bstr, str); 
     
    196211    } 
    197212 
    198     inline static void raw_copy (BSTR &ls, const BSTR rs) { 
     213    inline static void raw_copy (BSTR &ls, const BSTR rs) 
     214    { 
    199215        if (rs) 
    200216            ls = ::SysAllocString ((const OLECHAR *) rs); 
    201217    } 
    202218 
    203     inline static void raw_copy (BSTR &ls, const char *rs) { 
    204         if (rs) { 
     219    inline static void raw_copy (BSTR &ls, const char *rs) 
     220    { 
     221        if (rs) 
     222        { 
    205223            PRTUCS2 s = NULL; 
    206224            ::RTStrUtf8ToUcs2 (&s, rs); 
     
    241259    Utf8Str (const BSTR that) : str (NULL) { raw_copy (str, that); } 
    242260 
    243     /** 
    244      *  Allocates memory for a new string capable to store \a aSize - 1 
    245      *  characters plus the terminating zero character. If \a aSize is zero, 
    246      *  a null object will be created. 
    247      */ 
    248     void alloc (size_t aSize) 
     261    /** Shortcut that calls #alloc(aSize) right after object creation. */ 
     262    Utf8Str (size_t aSize) : str (NULL) { alloc(aSize); } 
     263 
     264    virtual ~Utf8Str () { setNull(); } 
     265 
     266    Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; } 
     267    Utf8Str &operator = (const char *that) { safe_assign (that); return *this; } 
     268 
     269    Utf8Str &operator = (const Bstr &that) 
     270    { 
     271        setNull(); 
     272        raw_copy (str, that); 
     273        return *this; 
     274    } 
     275    Utf8Str &operator = (const BSTR that) 
     276    { 
     277        setNull(); 
     278        raw_copy (str, that); 
     279        return *this; 
     280    } 
     281 
     282    Utf8Str &setNull() 
     283    { 
     284        if (str) 
     285        { 
     286#if defined (__WIN__) 
     287            ::RTStrFree (str); 
     288#else 
     289            nsMemory::Free (str); 
     290#endif 
     291            str = NULL; 
     292        } 
     293        return *this; 
     294    } 
     295 
     296    Utf8Str &setNullIfEmpty() 
     297    { 
     298        if (str && *str == 0) 
     299        { 
     300#if defined (__WIN__) 
     301            ::RTStrFree (str); 
     302#else 
     303            nsMemory::Free (str); 
     304#endif 
     305            str = NULL; 
     306        } 
     307        return *this; 
     308    } 
     309 
     310    /** 
     311     *  Allocates memory for a string capable to store \a aSize - 1 characters 
     312     *  plus the terminating zero character. If \a aSize is zero, or if a 
     313     *  memory allocation error occurs, this object will become null. 
     314     */ 
     315    Utf8Str &alloc (size_t aSize) 
    249316    { 
    250317        if (aSize) 
     
    259326                str [0] = 0; 
    260327        } 
    261     } 
    262  
    263     Utf8Str (size_t aSize) : str (NULL) 
    264     { 
    265         alloc(aSize); 
    266     } 
    267  
    268     virtual ~Utf8Str () { setNull(); } 
    269  
    270     Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; } 
    271     Utf8Str &operator = (const char *that) { safe_assign (that); return *this; } 
    272  
    273     Utf8Str &operator = (const Bstr &that) { 
    274         setNull(); 
    275         raw_copy (str, that); 
    276         return *this; 
    277     } 
    278     Utf8Str &operator = (const BSTR that) { 
    279         setNull(); 
    280         raw_copy (str, that); 
    281         return *this; 
    282     } 
    283  
    284     Utf8Str &setNull() { 
    285         if (str) { 
    286 #if defined (__WIN__) 
    287             ::RTStrFree (str); 
    288 #else 
    289             nsMemory::Free (str); 
    290 #endif 
    291             str = NULL; 
    292         } 
    293         return *this; 
    294     } 
    295  
    296     Utf8Str &setNullIfEmpty() { 
    297         if (str && *str == 0) { 
    298 #if defined (__WIN__) 
    299             ::RTStrFree (str); 
    300 #else 
    301             nsMemory::Free (str); 
    302 #endif 
    303             str = NULL; 
    304         } 
    305         return *this; 
    306     } 
    307  
    308     int compare (const char *s) const { 
     328        return *this; 
     329    } 
     330 
     331    int compare (const char *s) const 
     332    { 
    309333        return str == s ? 0 : ::strcmp (str, s); 
    310334    } 
     
    345369     *  caller. 
    346370     */ 
    347     const Utf8Str &cloneTo (char **pstr) const { 
    348         if (pstr) { 
     371    const Utf8Str &cloneTo (char **pstr) const 
     372    { 
     373        if (pstr) 
     374        { 
    349375            *pstr = NULL; 
    350376            raw_copy (*pstr, str); 
     
    358384     *  caller. 
    359385     */ 
    360     const Utf8Str &cloneTo (BSTR *pstr) const { 
    361         if (pstr) { 
     386    const Utf8Str &cloneTo (BSTR *pstr) const 
     387    { 
     388        if (pstr) 
     389        { 
    362390            *pstr = NULL; 
    363391            Bstr::raw_copy (*pstr, str); 
     
    374402private: 
    375403 
    376     void safe_assign (const char *s) { 
    377         if (str != s) { 
     404    void safe_assign (const char *s) 
     405    { 
     406        if (str != s) 
     407        { 
    378408            setNull(); 
    379409            raw_copy (str, s); 
     
    381411    } 
    382412 
    383     inline static void raw_copy (char *&ls, const char *rs) { 
     413    inline static void raw_copy (char *&ls, const char *rs) 
     414    { 
    384415        if (rs) 
    385416#if defined (__WIN__) 
     
    390421    } 
    391422 
    392     inline static void raw_copy (char *&ls, const BSTR rs) { 
    393         if (rs) { 
     423    inline static void raw_copy (char *&ls, const BSTR rs) 
     424    { 
     425        if (rs) 
     426        { 
    394427#if defined (__WIN__) 
    395428            ::RTStrUcs2ToUtf8 (&ls, (PRTUCS2) rs); 
     
    423456inline Bstr::Bstr (const char *that) : bstr (NULL) { raw_copy (bstr, that); } 
    424457 
    425 inline Bstr &Bstr::operator = (const Utf8Str &that) { 
     458inline Bstr &Bstr::operator = (const Utf8Str &that) 
     459
    426460    setNull(); 
    427461    raw_copy (bstr, that); 
    428462    return *this; 
    429463} 
    430 inline Bstr &Bstr::operator = (const char *that) { 
     464inline Bstr &Bstr::operator = (const char *that) 
     465
    431466    setNull(); 
    432467    raw_copy (bstr, that); 
     
    434469} 
    435470 
    436 inline const Bstr &Bstr::cloneTo (char **pstr) const { 
     471inline const Bstr &Bstr::cloneTo (char **pstr) const 
     472
    437473    if (pstr) { 
    438474        *pstr = NULL; 
     
    470506     *         platforms. If unsure, add an extra dummy argument. 
    471507     */ 
    472     explicit Utf8StrFmt (const char *format, ...) { 
     508    explicit Utf8StrFmt (const char *format, ...) 
     509    { 
    473510        va_list args; 
    474511        va_start (args, format); 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy