2010-04-09 20:25:54 +00:00
|
|
|
//===-- Optional.h - Simple variant for passing optional values ---*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides Optional, a template class modeled in the spirit of
|
|
|
|
// OCaml's 'opt' variant. The idea is to strongly type whether or not
|
|
|
|
// a value can be optional.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ADT_OPTIONAL_H
|
|
|
|
#define LLVM_ADT_OPTIONAL_H
|
2010-04-09 20:25:54 +00:00
|
|
|
|
2013-02-21 00:27:28 +00:00
|
|
|
#include "llvm/ADT/None.h"
|
2013-02-20 00:26:04 +00:00
|
|
|
#include "llvm/Support/AlignOf.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-04-09 20:25:54 +00:00
|
|
|
#include <cassert>
|
2014-10-01 02:12:35 +00:00
|
|
|
#include <new>
|
2012-10-18 22:22:55 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2010-04-09 20:25:54 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class Optional {
|
2013-02-20 00:26:04 +00:00
|
|
|
AlignedCharArrayUnion<T> storage;
|
2013-01-02 21:19:08 +00:00
|
|
|
bool hasVal;
|
2010-04-09 20:25:54 +00:00
|
|
|
public:
|
2014-09-29 18:56:05 +00:00
|
|
|
typedef T value_type;
|
|
|
|
|
2013-02-21 00:27:28 +00:00
|
|
|
Optional(NoneType) : hasVal(false) {}
|
2013-02-20 00:26:04 +00:00
|
|
|
explicit Optional() : hasVal(false) {}
|
|
|
|
Optional(const T &y) : hasVal(true) {
|
|
|
|
new (storage.buffer) T(y);
|
|
|
|
}
|
|
|
|
Optional(const Optional &O) : hasVal(O.hasVal) {
|
|
|
|
if (hasVal)
|
|
|
|
new (storage.buffer) T(*O);
|
|
|
|
}
|
2010-04-09 20:25:54 +00:00
|
|
|
|
2013-02-20 00:26:04 +00:00
|
|
|
Optional(T &&y) : hasVal(true) {
|
|
|
|
new (storage.buffer) T(std::forward<T>(y));
|
|
|
|
}
|
2013-02-21 07:55:39 +00:00
|
|
|
Optional(Optional<T> &&O) : hasVal(O) {
|
|
|
|
if (O) {
|
|
|
|
new (storage.buffer) T(std::move(*O));
|
|
|
|
O.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Optional &operator=(T &&y) {
|
|
|
|
if (hasVal)
|
|
|
|
**this = std::move(y);
|
|
|
|
else {
|
|
|
|
new (storage.buffer) T(std::move(y));
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Optional &operator=(Optional &&O) {
|
|
|
|
if (!O)
|
|
|
|
reset();
|
|
|
|
else {
|
|
|
|
*this = std::move(*O);
|
|
|
|
O.reset();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2012-10-18 22:22:55 +00:00
|
|
|
|
2014-10-01 02:12:35 +00:00
|
|
|
/// Create a new object by constructing it in place with the given arguments.
|
|
|
|
template<typename ...ArgTypes>
|
|
|
|
void emplace(ArgTypes &&...Args) {
|
|
|
|
reset();
|
|
|
|
hasVal = true;
|
|
|
|
new (storage.buffer) T(std::forward<ArgTypes>(Args)...);
|
|
|
|
}
|
|
|
|
|
2010-04-09 20:25:54 +00:00
|
|
|
static inline Optional create(const T* y) {
|
|
|
|
return y ? Optional(*y) : Optional();
|
|
|
|
}
|
|
|
|
|
2013-02-21 07:55:39 +00:00
|
|
|
// FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
|
|
|
|
// could be made more efficient by passing by value, possibly unifying them
|
|
|
|
// with the rvalue versions above - but this could place a different set of
|
|
|
|
// requirements (notably: the existence of a default ctor) when implemented
|
|
|
|
// in that way. Careful SFINAE to avoid such pitfalls would be required.
|
2010-04-09 20:25:54 +00:00
|
|
|
Optional &operator=(const T &y) {
|
2013-02-20 00:26:04 +00:00
|
|
|
if (hasVal)
|
|
|
|
**this = y;
|
|
|
|
else {
|
|
|
|
new (storage.buffer) T(y);
|
|
|
|
hasVal = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional &operator=(const Optional &O) {
|
|
|
|
if (!O)
|
2013-02-20 06:25:36 +00:00
|
|
|
reset();
|
2013-02-20 00:26:04 +00:00
|
|
|
else
|
|
|
|
*this = *O;
|
2010-04-09 20:25:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-02-20 00:26:04 +00:00
|
|
|
|
2013-02-20 06:25:36 +00:00
|
|
|
void reset() {
|
2013-02-20 00:26:04 +00:00
|
|
|
if (hasVal) {
|
2013-02-21 02:32:25 +00:00
|
|
|
(**this).~T();
|
2013-02-20 00:26:04 +00:00
|
|
|
hasVal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Optional() {
|
2013-02-20 06:25:36 +00:00
|
|
|
reset();
|
2013-02-20 00:26:04 +00:00
|
|
|
}
|
2013-02-20 15:12:32 +00:00
|
|
|
|
2013-02-20 00:26:04 +00:00
|
|
|
const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
|
|
|
|
T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
|
|
|
|
const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
|
|
|
|
T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
|
2010-04-09 20:25:54 +00:00
|
|
|
|
2015-02-15 22:00:20 +00:00
|
|
|
explicit operator bool() const { return hasVal; }
|
2010-04-09 20:25:54 +00:00
|
|
|
bool hasValue() const { return hasVal; }
|
|
|
|
const T* operator->() const { return getPointer(); }
|
2013-02-20 00:26:04 +00:00
|
|
|
T* operator->() { return getPointer(); }
|
|
|
|
const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
|
|
|
|
T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
|
2012-11-30 00:38:53 +00:00
|
|
|
|
2014-09-29 18:56:08 +00:00
|
|
|
template <typename U>
|
2014-09-29 20:27:01 +00:00
|
|
|
LLVM_CONSTEXPR T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
|
2014-09-29 18:56:08 +00:00
|
|
|
return hasValue() ? getValue() : std::forward<U>(value);
|
|
|
|
}
|
|
|
|
|
2012-11-30 11:04:18 +00:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCE_THIS
|
2013-02-20 00:26:04 +00:00
|
|
|
T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
|
|
|
|
T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
|
2014-09-29 18:56:08 +00:00
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
T getValueOr(U &&value) && {
|
|
|
|
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
|
|
|
|
}
|
2012-11-30 00:38:53 +00:00
|
|
|
#endif
|
2010-04-09 20:25:54 +00:00
|
|
|
};
|
|
|
|
|
2013-02-22 18:58:26 +00:00
|
|
|
template <typename T> struct isPodLike;
|
|
|
|
template <typename T> struct isPodLike<Optional<T> > {
|
|
|
|
// An Optional<T> is pod-like if T is.
|
|
|
|
static const bool value = isPodLike<T>::value;
|
|
|
|
};
|
|
|
|
|
2011-02-11 18:13:20 +00:00
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator==(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator!=(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator<(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator<=(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator>=(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
|
|
|
/// \brief Poison comparison between two \c Optional objects. Clients needs to
|
|
|
|
/// explicitly compare the underlying values and account for empty \c Optional
|
|
|
|
/// objects.
|
|
|
|
///
|
2013-02-20 15:12:32 +00:00
|
|
|
/// This routine will never be defined. It returns \c void to help diagnose
|
2011-02-11 18:13:20 +00:00
|
|
|
/// errors at compile time.
|
|
|
|
template<typename T, typename U>
|
|
|
|
void operator>(const Optional<T> &X, const Optional<U> &Y);
|
|
|
|
|
2010-04-09 20:25:54 +00:00
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
#endif
|