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