Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NekPtr.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File NekPtr.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Scientific Computing and Imaging Institute,
10 // University of Utah (USA) and Department of Aeronautics, Imperial
11 // College London (UK).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: A lightweight shared pointer.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_PTR_HPP
37 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_PTR_HPP
38 
41 
42 namespace Nektar
43 {
44  template<typename T>
45  class NekPtr
46  {
47  public:
48  NekPtr() :
49  m_data(0),
50  m_count(0),
51  m_size(0)
52  {
53  }
54 
55  explicit NekPtr(T* d) :
56  m_data(d),
57  m_count(MemoryManager<unsigned int>::RawAllocate(1)),
58  m_size(1)
59  {
60  *m_count = 1;
61  }
62 
63  NekPtr(T* d, unsigned int s) :
64  m_data(d),
65  m_count(MemoryManager<unsigned int>::RawAllocate(1)),
66  m_size(s)
67  {
68  *m_count = 1;
69  }
70 
71  NekPtr(const NekPtr<T>& rhs) :
72  m_data(rhs.m_data),
73  m_count(rhs.m_count),
74  m_size(rhs.m_size)
75  {
76  if( m_count != 0 )
77  {
78  *m_count += 1;
79  }
80  }
81 
83  {
84  NekPtr temp(rhs);
85  Swap(temp);
86  return *this;
87  }
88 
90  {
91  if( m_count == 0 )
92  {
93  return;
94  }
95 
96  *m_count -= 1;
97  if( *m_count == 0 )
98  {
99  ArrayDestructionPolicy<T>::Destroy(m_data, m_size);
102  }
103  }
104 
105  T* get() { return m_data; }
106  const T* get() const { return m_data; }
107 
108  private:
109  void Swap(NekPtr<T>& rhs)
110  {
111  std::swap(m_data, rhs.m_data);
112  std::swap(m_count, rhs.m_count);
113  std::swap(m_size, rhs.m_size);
114  }
115 
116  T* m_data;
117  unsigned int* m_count;
118  int m_size;
119  };
120 
121 }
122 
123 
124 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_PTR_HPP
125 
126 
127