optional_impl.h revision 39993a0032116a71eb51cbf14186eec6bab328f2
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
22namespace chre {
23
24template<typename ObjectType>
25Optional<ObjectType>::Optional() {}
26
27template<typename ObjectType>
28Optional<ObjectType>::Optional(const ObjectType& object)
29    : mObject(object), mHasValue(true) {}
30
31template<typename ObjectType>
32Optional<ObjectType>::Optional(ObjectType&& object)
33    : mObject(std::move(object)), mHasValue(true) {}
34
35template<typename ObjectType>
36bool Optional<ObjectType>::has_value() const {
37  return mHasValue;
38}
39
40template<typename ObjectType>
41void Optional<ObjectType>::reset() {
42  mObject.~ObjectType();
43  mHasValue = false;
44}
45
46template<typename ObjectType>
47Optional<ObjectType>& Optional<ObjectType>::operator=(ObjectType&& other) {
48  mObject = std::move(other);
49  mHasValue = true;
50  return *this;
51}
52
53template<typename ObjectType>
54Optional<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
62template<typename ObjectType>
63Optional<ObjectType>& Optional<ObjectType>::operator=(const ObjectType& other) {
64  mObject = other;
65  mHasValue = true;
66  return *this;
67}
68
69template<typename ObjectType>
70Optional<ObjectType>& Optional<ObjectType>::operator=(
71    const Optional<ObjectType>& other) {
72  mObject = other.mObject;
73  mHasValue = other.mHasValue;
74  return *this;
75}
76
77template<typename ObjectType>
78ObjectType& Optional<ObjectType>::operator*() {
79  return mObject;
80}
81
82template<typename ObjectType>
83ObjectType *Optional<ObjectType>::operator->() {
84  return &mObject;
85}
86
87}  // namespace chre
88
89#endif  // UTIL_CHRE_OPTIONAL_IMPL_H_
90