VirtualBox

Changeset 10925 in vbox


Ignore:
Timestamp:
Jul 29, 2008 12:11:56 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
33773
Message:

Runtime: updated RTAutoRes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/autores

    r10918 r10925  
    2525
    2626/**
    27  * Wrapper class around RTAutoRes to provide reference semantics.  Certain
    28  * operations like constructing from AutoRes objects passed by value are only
    29  * possible if the object "transitions through" a wrapper struct.
    30  * See auto_ptr in the C++ <memory> header.
     27 * Wrapper class around RTAutoRes to provide reference semantics.  This is
     28 * needed to construct AutoRes objects from constant references to other
     29 * AutoRes objects (such as temporary objects returned from functions) which
     30 * can not be modified due to their const modifier, but are not actually
     31 * constant objects.  See a discussion of auto_ptr and auto_ptr_ref for more
     32 * details.
    3133 */
    3234template <class T, void Destruct(T)>
     
    3840};
    3941
    40 /** Re-casting template to convert a void fn(void *) to a void fn(T) */
    41 template <class T, void fn(void *)>
    42 inline void RTAutoResRecastVoid(T aT) { fn(aT); }
    43 
    4442/**
    4543 * An auto pointer-type class for resources which take a C-style destructor
    4644 * destructor (free() or equivalent).
    4745 */
    48 template <class T, void Destruct(T) = RTAutoResRecastVoid<T, RTMemFree> >
     46template <class T, void Destruct(T)>
    4947class RTAutoRes
    5048{
     
    5452public:
    5553    /** Constructor */
    56     RTAutoRes(T aValue = NULL) { mValue = aValue; }
     54    RTAutoRes(T aValue = 0) { mValue = aValue; }
     55
    5756    /** Destructor */
    58     ~RTAutoRes() { if (NULL != mValue) Destruct(mValue); }
    59 
    60     /** release method to get the pointer's value and "reset" the pointer. */
    61     T release(void) { T aTmp = mValue; mValue = NULL; return aTmp; }
    62 
    63     /** reset the pointer value to zero or to another pointer. */
    64     void reset(T aValue = NULL) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } }
    65 
    66     /**
    67      * Reallocate the resource value.  Free the old value if allocation fails.
    68      * @returns true if the new allocation succeeds, false otherwise
    69      * @param   Reallocator  the function to be used for reallocating the
    70      *                       resource.  It should have equivalent semantics to
    71      *                       C realloc.
    72      * @note We can overload this member for other reallocator signatures as
    73      *       needed.
    74      */
    75     template <void *Reallocator(void *, size_t)>
    76     bool realloc(size_t cchNewSize)
    77     {
    78         T aNewValue = reinterpret_cast<T>(Reallocator(mValue, cchNewSize));
    79         if (aNewValue != NULL)
    80             mValue = aNewValue;
    81         else
    82             Destruct(mValue);
    83         return (aNewValue != NULL);
    84     }
     57    ~RTAutoRes() { if (mValue != 0) Destruct(mValue); }
    8558
    8659    /** Copy constructor */
     
    8962    /** Copy from equivalent class */
    9063    template <class T1>
    91     RTAutoRes(RTAutoRes<T1, Destruct> &orig) { mValue = orig.release(); }
     64    RTAutoRes(RTAutoRes<T1, Destruct> &orig)
     65    { mValue = orig.release(); }
     66
     67    /** Convert a reference structure into an AutoRes pointer. */
     68    RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; }
    9269
    9370    /** Assignment operator. */
     
    10986    RTAutoRes& operator=(T aValue)
    11087    {
    111         if (NULL != mValue)
     88        if (mValue != 0)
    11289        {
    11390            Destruct(mValue);
     
    11693        return *this;
    11794    }
    118 
    119 #if 0
    120     /**
    121      * @todo I'm not sure how to express this now that our type is T and not
    122      * T*.  Something like return type *T, which would only be valid for T a
    123      * pointer type.  Or perhaps easiest to just leave it out.
    124      */
    125 
    126     /** Dereference with * operator. */
    127     T &operator*() { return *mValue; }
    128 #endif
    129 
    130     /** Dereference with -> operator.  Only makes sense for pointer values. */
    131     T operator->() { return mValue; }
    132 
    133     /** Accessing the value inside. */
    134     T get(void) { return mValue; }
    135 
    136     /** Convert a reference structure into an AutoRes pointer. */
    137     RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; }
    13895
    13996    /** Assign from a reference structure into an AutoRes pointer. */
     
    148105    }
    149106
     107    /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */
     108    template <class T1>
     109    operator RTAutoRes<T1, Destruct>()
     110    { return RTAutoRes<T1, Destruct>(release()); }
     111
    150112    /** Typecast an AutoRes pointer to a reference structure. */
    151113    template <class T1>
    152     operator RTAutoResRef<T1, Destruct>() { return RTAutoResRef<T1, Destruct>(release()); }
     114    operator RTAutoResRef<T1, Destruct>()
     115    { return RTAutoResRef<T1, Destruct>(release()); }
    153116
    154     /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */
     117    /** release method to get the pointer's value and "reset" the pointer. */
     118    T release(void) { T aTmp = mValue; mValue = 0; return aTmp; }
     119
     120    /** reset the pointer value to zero or to another pointer. */
     121    void reset(T aValue = 0) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } }
     122
     123    /** Accessing the value inside. */
     124    T get(void) { return mValue; }
     125};
     126
     127/** Re-casting template to convert a void fn(void *) to a void fn(T *) */
     128template <class T, void fn(void *)>
     129inline void RTAutoResRecastVoid(T *aValue) { fn(aValue); }
     130
     131template <class T, void Destruct(T *) = RTAutoResRecastVoid <T, RTMemFree> >
     132class RTAutoResPtr : public RTAutoRes <T *, Destruct>
     133{
     134public:
     135    /** Constructor */
     136    RTAutoResPtr(T *aValue = NULL) : RTAutoRes <T *, Destruct>(aValue) {}
     137
     138    /** Copy constructors */
     139    RTAutoResPtr(RTAutoResPtr &orig) : RTAutoRes <T *, Destruct>(orig) {}
    155140    template <class T1>
    156     operator RTAutoRes<T1, Destruct>() { return RTAutoRes<T1, Destruct>(release()); }
     141    RTAutoResPtr(RTAutoResPtr<T1, Destruct> &orig) : RTAutoRes<T1 *, Destruct>(orig) {}
     142
     143    /** Assignment operators */
     144    RTAutoResPtr& operator=(RTAutoResPtr &orig)
     145    { this->RTAutoRes <T *, Destruct>::operator=(orig); }
     146    template <class T1>
     147    RTAutoResPtr& operator=(RTAutoResPtr<T1, Destruct> &orig)
     148    { this->RTAutoRes<T1 *, Destruct>::operator=(orig); return *this; }
     149    RTAutoResPtr& operator=(T *aValue)
     150    { this->RTAutoRes <T *, Destruct>::operator=(aValue); return *this; }
     151    RTAutoResPtr& operator=(RTAutoResRef<T *, Destruct> ref)
     152    { this->RTAutoRes <T *, Destruct>::operator=(ref); return *this; }
     153
     154    /** Dereference with * operator. */
     155    T &operator*() { return *this->get(); }
     156
     157    /** Dereference with -> operator. */
     158    T* operator->() { return this->get(); }
     159
     160    /**
     161     * Reallocate the resource value.  Free the old value if allocation fails.
     162     * @returns true if the new allocation succeeds, false otherwise
     163     * @param   Reallocator  the function to be used for reallocating the
     164     *                       resource.  It should have equivalent semantics to
     165     *                       C realloc.
     166     * @note We can overload this member for other reallocator signatures as
     167     *       needed.
     168     */
     169    template <void *Reallocator(void *, size_t)>
     170    bool realloc(size_t cchNewSize)
     171    {
     172        T *aNewValue = reinterpret_cast<T *>(Reallocator(this->release(), cchNewSize));
     173        if (aNewValue != NULL)  /* This line is technically not necessary */
     174            this->reset(aNewValue);
     175        return (aNewValue != NULL);
     176    }
    157177};
    158178
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