Skip to content

Commit c1061b7

Browse files
authored
carbon-copy of the optional-relevant src from abseil (#1083)
1 parent 582e4ba commit c1061b7

File tree

11 files changed

+1791
-0
lines changed

11 files changed

+1791
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
// http://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 <type_traits>
16+
17+
#include "absl/base/internal/inline_variable.h"
18+
#include "absl/base/internal/inline_variable_testing.h"
19+
20+
#include "gtest/gtest.h"
21+
22+
namespace absl {
23+
namespace inline_variable_testing_internal {
24+
namespace {
25+
26+
TEST(InlineVariableTest, Constexpr) {
27+
static_assert(inline_variable_foo.value == 5, "");
28+
static_assert(other_inline_variable_foo.value == 5, "");
29+
static_assert(inline_variable_int == 5, "");
30+
static_assert(other_inline_variable_int == 5, "");
31+
}
32+
33+
TEST(InlineVariableTest, DefaultConstructedIdentityEquality) {
34+
EXPECT_EQ(get_foo_a().value, 5);
35+
EXPECT_EQ(get_foo_b().value, 5);
36+
EXPECT_EQ(&get_foo_a(), &get_foo_b());
37+
}
38+
39+
TEST(InlineVariableTest, DefaultConstructedIdentityInequality) {
40+
EXPECT_NE(&inline_variable_foo, &other_inline_variable_foo);
41+
}
42+
43+
TEST(InlineVariableTest, InitializedIdentityEquality) {
44+
EXPECT_EQ(get_int_a(), 5);
45+
EXPECT_EQ(get_int_b(), 5);
46+
EXPECT_EQ(&get_int_a(), &get_int_b());
47+
}
48+
49+
TEST(InlineVariableTest, InitializedIdentityInequality) {
50+
EXPECT_NE(&inline_variable_int, &other_inline_variable_int);
51+
}
52+
53+
TEST(InlineVariableTest, FunPtrType) {
54+
static_assert(
55+
std::is_same<void(*)(),
56+
std::decay<decltype(inline_variable_fun_ptr)>::type>::value,
57+
"");
58+
}
59+
60+
} // namespace
61+
} // namespace inline_variable_testing_internal
62+
} // namespace absl
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// http://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/base/internal/inline_variable_testing.h"
16+
17+
namespace absl {
18+
namespace inline_variable_testing_internal {
19+
20+
const Foo& get_foo_a() { return inline_variable_foo; }
21+
22+
const int& get_int_a() { return inline_variable_int; }
23+
24+
} // namespace inline_variable_testing_internal
25+
} // namespace absl
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// http://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/base/internal/inline_variable_testing.h"
16+
17+
namespace absl {
18+
namespace inline_variable_testing_internal {
19+
20+
const Foo& get_foo_b() { return inline_variable_foo; }
21+
22+
const int& get_int_b() { return inline_variable_int; }
23+
24+
} // namespace inline_variable_testing_internal
25+
} // namespace absl
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// http://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+
16+
#ifndef ABSL_BASE_INTERNAL_IDENTITY_H_
17+
#define ABSL_BASE_INTERNAL_IDENTITY_H_
18+
19+
namespace absl {
20+
namespace internal {
21+
22+
template <typename T>
23+
struct identity {
24+
typedef T type;
25+
};
26+
27+
template <typename T>
28+
using identity_t = typename identity<T>::type;
29+
30+
} // namespace internal
31+
} // namespace absl
32+
33+
#endif // ABSL_BASE_INTERNAL_IDENTITY_H_
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
// http://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+
#ifndef ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
16+
#define ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
17+
18+
#include <type_traits>
19+
20+
#include "absl/base/internal/identity.h"
21+
22+
// File:
23+
// This file define a macro that allows the creation of or emulation of C++17
24+
// inline variables based on whether or not the feature is supported.
25+
26+
////////////////////////////////////////////////////////////////////////////////
27+
// Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init)
28+
//
29+
// Description:
30+
// Expands to the equivalent of an inline constexpr instance of the specified
31+
// `type` and `name`, initialized to the value `init`. If the compiler being
32+
// used is detected as supporting actual inline variables as a language
33+
// feature, then the macro expands to an actual inline variable definition.
34+
//
35+
// Requires:
36+
// `type` is a type that is usable in an extern variable declaration.
37+
//
38+
// Requires: `name` is a valid identifier
39+
//
40+
// Requires:
41+
// `init` is an expression that can be used in the following definition:
42+
// constexpr type name = init;
43+
//
44+
// Usage:
45+
//
46+
// // Equivalent to: `inline constexpr size_t variant_npos = -1;`
47+
// ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1);
48+
//
49+
// Differences in implementation:
50+
// For a direct, language-level inline variable, decltype(name) will be the
51+
// type that was specified along with const qualification, whereas for
52+
// emulated inline variables, decltype(name) may be different (in practice
53+
// it will likely be a reference type).
54+
////////////////////////////////////////////////////////////////////////////////
55+
56+
#ifdef __cpp_inline_variables
57+
58+
// Clang's -Wmissing-variable-declarations option erroneously warned that
59+
// inline constexpr objects need to be pre-declared. This has now been fixed,
60+
// but we will need to support this workaround for people building with older
61+
// versions of clang.
62+
//
63+
// Bug: https://bugs.llvm.org/show_bug.cgi?id=35862
64+
//
65+
// Note:
66+
// identity_t is used here so that the const and name are in the
67+
// appropriate place for pointer types, reference types, function pointer
68+
// types, etc..
69+
#if defined(__clang__)
70+
#define ABSL_INTERNAL_EXTERN_DECL(type, name) \
71+
extern const ::absl::internal::identity_t<type> name;
72+
#else // Otherwise, just define the macro to do nothing.
73+
#define ABSL_INTERNAL_EXTERN_DECL(type, name)
74+
#endif // defined(__clang__)
75+
76+
// See above comment at top of file for details.
77+
#define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \
78+
ABSL_INTERNAL_EXTERN_DECL(type, name) \
79+
inline constexpr ::absl::internal::identity_t<type> name = init
80+
81+
#else
82+
83+
// See above comment at top of file for details.
84+
//
85+
// Note:
86+
// identity_t is used here so that the const and name are in the
87+
// appropriate place for pointer types, reference types, function pointer
88+
// types, etc..
89+
#define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \
90+
template <class /*AbslInternalDummy*/ = void> \
91+
struct AbslInternalInlineVariableHolder##name { \
92+
static constexpr ::absl::internal::identity_t<var_type> kInstance = init; \
93+
}; \
94+
\
95+
template <class AbslInternalDummy> \
96+
constexpr ::absl::internal::identity_t<var_type> \
97+
AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \
98+
\
99+
static constexpr const ::absl::internal::identity_t<var_type>& \
100+
name = /* NOLINT */ \
101+
AbslInternalInlineVariableHolder##name<>::kInstance; \
102+
static_assert(sizeof(void (*)(decltype(name))) != 0, \
103+
"Silence unused variable warnings.")
104+
105+
#endif // __cpp_inline_variables
106+
107+
#endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// http://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+
#ifndef ABSL_BASE_INLINE_VARIABLE_TESTING_H_
16+
#define ABSL_BASE_INLINE_VARIABLE_TESTING_H_
17+
18+
#include "absl/base/internal/inline_variable.h"
19+
20+
namespace absl {
21+
namespace inline_variable_testing_internal {
22+
23+
struct Foo {
24+
int value = 5;
25+
};
26+
27+
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, inline_variable_foo, {});
28+
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, other_inline_variable_foo, {});
29+
30+
ABSL_INTERNAL_INLINE_CONSTEXPR(int, inline_variable_int, 5);
31+
ABSL_INTERNAL_INLINE_CONSTEXPR(int, other_inline_variable_int, 5);
32+
33+
ABSL_INTERNAL_INLINE_CONSTEXPR(void(*)(), inline_variable_fun_ptr, nullptr);
34+
35+
const Foo& get_foo_a();
36+
const Foo& get_foo_b();
37+
38+
const int& get_int_a();
39+
const int& get_int_b();
40+
41+
} // namespace inline_variable_testing_internal
42+
} // namespace absl
43+
44+
#endif // ABSL_BASE_INLINE_VARIABLE_TESTING_H_
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// http://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_optional_access.h"
16+
17+
#include <cstdlib>
18+
19+
#include "absl/base/config.h"
20+
#include "absl/base/internal/raw_logging.h"
21+
22+
namespace absl {
23+
24+
bad_optional_access::~bad_optional_access() = default;
25+
26+
const char* bad_optional_access::what() const noexcept {
27+
return "optional has no value";
28+
}
29+
30+
namespace optional_internal {
31+
32+
void throw_bad_optional_access() {
33+
#ifdef ABSL_HAVE_EXCEPTIONS
34+
throw bad_optional_access();
35+
#else
36+
ABSL_RAW_LOG(FATAL, "Bad optional access");
37+
abort();
38+
#endif
39+
}
40+
41+
} // namespace optional_internal
42+
} // namespace absl
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// http://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+
#ifndef ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_
16+
#define ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_
17+
18+
#include <stdexcept>
19+
20+
namespace absl {
21+
22+
class bad_optional_access : public std::exception {
23+
public:
24+
bad_optional_access() = default;
25+
~bad_optional_access() override;
26+
const char* what() const noexcept override;
27+
};
28+
29+
namespace optional_internal {
30+
31+
// throw delegator
32+
[[noreturn]] void throw_bad_optional_access();
33+
34+
} // namespace optional_internal
35+
} // namespace absl
36+
37+
#endif // ABSL_TYPES_BAD_OPTIONAL_ACCESS_H_

0 commit comments

Comments
 (0)