/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CHRE_UTIL_UNIQUE_PTR_IMPL_H_ #define CHRE_UTIL_UNIQUE_PTR_IMPL_H_ #include "chre/util/unique_ptr.h" #include #include #include #include "chre/util/container_support.h" #include "chre/util/memory.h" namespace chre { template UniquePtr::UniquePtr() : mObject(nullptr) {} template UniquePtr::UniquePtr(ObjectType *object) : mObject(object) {} template UniquePtr::UniquePtr(UniquePtr &&other) { mObject = other.mObject; other.mObject = nullptr; } template template UniquePtr::UniquePtr(UniquePtr &&other) { mObject = other.mObject; other.mObject = nullptr; } template UniquePtr::~UniquePtr() { reset(); } template bool UniquePtr::isNull() const { return (mObject == nullptr); } template ObjectType *UniquePtr::get() const { return mObject; } template ObjectType *UniquePtr::release() { ObjectType *obj = mObject; mObject = nullptr; return obj; } template void UniquePtr::reset(ObjectType *object) { CHRE_ASSERT(object == nullptr || mObject != object); reset(); mObject = object; } template void UniquePtr::reset() { if (mObject != nullptr) { mObject->~ObjectType(); memoryFree(mObject); mObject = nullptr; } } template ObjectType *UniquePtr::operator->() const { return get(); } template ObjectType &UniquePtr::operator*() const { return *get(); } template ObjectType &UniquePtr::operator[](size_t index) const { return get()[index]; } template bool UniquePtr::operator==( const UniquePtr &other) const { return mObject == other.get(); } template bool UniquePtr::operator!=( const UniquePtr &other) const { return !(*this == other); } template UniquePtr &UniquePtr::operator=( UniquePtr &&other) { reset(); mObject = other.mObject; other.mObject = nullptr; return *this; } template inline UniquePtr MakeUnique(Args &&... args) { return UniquePtr( memoryAlloc(std::forward(args)...)); } template inline UniquePtr MakeUniqueZeroFill() { // For simplicity, we call memset *after* memoryAlloc() - this is // only valid for types that have a trivial constructor. This utility function // is really meant to be used with trivial types only - if there's a desire to // zero things out in a non-trivial type, the right place for that is in its // constructor. static_assert(std::is_trivial::value, "MakeUniqueZeroFill is only supported for trivial types"); auto ptr = UniquePtr(memoryAlloc()); if (!ptr.isNull()) { memset(ptr.get(), 0, sizeof(ObjectType)); } return ptr; } } // namespace chre #endif // CHRE_UTIL_UNIQUE_PTR_IMPL_H_