1 /*
2 ** Copyright 2011-2013 Centreon
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 **
16 ** For more information : contact@centreon.com
17 */
18 
19 #ifndef CC_UNIQUE_ARRAY_PTR_HH
20 #define CC_UNIQUE_ARRAY_PTR_HH
21 
22 #include <cstddef>
23 #include "com/centreon/namespace.hh"
24 
25 CC_BEGIN()
26 
27 /**
28  *  @class unique_array_ptr unique_array_ptr.hh
29  *  @brief Similar to unique_ptr for arrays.
30  *
31  *  Provide similar feature as unique_ptr but for array pointers.
32  */
33 template <typename T>
34 class unique_array_ptr {
35  private:
36   T* _ptr;
37 
38  public:
39   /**
40    *  Constructor.
41    *
42    *  @param[in] t Array pointer.
43    */
unique_array_ptr(T * t=NULL)44   unique_array_ptr(T* t = NULL) : _ptr(t) {}
45 
46   /**
47    *  Copy constructor.
48    *
49    *  @param[in] uap Object to copy.
50    */
unique_array_ptr(unique_array_ptr & uap)51   unique_array_ptr(unique_array_ptr& uap) : _ptr(uap._ptr) { uap._ptr = NULL; }
52 
53   /**
54    *  Destructor.
55    */
~unique_array_ptr()56   ~unique_array_ptr() { delete[] _ptr; }
57 
58   /**
59    *  Assignment operator.
60    *
61    *  @param[in] uap Object to copy.
62    *
63    *  @return This object.
64    */
operator =(unique_array_ptr & uap)65   unique_array_ptr& operator=(unique_array_ptr& uap) {
66     if (&uap != this) {
67       _ptr = uap._ptr;
68       uap._ptr = NULL;
69     }
70     return (*this);
71   }
72 
73   /**
74    *  Dereferencing pointer.
75    *
76    *  @return Dereferenced pointer.
77    */
operator *()78   T& operator*() { return (*_ptr); }
79 
80   /**
81    *  Array access operator.
82    *
83    *  @param[in] idx Index in array.
84    *
85    *  @return Element at position idx.
86    */
operator [](unsigned int idx)87   T& operator[](unsigned int idx) { return (_ptr[idx]); }
88 
89   /**
90    *  Get the pointer associated with this object.
91    *
92    *  @return Pointer associated with this object.
93    */
get() const94   T* get() const { return (_ptr); }
95 
96   /**
97    *  Release the associated pointer and release it.
98    *
99    *  @return Pointer associated with this object.
100    */
release()101   T* release() {
102     T* tmp(_ptr);
103     _ptr = NULL;
104     return (tmp);
105   }
106 
107   /**
108    *  Reset this automatic pointer.
109    *
110    *  @param[in] ptr New pointer (can be NULL).
111    */
reset(T * t=NULL)112   void reset(T* t = NULL) {
113     delete[] _ptr;
114     _ptr = t;
115     return;
116   }
117 };
118 
119 CC_END()
120 
121 #endif  // !CC_UNIQUE_ARRAY_PTR_HH
122