VirtualBox

Changeset 32718 in vbox for trunk/include/VBox


Ignore:
Timestamp:
Sep 23, 2010 12:57:52 PM (14 years ago)
Author:
vboxsync
Message:

com/string: Remove bool conversion operator and other convenience error operators. They are hiding programming errors (like incorrect empty string checks, and in one case a free of the wrong pointer).

Location:
trunk/include/VBox/com
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/ErrorInfo.h

    r30683 r32718  
    55
    66/*
    7  * Copyright (C) 2006-2007 Oracle Corporation
     7 * Copyright (C) 2006-2010 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    5555 *      ...
    5656 *      HRESULT rc = foo->SomeMethod();
    57  *      if (FAILED (rc)) {
    58  *          ErrorInfo info (foo);
     57 *      if (FAILED(rc)) {
     58 *          ErrorInfo info(foo);
    5959 *          if (info.isFullAvailable()) {
    60  *              printf ("error message = %ls\n", info.getText().raw());
     60 *              printf("error message = %ls\n", info.getText().raw());
    6161 *          }
    6262 *      }
     
    227227     *
    228228     *  This method returns a non-null IID only if the instance was created
    229      *  using #template <class I> ErrorInfo (I *i) or
    230      *  template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
     229     *  using #template <class I> ErrorInfo(I *i) or
     230     *  template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
    231231     */
    232232    const Guid& getCalleeIID() const
     
    239239     *
    240240     *  This method returns a non-null name only if the instance was created
    241      *  using #template <class I> ErrorInfo (I *i) or
    242      *  template <class I> ErrorInfo (const ComPtr <I> &i) constructor.
     241     *  using #template <class I> ErrorInfo(I *i) or
     242     *  template <class I> ErrorInfo(const ComPtr<I> &i) constructor.
    243243     */
    244244    const Bstr& getCalleeName() const
     
    248248
    249249    /**
    250      *  Resets all collected error information. #isNull() will
    251      *  return @c true after this method is called.
     250     *  Resets all collected error information. #isBasicAvailable() and
     251     *  #isFullAvailable will return @c true after this method is called.
    252252     */
    253253    void setNull()
     
    327327 *  <code>
    328328 *      rc = foo->method();
    329  *      if (FAILED (rc))
     329 *      if (FAILED(rc))
    330330 *      {
    331331 *           ErrorInfoKeeper eik;
     
    353353     *                  the instance uninitialized.
    354354     */
    355     ErrorInfoKeeper (bool aIsNull = false)
    356         : ErrorInfo (false), mForgot (aIsNull)
     355    ErrorInfoKeeper(bool aIsNull = false)
     356        : ErrorInfo(false), mForgot(aIsNull)
    357357    {
    358358        if (!aIsNull)
    359             init (true /* aKeepObj */);
     359            init(true /* aKeepObj */);
    360360    }
    361361
     
    399399     *  stored error info object to the caller.
    400400     */
    401     ComPtr <IUnknown> takeError() { mForgot = true; return mErrorInfo; }
     401    ComPtr<IUnknown> takeError() { mForgot = true; return mErrorInfo; }
    402402
    403403private:
  • trunk/include/VBox/com/string.h

    r31539 r32718  
    179179    }
    180180
    181     int compare(BSTR str) const
    182     {
    183         return compare((CBSTR)str);
     181    int compare(BSTR str, CaseSensitivity cs = CaseSensitive) const
     182    {
     183        return compare((CBSTR)str, cs);
     184    }
     185
     186    int compare(const Bstr &that, CaseSensitivity cs = CaseSensitive) const
     187    {
     188        return compare(that.m_bstr, cs);
    184189    }
    185190
     
    201206     * @note Always use this method to check if an instance is empty. Do not
    202207     * use length() because that may need to run through the entire string
    203      * (Bstr does not cache string lengths). Also do not use operator bool();
    204      * for one, MSVC is really annoying with its thinking that that is ambiguous,
    205      * and even though operator bool() is protected with Bstr, at least gcc uses
    206      * operator CBSTR() when a construct like "if (string)" is encountered, which
    207      * is always true now since it raw() never returns an empty string. Again,
    208      * always use isEmpty() even though if (string) may compile!
     208     * (Bstr does not cache string lengths).
    209209     */
    210210    bool isEmpty() const { return m_bstr == NULL || *m_bstr == 0; }
    211211
    212212    size_t length() const { return isEmpty() ? 0 : ::RTUtf16Len((PRTUTF16)m_bstr); }
    213 
    214     /**
    215      * Returns true if the member string is not empty. I'd like to make this
    216      * private but since we require operator BSTR() it's futile anyway because
    217      * the compiler will then (wrongly) use that one instead. Also if this is
    218      * private the odd WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP macro below
    219      * will fail on Windows.
    220      */
    221     operator bool() const
    222     {
    223         return m_bstr != NULL;
    224     }
    225213
    226214    /**
     
    236224        return g_bstrEmpty;
    237225    }
    238 
    239     /**
    240      * Convenience operator so that Bstr's can be passed to CBSTR input parameters
    241      * of COM methods.
    242      */
    243     operator CBSTR() const { return raw(); }
    244 
    245     /**
    246      * Convenience operator so that Bstr's can be passed to CBSTR input parameters
    247      * of COM methods. Unfortunately this is required for Windows since with
    248      * MSCOM, input BSTR parameters of interface methods are not const.
    249      */
    250     operator BSTR() { return (BSTR)raw(); }
    251226
    252227    /**
     
    397372inline bool operator!=(BSTR l, const Bstr &r) { return r.operator!=(l); }
    398373
    399 // work around error C2593 of the stupid MSVC 7.x ambiguity resolver
    400 WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Bstr)
    401374
    402375////////////////////////////////////////////////////////////////////////////////
     
    431404    Utf8Str(const Bstr &that)
    432405    {
     406        copyFrom(that.raw());
     407    }
     408
     409    Utf8Str(CBSTR that)
     410    {
    433411        copyFrom(that);
    434412    }
    435413
    436     Utf8Str(CBSTR that)
    437     {
    438         copyFrom(that);
    439     }
    440 
    441414    Utf8Str& operator=(const MiniString &that)
    442415    {
     
    454427    {
    455428        cleanup();
    456         copyFrom(that);
     429        copyFrom(that.raw());
    457430        return *this;
    458431    }
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