1// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "absl/types/bad_variant_access.h"
16
17#ifndef ABSL_HAVE_STD_VARIANT
18
19#include <cstdlib>
20#include <stdexcept>
21
22#include "absl/base/config.h"
23#include "absl/base/internal/raw_logging.h"
24
25namespace absl {
26
27//////////////////////////
28// [variant.bad.access] //
29//////////////////////////
30
31bad_variant_access::~bad_variant_access() = default;
32
33const char* bad_variant_access::what() const noexcept {
34 return "Bad variant access";
35}
36
37namespace variant_internal {
38
39void ThrowBadVariantAccess() {
40#ifdef ABSL_HAVE_EXCEPTIONS
41 throw bad_variant_access();
42#else
43 ABSL_RAW_LOG(FATAL, "Bad variant access");
44 abort(); // TODO(calabrese) Remove once RAW_LOG FATAL is noreturn.
45#endif
46}
47
48void Rethrow() {
49#ifdef ABSL_HAVE_EXCEPTIONS
50 throw;
51#else
52 ABSL_RAW_LOG(FATAL,
53 "Internal error in absl::variant implementation. Attempted to "
54 "rethrow an exception when building with exceptions disabled.");
55 abort(); // TODO(calabrese) Remove once RAW_LOG FATAL is noreturn.
56#endif
57}
58
59} // namespace variant_internal
60} // namespace absl
61
62#endif // ABSL_HAVE_STD_VARIANT
63