• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 CHRE_UTIL_UNIQUE_PTR_IMPL_H_
18 #define CHRE_UTIL_UNIQUE_PTR_IMPL_H_
19 
20 #include "chre/util/unique_ptr.h"
21 
22 #include <string.h>
23 #include <type_traits>
24 #include <utility>
25 
26 #include "chre/util/container_support.h"
27 #include "chre/util/memory.h"
28 
29 namespace chre {
30 
31 template<typename ObjectType>
UniquePtr()32 UniquePtr<ObjectType>::UniquePtr() : mObject(nullptr) {}
33 
34 template<typename ObjectType>
UniquePtr(ObjectType * object)35 UniquePtr<ObjectType>::UniquePtr(ObjectType *object) : mObject(object) {}
36 
37 template<typename ObjectType>
UniquePtr(UniquePtr<ObjectType> && other)38 UniquePtr<ObjectType>::UniquePtr(UniquePtr<ObjectType>&& other) {
39   mObject = other.mObject;
40   other.mObject = nullptr;
41 }
42 
43 template<typename ObjectType>
44 template<typename OtherObjectType>
UniquePtr(UniquePtr<OtherObjectType> && other)45 UniquePtr<ObjectType>::UniquePtr(UniquePtr<OtherObjectType>&& other) {
46   mObject = other.mObject;
47   other.mObject = nullptr;
48 }
49 
50 template<typename ObjectType>
~UniquePtr()51 UniquePtr<ObjectType>::~UniquePtr() {
52   if (mObject != nullptr) {
53     mObject->~ObjectType();
54     memoryFree(mObject);
55     mObject = nullptr;
56   }
57 }
58 
59 template<typename ObjectType>
isNull()60 bool UniquePtr<ObjectType>::isNull() const {
61   return (mObject == nullptr);
62 }
63 
64 template<typename ObjectType>
get()65 ObjectType *UniquePtr<ObjectType>::get() const {
66   return mObject;
67 }
68 
69 template<typename ObjectType>
release()70 ObjectType *UniquePtr<ObjectType>::release() {
71   ObjectType *obj = mObject;
72   mObject = nullptr;
73   return obj;
74 }
75 
76 template<typename ObjectType>
reset(ObjectType * object)77 void UniquePtr<ObjectType>::reset(ObjectType *object) {
78   CHRE_ASSERT(object == nullptr || mObject != object);
79 
80   this->~UniquePtr<ObjectType>();
81   mObject = object;
82 }
83 
84 template<typename ObjectType>
85 ObjectType *UniquePtr<ObjectType>::operator->() const {
86   return get();
87 }
88 
89 template<typename ObjectType>
90 ObjectType& UniquePtr<ObjectType>::operator*() const {
91   return *get();
92 }
93 
94 template<typename ObjectType>
95 ObjectType& UniquePtr<ObjectType>::operator[](size_t index) const {
96   return get()[index];
97 }
98 
99 template<typename ObjectType>
100 UniquePtr<ObjectType>& UniquePtr<ObjectType>::operator=(
101     UniquePtr<ObjectType>&& other) {
102   this->~UniquePtr<ObjectType>();
103   mObject = other.mObject;
104   other.mObject = nullptr;
105   return *this;
106 }
107 
108 template<typename ObjectType, typename... Args>
MakeUnique(Args &&...args)109 inline UniquePtr<ObjectType> MakeUnique(Args&&... args) {
110   return UniquePtr<ObjectType>(memoryAlloc<ObjectType>(
111       std::forward<Args>(args)...));
112 }
113 
114 template<typename ObjectType>
MakeUniqueZeroFill()115 inline UniquePtr<ObjectType> MakeUniqueZeroFill() {
116   // For simplicity, we call memset *after* memoryAlloc<ObjectType>() - this is
117   // only valid for types that have a trivial constructor. This utility function
118   // is really meant to be used with trivial types only - if there's a desire to
119   // zero things out in a non-trivial type, the right place for that is in its
120   // constructor.
121   static_assert(std::is_trivial<ObjectType>::value,
122                 "MakeUniqueZeroFill is only supported for trivial types");
123   auto ptr = UniquePtr<ObjectType>(memoryAlloc<ObjectType>());
124   if (!ptr.isNull()) {
125     memset(ptr.get(), 0, sizeof(ObjectType));
126   }
127   return ptr;
128 }
129 
130 }  // namespace chre
131 
132 #endif  // CHRE_UTIL_UNIQUE_PTR_IMPL_H_
133