optional.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 The Cartographer Authors
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 CARTOGRAPHER_COMMON_OPTIONAL_H_
18 #define CARTOGRAPHER_COMMON_OPTIONAL_H_
19 
20 #include <memory>
21 
23 #include "glog/logging.h"
24 
25 namespace cartographer {
26 namespace common {
27 
28 template <class T>
29 class optional {
30  public:
31  optional() {}
32 
33  optional(const optional& other) {
34  if (other.has_value()) {
35  value_ = common::make_unique<T>(other.value());
36  }
37  }
38 
39  explicit optional(const T& value) { value_ = common::make_unique<T>(value); }
40 
41  bool has_value() const { return value_ != nullptr; }
42 
43  const T& value() const {
44  CHECK(value_ != nullptr);
45  return *value_;
46  }
47 
48  optional<T>& operator=(const T& other_value) {
49  this->value_ = common::make_unique<T>(other_value);
50  return *this;
51  }
52 
54  if (!other.has_value()) {
55  this->value_ = nullptr;
56  } else {
57  this->value_ = common::make_unique<T>(other.value());
58  }
59  return *this;
60  }
61 
62  private:
63  std::unique_ptr<T> value_;
64 };
65 
66 } // namespace common
67 } // namespace cartographer
68 
69 #endif // CARTOGRAPHER_COMMON_OPTIONAL_H_
optional(const T &value)
Definition: optional.h:39
optional< T > & operator=(const optional< T > &other)
Definition: optional.h:53
std::unique_ptr< T > value_
Definition: optional.h:63
optional< T > & operator=(const T &other_value)
Definition: optional.h:48
optional(const optional &other)
Definition: optional.h:33
const T & value() const
Definition: optional.h:43


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58