VirtualBox

Changeset 26753 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Feb 24, 2010 4:24:33 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
58004
Message:

Main: Bstr makeover (third attempt) -- make Bstr(NULL) and Bstr() behave the same; resulting cleanup; make some more internal methods use Utf8Str instead of Bstr; fix a lot of CheckComArgNotNull??() usage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/ministring.h

    r26603 r26753  
    4545 * else except IPRT memory management functions.  Semantics are like in
    4646 * std::string, except it can do a lot less.
     47 *
     48 *
     49 * Note that MiniString does not differentiate between NULL strings and
     50 * empty strings. In other words, MiniString("") and MiniString(NULL)
     51 * behave the same. In both cases, MiniString allocates no memory, reports
     52 * a zero length and zero allocated bytes for both, and returns an empty
     53 * C string from c_str().
    4754 */
    4855#ifdef VBOX
     
    7178     * Creates a copy of another MiniString.
    7279     *
    73      * This allocates s.length() + 1 bytes for the new instance.
     80     * This allocates s.length() + 1 bytes for the new instance, unless s is empty.
    7481     *
    7582     * @param   s               The source string.
     
    8390
    8491    /**
    85      * Creates a copy of another MiniString.
    86      *
    87      * This allocates strlen(pcsz) + 1 bytes for the new instance.
     92     * Creates a copy of a C string.
     93     *
     94     * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty.
    8895     *
    8996     * @param   pcsz            The source string.
     
    123130     *
    124131     * Returns the number of bytes allocated in the internal string buffer, which is
    125      * at least length() + 1 if length() > 0.
     132     * at least length() + 1 if length() > 0; for an empty string, this returns 0.
    126133     *
    127134     * @returns m_cbAllocated.
     
    172179
    173180    /**
     181     * Assigns a copy of pcsz to "this".
     182     *
     183     * @param   pcsz            The source string.
     184     *
     185     * @throws  std::bad_alloc  On allocation failure.  The object is left describing
     186     *             a NULL string.
     187     *
     188     * @returns Reference to the object.
     189     */
     190    MiniString &operator=(const char *pcsz)
     191    {
     192        if (m_psz != pcsz)
     193        {
     194            cleanup();
     195            copyFrom(pcsz);
     196        }
     197        return *this;
     198    }
     199
     200    /**
     201     * Assigns a copy of s to "this".
     202     *
     203     * @param   s               The source string.
     204     *
     205     * @throws  std::bad_alloc  On allocation failure.  The object is left describing
     206     *             a NULL string.
     207     *
     208     * @returns Reference to the object.
     209     */
     210    MiniString &operator=(const MiniString &s)
     211    {
     212        if (this != &s)
     213        {
     214            cleanup();
     215            copyFrom(s);
     216        }
     217        return *this;
     218    }
     219
     220    /**
     221     * Appends the string "that" to "this".
     222     *
     223     * @param   that            The string to append.
     224     *
     225     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     226     *
     227     * @returns Reference to the object.
     228     */
     229    MiniString &append(const MiniString &that);
     230
     231    /**
     232     * Appends the given character to "this".
     233     *
     234     * @param   c               The character to append.
     235     *
     236     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     237     *
     238     * @returns Reference to the object.
     239     */
     240    MiniString &append(char c);
     241
     242    /**
     243     * Index operator.
     244     *
     245     * Returns the byte at the given index, or a null byte if the index is not
     246     * smaller than length().  This does _not_ count codepoints but simply points
     247     * into the member C string.
     248     *
     249     * @param   i       The index into the string buffer.
     250     * @returns char at the index or null.
     251     */
     252    inline char operator[](size_t i) const
     253    {
     254        if (i < length())
     255            return m_psz[i];
     256        return '\0';
     257    }
     258
     259    /**
     260     * Returns the contained string as a C-style const char* pointer.
     261     * This never returns NULL; if the string is empty, this returns a
     262     * pointer to static null byte.
     263     *
     264     * @returns const pointer to C-style string.
     265     */
     266    inline const char *c_str() const
     267    {
     268        return (m_psz) ? m_psz : "";
     269    }
     270
     271    /**
     272     * Like c_str(), for compatibility with lots of VirtualBox Main code.
     273     *
     274     * @returns const pointer to C-style string.
     275     */
     276    inline const char *raw() const
     277    {
     278        return (m_psz) ? m_psz : "";
     279    }
     280
     281    /**
    174282     * Returns a non-const raw pointer that allows to modify the string directly.
     283     * As opposed to c_str() and raw(), this DOES return NULL for an empty string
     284     * because we cannot return a non-const pointer to a static "" global.
    175285     *
    176286     * @warning
     
    209319
    210320    /**
    211      * Assigns a copy of pcsz to "this".
    212      *
    213      * @param   pcsz            The source string.
    214      *
    215      * @throws  std::bad_alloc  On allocation failure.  The object is left describing
    216      *             a NULL string.
    217      *
    218      * @returns Reference to the object.
    219      */
    220     MiniString &operator=(const char *pcsz)
    221     {
    222         if (m_psz != pcsz)
    223         {
    224             cleanup();
    225             copyFrom(pcsz);
    226         }
    227         return *this;
    228     }
    229 
    230     /**
    231      * Assigns a copy of s to "this".
    232      *
    233      * @param   s               The source string.
    234      *
    235      * @throws  std::bad_alloc  On allocation failure.  The object is left describing
    236      *             a NULL string.
    237      *
    238      * @returns Reference to the object.
    239      */
    240     MiniString &operator=(const MiniString &s)
    241     {
    242         if (this != &s)
    243         {
    244             cleanup();
    245             copyFrom(s);
    246         }
    247         return *this;
    248     }
    249 
    250     /**
    251      * Appends the string "that" to "this".
    252      *
    253      * @param   that            The string to append.
    254      *
    255      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    256      *
    257      * @returns Reference to the object.
    258      */
    259     MiniString &append(const MiniString &that);
    260 
    261     /**
    262      * Appends the given character to "this".
    263      *
    264      * @param   c               The character to append.
    265      *
    266      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    267      *
    268      * @returns Reference to the object.
    269      */
    270     MiniString &append(char c);
    271 
    272     /**
    273      * Index operator.
    274      *
    275      * Returns the byte at the given index, or a null byte if the index is not
    276      * smaller than length().  This does _not_ count codepoints but simply points
    277      * into the member C string.
    278      *
    279      * @param   i       The index into the string buffer.
    280      * @returns char at the index or null.
    281      */
    282     inline char operator[](size_t i) const
    283     {
    284         if (i < length())
    285             return m_psz[i];
    286         return '\0';
    287     }
    288 
    289     /**
    290      * Returns the contained string as a C-style const char* pointer.
    291      *
    292      * @returns const pointer to C-style string.
    293      */
    294     inline const char *c_str() const
    295     {
    296         return m_psz;
    297     }
    298 
    299     /**
    300      * Like c_str(), for compatibility with lots of VirtualBox Main code.
    301      *
    302      * @returns const pointer to C-style string.
    303      */
    304     inline const char *raw() const
    305     {
    306         return m_psz;
    307     }
    308 
    309     /**
    310      * Emptry string or not?
    311      *
    312      * Returns true if the member string has no length.  This states nothing about
    313      * how much memory might be allocated.
     321     * Returns true if the member string has no length.
     322     * This is true for instances created from both NULL and "" input strings.
     323     *
     324     * This states nothing about how much memory might be allocated.
    314325     *
    315326     * @returns true if empty, false if not.
     
    474485     * Hide operator bool() to force people to use isEmpty() explicitly.
    475486     */
    476     operator bool() const { return false; }
     487    operator bool() const;
    477488
    478489    /**
     
    492503
    493504    /**
    494      * Protected internal helper for copy a string that completely ignors the
    495      * current object state.
     505     * Protected internal helper to copy a string. This ignores the previous object
     506     * state, so either call this from a constructor or call cleanup() first.
    496507     *
    497508     * copyFrom() unconditionally sets the members to a copy of the given other
     
    501512     *
    502513     * This variant copies from another MiniString and is fast since
    503      * the length of source string is known.
     514     * the length of the source string is known.
    504515     *
    505516     * @param   s               The source string.
     
    533544
    534545    /**
    535      * Protected internal helper for copy a string that completely ignors the
    536      * current object state.
     546     * Protected internal helper to copy a string. This ignores the previous object
     547     * state, so either call this from a constructor or call cleanup() first.
    537548     *
    538549     * See copyFrom() above.
     
    548559    void copyFrom(const char *pcsz)
    549560    {
    550         if (pcsz)
     561        if (pcsz && *pcsz)
    551562        {
    552563            m_cbLength = strlen(pcsz);
Note: See TracChangeset for help on using the changeset viewer.

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