1 /*
2   Copyright (c) DataStax, Inc.
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 
17 #ifndef DATASTAX_INTERNAL_SCOPED_PTR_HPP
18 #define DATASTAX_INTERNAL_SCOPED_PTR_HPP
19 
20 #include "macros.hpp"
21 #include "utils.hpp"
22 
23 #include <stddef.h>
24 
25 namespace datastax { namespace internal {
26 
27 template <class T>
28 struct DefaultDeleter {
operator ()datastax::internal::DefaultDeleter29   void operator()(T* ptr) const { delete ptr; }
30 };
31 
32 template <class T, class D = DefaultDeleter<T> >
33 class ScopedPtr {
34 public:
35   typedef T type;
36   typedef D deleter;
37 
ScopedPtr(type * ptr=NULL)38   explicit ScopedPtr(type* ptr = NULL)
39       : ptr_(ptr) {}
40 
~ScopedPtr()41   ~ScopedPtr() { deleter()(ptr_); }
42 
reset(type * ptr=NULL)43   void reset(type* ptr = NULL) {
44     if (ptr_ != NULL) {
45       deleter()(ptr_);
46     }
47     ptr_ = ptr;
48   }
49 
release()50   type* release() {
51     type* temp = ptr_;
52     ptr_ = NULL;
53     return temp;
54   }
55 
get() const56   type* get() const { return ptr_; }
operator *() const57   type& operator*() const { return *ptr_; }
operator ->() const58   type* operator->() const { return ptr_; }
operator bool() const59   operator bool() const { return ptr_ != NULL; }
60 
61 private:
62   type* ptr_;
63 
64 private:
65   DISALLOW_COPY_AND_ASSIGN(ScopedPtr);
66 };
67 
68 template <class T>
69 struct DefaultArrayDeleter {
operator ()datastax::internal::DefaultArrayDeleter70   void operator()(T* ptr) const { delete[] ptr; }
71 };
72 
73 template <class T, class D = DefaultArrayDeleter<T> >
74 class ScopedArray {
75 public:
76   typedef T type;
77   typedef D deleter;
78 
ScopedArray(type * ptr=NULL)79   explicit ScopedArray(type* ptr = NULL)
80       : ptr_(ptr) {}
81 
~ScopedArray()82   ~ScopedArray() { deleter()(ptr_); }
83 
reset(type * ptr=NULL)84   void reset(type* ptr = NULL) {
85     if (ptr_ != NULL) {
86       deleter()(ptr_);
87     }
88     ptr_ = ptr;
89   }
90 
release()91   type* release() {
92     type* temp = ptr_;
93     ptr_ = NULL;
94     return temp;
95   }
96 
get() const97   type* get() const { return ptr_; }
operator [](size_t index) const98   type& operator[](size_t index) const { return ptr_[index]; }
operator bool() const99   operator bool() const { return ptr_ != NULL; }
100 
101 private:
102   type* ptr_;
103 
104 private:
105   DISALLOW_COPY_AND_ASSIGN(ScopedArray);
106 };
107 
108 }} // namespace datastax::internal
109 
110 #endif
111