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  explicit ScopedPtr( T* p = NULL )
57  : _p( p )
58  {
59  }
60 
61  ~ScopedPtr()
62  {
63  delete _p;
64  _p = NULL;
65  }
66 
67 #if defined( DISTI_HAS_RVAL_REFS )
68  ScopedPtr( ScopedPtr&& that )
69  {
70  *this = std::move( that );
71  }
72 
73  ScopedPtr& operator=( ScopedPtr&& that )
74  {
75  // Pilfer that's pointer
76  Reset( that._p );
77  that._p = NULL;
78  return *this;
79  }
80 #endif
81 
82  T& operator*() const
83  {
84  GLS_ASSERT( _p );
85  return *_p;
86  }
87 
88  T* operator->() const
89  {
90  GLS_ASSERT( _p );
91  return _p;
92  }
93 
94  void Reset( T* p = NULL )
95  {
96  delete _p;
97  _p = p;
98  }
99 
100  bool IsNull() const
101  {
102  return NULL == _p;
103  }
104 
105  T* Get() const { return _p; }
106 
107 private:
108  T* _p;
109 
110  ScopedPtr( const ScopedPtr& ) DISTI_SPECIAL_MEM_FUN_DELETE;
111  void operator=( const ScopedPtr& ) DISTI_SPECIAL_MEM_FUN_DELETE;
112 };
113 } // namespace disti
114 #endif
Definition: scoped_ptr.h:53
Contains the DistiAssert macro.
Macros and helper code to determine what subset of C++11/14/17 is available.
Definition: bmpimage.h:46
#define GLS_ASSERT(exp)
Definition: disti_assert.h:135