From ed2f2769bf113df0088608fa5e949e3e48216a04 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 22 Aug 2017 13:30:20 -0400 Subject: Add pdfium::Optional to third_party/base It is intended to use this class instead of the std::pair pattern that has been used for guarded pointer returns in PDFium. Change-Id: Id3e305d6cdb329c84e1d827c855423d3efae42c0 Reviewed-on: https://pdfium-review.googlesource.com/11610 Reviewed-by: dsinclair Commit-Queue: Ryan Harrison --- third_party/base/optional.h | 506 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 506 insertions(+) create mode 100644 third_party/base/optional.h (limited to 'third_party/base') diff --git a/third_party/base/optional.h b/third_party/base/optional.h new file mode 100644 index 0000000000..4cf91e9b2b --- /dev/null +++ b/third_party/base/optional.h @@ -0,0 +1,506 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PDFIUM_THIRD_PARTY_BASE_OPTIONAL_H_ +#define PDFIUM_THIRD_PARTY_BASE_OPTIONAL_H_ + +#include + +#include "third_party/base/logging.h" + +namespace pdfium { + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/in_place_t +struct in_place_t {}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/nullopt_t +struct nullopt_t { + constexpr explicit nullopt_t(int) {} +}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/in_place +constexpr in_place_t in_place = {}; + +// Specification: +// http://en.cppreference.com/w/cpp/utility/optional/nullopt +constexpr nullopt_t nullopt(0); + +namespace internal { + +template ::value> +struct OptionalStorage { + // Initializing |empty_| here instead of using default member initializing + // to avoid errors in g++ 4.8. + constexpr OptionalStorage() : empty_('\0') {} + + constexpr explicit OptionalStorage(const T& value) + : is_null_(false), value_(value) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::move until C++14. + explicit OptionalStorage(T&& value) + : is_null_(false), value_(std::move(value)) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::forward until C++14. + template + explicit OptionalStorage(in_place_t, Args&&... args) + : is_null_(false), value_(std::forward(args)...) {} + + // When T is not trivially destructible we must call its + // destructor before deallocating its memory. + ~OptionalStorage() { + if (!is_null_) + value_.~T(); + } + + bool is_null_ = true; + union { + // |empty_| exists so that the union will always be initialized, even when + // it doesn't contain a value. Union members must be initialized for the + // constructor to be 'constexpr'. + char empty_; + T value_; + }; +}; + +template +struct OptionalStorage { + // Initializing |empty_| here instead of using default member initializing + // to avoid errors in g++ 4.8. + constexpr OptionalStorage() : empty_('\0') {} + + constexpr explicit OptionalStorage(const T& value) + : is_null_(false), value_(value) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::move until C++14. + explicit OptionalStorage(T&& value) + : is_null_(false), value_(std::move(value)) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::forward until C++14. + template + explicit OptionalStorage(in_place_t, Args&&... args) + : is_null_(false), value_(std::forward(args)...) {} + + // When T is trivially destructible (i.e. its destructor does nothing) there + // is no need to call it. Explicitly defaulting the destructor means it's not + // user-provided. Those two together make this destructor trivial. + ~OptionalStorage() = default; + + bool is_null_ = true; + union { + // |empty_| exists so that the union will always be initialized, even when + // it doesn't contain a value. Union members must be initialized for the + // constructor to be 'constexpr'. + char empty_; + T value_; + }; +}; + +} // namespace internal + +// pdfium::Optional is a PDFium version of the C++17 optional class, +// based on the Chromium version: +// std::optional documentation: +// http://en.cppreference.com/w/cpp/utility/optional +// Chromium documentation: +// https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md +// +// These are the differences between the specification and the implementation: +// - The constructor and emplace method using initializer_list are not +// implemented because 'initializer_list' is banned from Chromium. +// - Constructors do not use 'constexpr' as it is a C++14 extension. +// - 'constexpr' might be missing in some places for reasons specified locally. +// - No exceptions are thrown, because they are banned from Chromium. +// - All the non-members are in the 'pdifum' namespace instead of 'std'. +template +class Optional { + public: + using value_type = T; + + constexpr Optional() {} + + constexpr Optional(nullopt_t) {} + + Optional(const Optional& other) { + if (!other.storage_.is_null_) + Init(other.value()); + } + + Optional(Optional&& other) { + if (!other.storage_.is_null_) + Init(std::move(other.value())); + } + + constexpr Optional(const T& value) : storage_(value) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::move until C++14. + Optional(T&& value) : storage_(std::move(value)) {} + + // TODO(alshabalin): Can't use 'constexpr' with std::forward until C++14. + template + explicit Optional(in_place_t, Args&&... args) + : storage_(in_place, std::forward(args)...) {} + + ~Optional() = default; + + Optional& operator=(nullopt_t) { + FreeIfNeeded(); + return *this; + } + + Optional& operator=(const Optional& other) { + if (other.storage_.is_null_) { + FreeIfNeeded(); + return *this; + } + + InitOrAssign(other.value()); + return *this; + } + + Optional& operator=(Optional&& other) { + if (other.storage_.is_null_) { + FreeIfNeeded(); + return *this; + } + + InitOrAssign(std::move(other.value())); + return *this; + } + + template + typename std::enable_if, T>::value, + Optional&>::type + operator=(U&& value) { + InitOrAssign(std::forward(value)); + return *this; + } + + // TODO(mlamouri): can't use 'constexpr' with DCHECK. + const T* operator->() const { + DCHECK(!storage_.is_null_); + return &value(); + } + + // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was + // meant to be 'constexpr const'. + T* operator->() { + DCHECK(!storage_.is_null_); + return &value(); + } + + constexpr const T& operator*() const& { return value(); } + + // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was + // meant to be 'constexpr const'. + T& operator*() & { return value(); } + + constexpr const T&& operator*() const&& { return std::move(value()); } + + // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was + // meant to be 'constexpr const'. + T&& operator*() && { return std::move(value()); } + + constexpr explicit operator bool() const { return !storage_.is_null_; } + + constexpr bool has_value() const { return !storage_.is_null_; } + + // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was + // meant to be 'constexpr const'. + T& value() & { + DCHECK(!storage_.is_null_); + return storage_.value_; + } + + // TODO(mlamouri): can't use 'constexpr' with DCHECK. + const T& value() const& { + DCHECK(!storage_.is_null_); + return storage_.value_; + } + + // TODO(mlamouri): using 'constexpr' here breaks compiler that assume it was + // meant to be 'constexpr const'. + T&& value() && { + DCHECK(!storage_.is_null_); + return std::move(storage_.value_); + } + + // TODO(mlamouri): can't use 'constexpr' with DCHECK. + const T&& value() const&& { + DCHECK(!storage_.is_null_); + return std::move(storage_.value_); + } + + template + constexpr T value_or(U&& default_value) const& { + // TODO(mlamouri): add the following assert when possible: + // static_assert(std::is_copy_constructible::value, + // "T must be copy constructible"); + static_assert(std::is_convertible::value, + "U must be convertible to T"); + return storage_.is_null_ ? static_cast(std::forward(default_value)) + : value(); + } + + template + T value_or(U&& default_value) && { + // TODO(mlamouri): add the following assert when possible: + // static_assert(std::is_move_constructible::value, + // "T must be move constructible"); + static_assert(std::is_convertible::value, + "U must be convertible to T"); + return storage_.is_null_ ? static_cast(std::forward(default_value)) + : std::move(value()); + } + + void swap(Optional& other) { + if (storage_.is_null_ && other.storage_.is_null_) + return; + + if (storage_.is_null_ != other.storage_.is_null_) { + if (storage_.is_null_) { + Init(std::move(other.storage_.value_)); + other.FreeIfNeeded(); + } else { + other.Init(std::move(storage_.value_)); + FreeIfNeeded(); + } + return; + } + + DCHECK(!storage_.is_null_ && !other.storage_.is_null_); + using std::swap; + swap(**this, *other); + } + + void reset() { + FreeIfNeeded(); + } + + template + void emplace(Args&&... args) { + FreeIfNeeded(); + Init(std::forward(args)...); + } + + private: + void Init(const T& value) { + DCHECK(storage_.is_null_); + new (&storage_.value_) T(value); + storage_.is_null_ = false; + } + + void Init(T&& value) { + DCHECK(storage_.is_null_); + new (&storage_.value_) T(std::move(value)); + storage_.is_null_ = false; + } + + template + void Init(Args&&... args) { + DCHECK(storage_.is_null_); + new (&storage_.value_) T(std::forward(args)...); + storage_.is_null_ = false; + } + + void InitOrAssign(const T& value) { + if (storage_.is_null_) + Init(value); + else + storage_.value_ = value; + } + + void InitOrAssign(T&& value) { + if (storage_.is_null_) + Init(std::move(value)); + else + storage_.value_ = std::move(value); + } + + void FreeIfNeeded() { + if (storage_.is_null_) + return; + storage_.value_.~T(); + storage_.is_null_ = true; + } + + internal::OptionalStorage storage_; +}; + +template +constexpr bool operator==(const Optional& lhs, const Optional& rhs) { + return !!lhs != !!rhs ? false : lhs == nullopt || (*lhs == *rhs); +} + +template +constexpr bool operator!=(const Optional& lhs, const Optional& rhs) { + return !(lhs == rhs); +} + +template +constexpr bool operator<(const Optional& lhs, const Optional& rhs) { + return rhs == nullopt ? false : (lhs == nullopt ? true : *lhs < *rhs); +} + +template +constexpr bool operator<=(const Optional& lhs, const Optional& rhs) { + return !(rhs < lhs); +} + +template +constexpr bool operator>(const Optional& lhs, const Optional& rhs) { + return rhs < lhs; +} + +template +constexpr bool operator>=(const Optional& lhs, const Optional& rhs) { + return !(lhs < rhs); +} + +template +constexpr bool operator==(const Optional& opt, nullopt_t) { + return !opt; +} + +template +constexpr bool operator==(nullopt_t, const Optional& opt) { + return !opt; +} + +template +constexpr bool operator!=(const Optional& opt, nullopt_t) { + return !!opt; +} + +template +constexpr bool operator!=(nullopt_t, const Optional& opt) { + return !!opt; +} + +template +constexpr bool operator<(const Optional& opt, nullopt_t) { + return false; +} + +template +constexpr bool operator<(nullopt_t, const Optional& opt) { + return !!opt; +} + +template +constexpr bool operator<=(const Optional& opt, nullopt_t) { + return !opt; +} + +template +constexpr bool operator<=(nullopt_t, const Optional& opt) { + return true; +} + +template +constexpr bool operator>(const Optional& opt, nullopt_t) { + return !!opt; +} + +template +constexpr bool operator>(nullopt_t, const Optional& opt) { + return false; +} + +template +constexpr bool operator>=(const Optional& opt, nullopt_t) { + return true; +} + +template +constexpr bool operator>=(nullopt_t, const Optional& opt) { + return !opt; +} + +template +constexpr bool operator==(const Optional& opt, const T& value) { + return opt != nullopt ? *opt == value : false; +} + +template +constexpr bool operator==(const T& value, const Optional& opt) { + return opt == value; +} + +template +constexpr bool operator!=(const Optional& opt, const T& value) { + return !(opt == value); +} + +template +constexpr bool operator!=(const T& value, const Optional& opt) { + return !(opt == value); +} + +template +constexpr bool operator<(const Optional& opt, const T& value) { + return opt != nullopt ? *opt < value : true; +} + +template +constexpr bool operator<(const T& value, const Optional& opt) { + return opt != nullopt ? value < *opt : false; +} + +template +constexpr bool operator<=(const Optional& opt, const T& value) { + return !(opt > value); +} + +template +constexpr bool operator<=(const T& value, const Optional& opt) { + return !(value > opt); +} + +template +constexpr bool operator>(const Optional& opt, const T& value) { + return value < opt; +} + +template +constexpr bool operator>(const T& value, const Optional& opt) { + return opt < value; +} + +template +constexpr bool operator>=(const Optional& opt, const T& value) { + return !(opt < value); +} + +template +constexpr bool operator>=(const T& value, const Optional& opt) { + return !(value < opt); +} + +template +constexpr Optional::type> make_optional(T&& value) { + return Optional::type>(std::forward(value)); +} + +template +void swap(Optional& lhs, Optional& rhs) { + lhs.swap(rhs); +} + +} // namespace pdfium + +namespace std { + +template +struct hash> { + size_t operator()(const pdfium::Optional& opt) const { + return opt == pdfium::nullopt ? 0 : std::hash()(*opt); + } +}; + +} // namespace std + +#endif // PDFIUM_THIRD_PARTY_BASE_OPTIONAL_H_ -- cgit v1.2.3