From 63a80c9340782e8b35260b682c227320cc955885 Mon Sep 17 00:00:00 2001 From: Susanne Pielawa <32822068+spielawa@users.noreply.github.com> Date: Fri, 5 Jan 2018 11:27:21 +0100 Subject: [PATCH] Adding a minimal implementation of std::optional. (#783) We're using C++11, which doesn't have std::optional. We need a few features of std::optional for improved GPS support. These are implemented here. --- cartographer/common/optional.h | 55 ++++++++++++++++++++++++++++ cartographer/common/optional_test.cc | 49 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 cartographer/common/optional.h create mode 100644 cartographer/common/optional_test.cc diff --git a/cartographer/common/optional.h b/cartographer/common/optional.h new file mode 100644 index 0000000..1f6c8d9 --- /dev/null +++ b/cartographer/common/optional.h @@ -0,0 +1,55 @@ +/* + * Copyright 2017 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CARTOGRAPHER_COMMON_OPTIONAL_H_ +#define CARTOGRAPHER_COMMON_OPTIONAL_H_ + +#include + +#include "cartographer/common/make_unique.h" +#include "glog/logging.h" + +namespace cartographer { +namespace common { + +template +class optional { + public: + optional() {} + + optional(const optional& other) { + if (other.has_value()) { + value_ = common::make_unique(other.value()); + } + } + + explicit optional(const T& value) { value_ = common::make_unique(value); } + + bool has_value() const { return value_ != nullptr; } + + const T& value() const { + CHECK(value_ != nullptr); + return *value_; + } + + private: + std::unique_ptr value_; +}; + +} // namespace common +} // namespace cartographer + +#endif // CARTOGRAPHER_COMMON_OPTIONAL_H_ diff --git a/cartographer/common/optional_test.cc b/cartographer/common/optional_test.cc new file mode 100644 index 0000000..f7dcc7c --- /dev/null +++ b/cartographer/common/optional_test.cc @@ -0,0 +1,49 @@ +/* + * Copyright 2017 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cartographer/common/optional.h" + +#include "gtest/gtest.h" + +namespace cartographer { +namespace common { +namespace { + +TEST(OptionalTest, CreateDisengagedObject) { + const optional o; + EXPECT_FALSE(o.has_value()); + const optional x; + EXPECT_FALSE(x.has_value()); +} + +TEST(OptionalTest, CreateWithValue) { + const optional a(5); + EXPECT_TRUE(a.has_value()); + EXPECT_EQ(5, a.value()); +} + +TEST(OptionalTest, CreateFromOtherOptional) { + const optional a(5); + const optional b = a; + EXPECT_TRUE(a.has_value()); + EXPECT_TRUE(b.has_value()); + EXPECT_EQ(5, a.value()); + EXPECT_EQ(5, b.value()); +} + +} // namespace +} // namespace common +} // namespace cartographer