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 UTIL_CHRE_OPTIONAL_IMPL_H_ 18 #define UTIL_CHRE_OPTIONAL_IMPL_H_ 19 20 #include "chre/util/optional.h" 21 22 namespace chre { 23 24 template<typename ObjectType> Optional()25Optional<ObjectType>::Optional() {} 26 27 template<typename ObjectType> Optional(const ObjectType & object)28Optional<ObjectType>::Optional(const ObjectType& object) 29 : mObject(object), mHasValue(true) {} 30 31 template<typename ObjectType> Optional(ObjectType && object)32Optional<ObjectType>::Optional(ObjectType&& object) 33 : mObject(std::move(object)), mHasValue(true) {} 34 35 template<typename ObjectType> has_value()36bool Optional<ObjectType>::has_value() const { 37 return mHasValue; 38 } 39 40 template<typename ObjectType> reset()41void Optional<ObjectType>::reset() { 42 mObject.~ObjectType(); 43 mHasValue = false; 44 } 45 46 template<typename ObjectType> 47 Optional<ObjectType>& Optional<ObjectType>::operator=(ObjectType&& other) { 48 mObject = std::move(other); 49 mHasValue = true; 50 return *this; 51 } 52 53 template<typename ObjectType> 54 Optional<ObjectType>& Optional<ObjectType>::operator=( 55 Optional<ObjectType>&& other) { 56 mObject = std::move(other.mObject); 57 mHasValue = other.mHasValue; 58 other.mHasValue = false; 59 return *this; 60 } 61 62 template<typename ObjectType> 63 Optional<ObjectType>& Optional<ObjectType>::operator=(const ObjectType& other) { 64 mObject = other; 65 mHasValue = true; 66 return *this; 67 } 68 69 template<typename ObjectType> 70 Optional<ObjectType>& Optional<ObjectType>::operator=( 71 const Optional<ObjectType>& other) { 72 mObject = other.mObject; 73 mHasValue = other.mHasValue; 74 return *this; 75 } 76 77 template<typename ObjectType> 78 ObjectType& Optional<ObjectType>::operator*() { 79 return mObject; 80 } 81 82 template<typename ObjectType> 83 const ObjectType& Optional<ObjectType>::operator*() const { 84 return mObject; 85 } 86 87 template<typename ObjectType> 88 ObjectType *Optional<ObjectType>::operator->() { 89 return &mObject; 90 } 91 92 template<typename ObjectType> 93 const ObjectType *Optional<ObjectType>::operator->() const { 94 return &mObject; 95 } 96 97 } // namespace chre 98 99 #endif // UTIL_CHRE_OPTIONAL_IMPL_H_ 100