mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:14:43 +01:00
Provide a BisonWrap for default construction (#761)
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
367f9e4e94
commit
0619b4ce6a
@@ -0,0 +1,41 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_SYNTAX_BISON_WRAP_H_
|
||||
#define EXECUTABLE_SEMANTICS_SYNTAX_BISON_WRAP_H_
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "common/check.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Bison requires that types be default initializable for use with its variant
|
||||
// semantics. This wraps arbitrary types to support a default constructor, while
|
||||
// still requiring they be properly initialized.
|
||||
template <typename T>
|
||||
class BisonWrap {
|
||||
public:
|
||||
// Assigning a value initializes the wrapper.
|
||||
BisonWrap& operator=(T&& rhs) {
|
||||
val = std::move(rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Support transparent conversion to the wrapped type, erroring if not
|
||||
// initialized.
|
||||
operator T() {
|
||||
CHECK(val.has_value());
|
||||
T ret = std::move(*val);
|
||||
val.reset();
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<T> val;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_SYNTAX_BISON_WRAP_H_
|
||||
Reference in New Issue
Block a user