OptiX  3.9
NVIDIA OptiX Acceleration Engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Handle.h
1 
2 /*
3  * Copyright (c) 2013 NVIDIA Corporation. All rights reserved.
4  *
5  * NVIDIA Corporation and its licensors retain all intellectual property and proprietary
6  * rights in and to this software, related documentation and any modifications thereto.
7  * Any use, reproduction, disclosure or distribution of this software and related
8  * documentation without an express license agreement from NVIDIA Corporation is strictly
9  * prohibited.
10  *
11  * TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS*
12  * AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
13  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14  * PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY
15  * SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT
16  * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF
17  * BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
18  * INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF
19  * SUCH DAMAGES
20  */
21 
22 #ifndef __optix_optix_prime_handle_h__
23 #define __optix_optix_prime_handle_h__
24 
25 #include "Ref.h"
26 
27 namespace optix {
28  namespace prime {
29 
31 
33  template <class RefCountedObj>
34  class Handle
35  {
36  public:
38  Handle();
39 
44  explicit Handle( RefCountedObj* ptr );
45 
47  Handle( const Handle<RefCountedObj>& other);
48 
50  void swap( Handle<RefCountedObj>& other);
51 
54  Handle<RefCountedObj>& operator=( const Handle<RefCountedObj>& other);
55 
60  Handle<RefCountedObj>& operator=( RefCountedObj* ptr);
61 
63  void unref();
64 
68  ~Handle();
69 
71  bool isValid() const;
72 
74  static Handle<RefCountedObj> create( RTPcontexttype type ) { return RefCountedObj::create( type ); }
75 
77  RefCountedObj* operator->() const;
78  private:
79 
80  RefCountedObj* m_iptr; // pointer to underlying interface, can be 0
81  };
82 
83 
84  //
85  // Default constructor
86  //
87  template <class RefCountedObj>
88  inline Handle<RefCountedObj>::Handle() : m_iptr(0)
89  {
90 
91  }
92 
93  //
94  // Constructor from Ref pointer
95  //
96  template <class RefCountedObj>
97  inline Handle<RefCountedObj>::Handle( RefCountedObj* ptr ) : m_iptr(ptr)
98  {
99 
100  }
101 
102  //
103  // Copy constructor
104  //
105  template <class RefCountedObj>
106  inline Handle<RefCountedObj>::Handle( const Handle<RefCountedObj>& other ) : m_iptr(other.m_iptr)
107  {
108  if ( m_iptr)
109  m_iptr->ref();
110  }
111 
112  //
113  // Returns \c true if Ref is valid.
114  //
115  template <class RefCountedObj>
116  inline bool Handle<RefCountedObj>::isValid() const
117  {
118  return m_iptr != 0;
119  }
120 
121  //
122  // Releases the current Ref
123  //
124  template <class RefCountedObj>
125  inline void Handle<RefCountedObj>::unref()
126  {
127  if(m_iptr != NULL) {
128  m_iptr->unref();
129  m_iptr = NULL;
130  }
131  }
132 
133  //
134  // Releases the current Ref
135  //
136  template <class RefCountedObj>
137  inline Handle<RefCountedObj>::~Handle()
138  {
139  if ( m_iptr )
140  m_iptr->unref();
141  }
142 
143  //
144  // Assignment operator
145  //
146  template <class RefCountedObj>
147  inline Handle<RefCountedObj>& Handle<RefCountedObj>::operator=( RefCountedObj* ptr )
148  {
149  Handle<RefCountedObj>(ptr).swap(*this);
150  return *this;
151  }
152 
153  //
154  // Assignment operator
155  //
156  template <class RefCountedObj>
157  inline Handle<RefCountedObj>& Handle<RefCountedObj>::operator=( const Handle<RefCountedObj>& other )
158  {
159  Handle<RefCountedObj>(other).swap(*this);
160  return *this;
161  }
162 
163  //
164  // Swap two Refs
165  //
166  template <class RefCountedObj>
167  inline void Handle<RefCountedObj>::swap( Handle<RefCountedObj>& other )
168  {
169  RefCountedObj* tmp_iptr = m_iptr;
170  m_iptr = other.m_iptr;
171  other.m_iptr = tmp_iptr;
172  }
173 
174  //
175  // The arrow operator accesses the interface.
176  //
177  template <class RefCountedObj>
178  inline RefCountedObj* Handle<RefCountedObj>::operator->() const
179  {
180  return m_iptr;
181  }
182 
184 
185  } // end namespace prime
186 } // end namespace optix
187 
188 #endif // #ifndef __optix_optix_prime_handle_h__
RTPcontexttype
Definition: optix_prime_declarations.h:50
Handle()
Default constructor initializes handle to null pointer.
Definition: optixpp_namespace.h:95