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