Scoped_ptr source code

Source: Internet
Author: User
/* E: \ Program Files \ codegear \ rad studio \ 7.0 \ include \ boost_000035 \ boost/checked_delete.hpp 24 :*/
Namespace boost
{
Template <class T> inline void checked_delete (T * X)
{
Typedef char type_must_be_complete [sizeof (t )? 1:-1];
(Void) sizeof (type_must_be_complete );
Delete X;
}

Template <class T> inline void checked_array_delete (T * X)
{
Typedef char type_must_be_complete [sizeof (t )? 1:-1];
(Void) sizeof (type_must_be_complete );
Delete [] X;
}

Template <class T> struct checked_deleter
{
Typedef void result_type;
Typedef T * argument_type;

Void operator () (T * X) const
{
Boost: checked_delete (X );
}
};

Template <class T> struct checked_array_deleter
{
Typedef void result_type;
Typedef T * argument_type;

Void operator () (T * X) const
{
Boost: checked_array_delete (X );
}
};
}

/* E: \ Program Files \ codegear \ rad studio \ 7.0 \ include \ boost_000035 \ boost/scoped_ptr.hpp 23 :*/
Namespace boost
{
Template <class T> class scoped_ptr
{
PRIVATE:
T * PTR;

Scoped_ptr (scoped_ptr const &);
Scoped_ptr & operator = (scoped_ptr const &);

Typedef scoped_ptr <t> this_type;

Public:
Typedef t element_type;

Explicit scoped_ptr (T * p = 0): PTR (P)
{
}
/* Constructor, storageP. Note,PMust be usedOperator newAllocated or null. During construction,
Not RequiredTMust be a complete type. When pointerPIs the result of calling an allocation function, rather than calling it directly.NewWhen
Useful: Because this type does not need to be complete, only the type is required.TA pre-declaration. This constructor will not throw
Exception. */

Explicit scoped_ptr (STD: auto_ptr <t> P): PTR (P. Release ())
{
}
/* Take control from an auto_ptr and point to the object indicated by the auto_ptr pointer, And the auto_prt pointer no longer points to this object */

~ Scoped_ptr ()
{
Boost: checked_delete (PTR );
}
/* Delete the specified object. TypeTIt must be a complete type when it is destroyed. IfScoped_ptrNo
It occupies resources and does nothing. This destructor does not throw an exception. */

Void reset (T * p = 0)
{
(P = 0 | P! = PTR )? (Void) 0: _ assert ("P = 0 | P! = PTR "," e :\\ Program Files \ codegear \ rad studio \ 7.0 \ include \ boost_000035 \ boost/scoped_ptr.hpp ", 82 ));
This_type (P). Swap (* This );
}
/* Reset oneScoped_ptrIs to delete the saved pointer, if it has, and save againP. Generally, resource survival
Management should be completelyScoped_ptrHandle it by yourself, but in rare cases, resources needScoped_ptrOr
ByScoped_ptrTo process another resource other than its original resource. In this case, you can useResetBut use it as little as possible.
(Using it too much usually indicates a design problem.) This function does not throw an exception. */

T & operator * () const
{
(PTR! = 0 )? (Void) 0: _ assert ("PTR! = 0 "," e :\\ Program Files \ codegear \ rad studio \ 7.0 \ include \ boost_000035 \ boost/scoped_ptr.hpp ", 88 ));
Return * PTR;
}
/* Returns a reference to the object pointed to by the saved pointer. Because null references are not allowed, a null pointer is obtained.
OfScoped_ptrWill lead to undefined behavior. If you are not sure whether the pointer is valid, use the FunctionGetSubstitute for unreference.
This function does not throw an exception. */

T * operator-> () const
{
(PTR! = 0 )? (Void) 0: _ assert ("PTR! = 0 "," e :\\ Program Files \ codegear \ rad studio \ 7.0 \ include \ boost_000035 \ boost/scoped_ptr.hpp ", 94 ));
Return PTR;
}
/* Return the saved pointer. If the pointer to be saved is null, calling this function will lead to undefined behavior. If you cannot determine whether the pointer is
Null. It is best to use functions.Get. This function does not throw an exception. */

T * Get () const
{
Return PTR;
}
/* Return the saved pointer. Be careful when usingGetBecause it can directly operate the bare pointer. However,GetSo that you can test the saved
Whether the pointer is null. This function does not throw an exception.GetIt is usually used to call functions that require bare pointers. */

Typedef T * this_type: * unspecified_bool_type;

Operator unspecified_bool_type () const
{
Return PTR = 0? 0: & this_type: PTR;
}
/* ReturnScoped_ptrWhether it is not empty. The type of the returned value is unspecified, but this type can be used in the context of Boolean.
It is best to use this type conversion function in the IF statement, insteadGetTest nowScoped_ptrValidity */

Bool Operator! () Const
{
Return PTR = 0;
}

Void swap (scoped_ptr & B)
{
T * TMP = B. PTR;
B. PTR = PTR;
PTR = TMP;
}
/* Exchange twoScoped_ptr. This function does not throw an exception. */
};

Template <class T> inline void swap (scoped_ptr <t> & A, scoped_ptr <t> & B)
{
A. Swap (B );
}
/* Functions provide a better way to swap the content of two scoped pointer. It is better because
  Swap (scoped1, scoped2)It can be widely used in many pointer types, including bare pointers and third-party smart pointers.
  [2]Scoped1.swap (scoped2)It can only be used for its definition, but not for bare pointers. */

Template <class T> inline T * get_pointer (scoped_ptr <t> const & P)
{
Return P. Get ();
}
}

Usage:

Scoped_ptrThe usage is no different from that of a common pointer. The biggest difference is that you do not have to remember to callDeleteAnd replication is not allowed. Typical pointer operations (Operator *AndOperator->), And provides the same syntax as the bare pointer. UseScoped_ptrIt is as fast as using bare pointers, and there is no increase in size, so they can be widely used. UseBoost: scoped_ptrContains header files"Boost/scoped_ptr.hpp". DeclareScoped_ptrTo specify the parameters of the class template. For exampleSTD: StringPointerScoped_ptr:

boost::scoped_ptr<std::string> p(new std::string("Hello"));

WhenScoped_ptrWhen it is destroyed, it calls the pointer it owns.Delete.

Differences:

Scoped_ptrAndAuto_ptrThe main difference between them is the handling of ownership.Auto_ptrThe SourceAuto_ptrAutomatically hand over ownership, andScoped_ptrReplication is not allowed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.