GL Studio C++ Runtime API
scoped_ptr.h
Go to the documentation of this file.
1/// \file scoped_ptr.h
2/// \brief A smart pointer with unique ownership -- poor man's std::unique_ptr.
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2017 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<br>
9/// USA<br>
10/// <br>
11/// All rights reserved.<br>
12///
13/// This Software contains proprietary trade secrets of DiSTI and may not be
14/// reproduced, in whole or part, in any form, or by any means of electronic,
15/// mechanical, or otherwise, without the written permission of DiSTI. Said
16/// permission may be derived through the purchase of applicable DiSTI product
17/// licenses which detail the distribution rights of this content and any
18/// Derivative Works based on this or other copyrighted DiSTI Software.
19///
20/// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21/// AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22/// PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23/// AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24/// IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25/// PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26///
27/// LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28/// IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29/// INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30/// DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31/// INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32/// INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33/// OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34/// EXCEED FIVE DOLLARS (US$5.00).
35///
36/// The aforementioned terms and restrictions are governed by the laws of the
37/// State of Florida and the United States of America.
38
39#ifndef INCLUDED_DISTI_SCOPED_PTR_H
40#define INCLUDED_DISTI_SCOPED_PTR_H
41
42#include "disti_assert.h"
44
45namespace disti
46{
47/** The ScopedPtr class template stores a pointer to a dynamically allocated object.
48 * The object pointed to is guaranteed to be deleted, either on destruction of the
49 * ScopedPtr or via an explicit Reset.
50 */
51template<class T>
53{
54public:
55 /// Constructor
56 /// \param p The pointer to wrap with this object.
57 explicit ScopedPtr( T* p = NULL )
58 : _p( p )
59 {
60 }
61
63 {
64 delete _p;
65 _p = NULL;
66 }
67
68#if defined( DISTI_HAS_RVAL_REFS )
69 /// Move constructor
70 /// \param that The object to move from.
72 {
73 *this = std::move( that );
74 }
75
76 /// Move assignment operator
77 /// \param that The object to move from.
78 /// \return The resulting object (this).
80 {
81 // Pilfer that's pointer
82 Reset( that._p );
83 that._p = NULL;
84 return *this;
85 }
86#endif
87 /// Address operator
88 /// \return The address of the underlying pointer.
89 T& operator*() const
90 {
91 GLS_ASSERT( _p );
92 return *_p;
93 }
94
95 /// Dereference operator
96 /// \return The underlying pointer.
97 T* operator->() const
98 {
99 GLS_ASSERT( _p );
100 return _p;
101 }
102
103 /// Reset the object by deleting the old object, and assigning a new one.
104 /// \param p The new underlying pointer to assign.
105 void Reset( T* p = NULL )
106 {
107 delete _p;
108 _p = p;
109 }
110
111 /// \return True if the underlying pointer isn't NULL.
112 bool IsNull() const
113 {
114 return NULL == _p;
115 }
116
117 /// \return The underlying pointer.
118 T* Get() const { return _p; }
119
120private:
121 T* _p; ///< The pointer wrapped by this object.
122
125};
126} // namespace disti
127#endif
Definition: scoped_ptr.h:53
T & operator*() const
Definition: scoped_ptr.h:89
void Reset(T *p=NULL)
Definition: scoped_ptr.h:105
ScopedPtr & operator=(ScopedPtr &&that)
Definition: scoped_ptr.h:79
T * operator->() const
Definition: scoped_ptr.h:97
T * Get() const
Definition: scoped_ptr.h:118
bool IsNull() const
Definition: scoped_ptr.h:112
ScopedPtr(T *p=NULL)
Definition: scoped_ptr.h:57
ScopedPtr(ScopedPtr &&that)
Definition: scoped_ptr.h:71
Contains the DistiAssert macro.
#define GLS_ASSERT(exp)
Definition: disti_assert.h:149
Macros and helper code to determine what subset of C++11/14/17 is available.
#define DISTI_SPECIAL_MEM_FUN_DELETE
Macro to wrap function deletion, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:245
#define DISTI_FUNC_NOEXCEPT
Macro to wrap the noexcept keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:239
Definition: bmpimage.h:46