| 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
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*/
40#ifndef INCLUDED_DISTI_SCOPED_PTR_H
41#define INCLUDED_DISTI_SCOPED_PTR_H
42
43#include "disti_assert.h"
44#include "gls_cpp_lang_support.h"
45
46 namespace 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 */
52 template<class T>
53 class ScopedPtr
54{
55 public:
56 /// Constructor
57 /// \param p The pointer to wrap with this object.
58 explicit ScopedPtr( T* p = NULL )
59 : _p( p )
60 {
61 }
62
63 ~ScopedPtr()
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.
72 ScopedPtr( ScopedPtr&& that )
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).
80 ScopedPtr& operator=( ScopedPtr&& that )
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
121 private:
122 T* _p; ///< The pointer wrapped by this object.
123
124 ScopedPtr( const ScopedPtr& ) DISTI_SPECIAL_MEM_FUN_DELETE;
125 void operator=( const ScopedPtr& ) DISTI_SPECIAL_MEM_FUN_DELETE;
126};
127} // namespace disti
128#endif
disti::ScopedPtr
Definition: scoped_ptr.h:54
disti::ScopedPtr::operator*
T & operator*() const
Definition: scoped_ptr.h:90
disti::ScopedPtr::Reset
void Reset(T *p=NULL)
Definition: scoped_ptr.h:106
disti::ScopedPtr::operator=
ScopedPtr & operator=(ScopedPtr &&that)
Definition: scoped_ptr.h:80
disti::ScopedPtr::operator->
T * operator->() const
Definition: scoped_ptr.h:98
disti::ScopedPtr::Get
T * Get() const
Definition: scoped_ptr.h:119
disti::ScopedPtr::IsNull
bool IsNull() const
Definition: scoped_ptr.h:113
disti::ScopedPtr::ScopedPtr
ScopedPtr(T *p=NULL)
Definition: scoped_ptr.h:58
disti::ScopedPtr::ScopedPtr
ScopedPtr(ScopedPtr &&that)
Definition: scoped_ptr.h:72
disti_assert.h
Contains the DistiAssert macro.
GLS_ASSERT
#define GLS_ASSERT(exp)
Definition: disti_assert.h:150
gls_cpp_lang_support.h
Macros and helper code to determine what subset of C++11/14/17 is available.
DISTI_SPECIAL_MEM_FUN_DELETE
#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:246
disti
Definition: bmpimage.h:47