optional_impl.h revision a168da9318a8d996ffd82beacb69a248b11d8569
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>
32bool Optional<ObjectType>::has_value() const {
33  return mHasValue;
34}
35
36template<typename ObjectType>
37void Optional<ObjectType>::reset() {
38  mObject.~ObjectType();
39  mHasValue = false;
40}
41
42template<typename ObjectType>
43Optional<ObjectType>& Optional<ObjectType>::operator=(ObjectType&& other) {
44  mObject = std::move(other);
45  mHasValue = true;
46  return *this;
47}
48
49template<typename ObjectType>
50Optional<ObjectType>& Optional<ObjectType>::operator=(
51    Optional<ObjectType>&& other) {
52  mObject = std::move(other.mObject);
53  mHasValue = other.mHasValue;
54  other.mHasValue = false;
55  return *this;
56}
57
58template<typename ObjectType>
59Optional<ObjectType>& Optional<ObjectType>::operator=(const ObjectType& other) {
60  mObject = other;
61  mHasValue = true;
62  return *this;
63}
64
65template<typename ObjectType>
66ObjectType& Optional<ObjectType>::operator*() {
67  return mObject;
68}
69
70template<typename ObjectType>
71ObjectType *Optional<ObjectType>::operator->() {
72  return &mObject;
73}
74
75}  // namespace chre
76
77#endif  // UTIL_CHRE_OPTIONAL_IMPL_H_
78