Commit 916d7d93 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

extern access

Summary: A mechanism to make it easier conditionally to access possibly-defined names, which can be a bit tricky especially without `if constexpr`.

Reviewed By: luciang

Differential Revision: D27434753

fbshipit-source-id: 4c9aaebe89b354aa94f4f370c8fbdc8456b0e7f7
parent 6e9d4d69
...@@ -784,6 +784,7 @@ if (BUILD_TESTS) ...@@ -784,6 +784,7 @@ if (BUILD_TESTS)
TEST lang_cast_test SOURCES CastTest.cpp TEST lang_cast_test SOURCES CastTest.cpp
TEST lang_checked_math_test SOURCES CheckedMathTest.cpp TEST lang_checked_math_test SOURCES CheckedMathTest.cpp
TEST lang_exception_test SOURCES ExceptionTest.cpp TEST lang_exception_test SOURCES ExceptionTest.cpp
TEST lang_extern_test SOURCES ExternTest.cpp
TEST lang_launder_test SOURCES LaunderTest.cpp TEST lang_launder_test SOURCES LaunderTest.cpp
TEST lang_ordering_test SOURCES OrderingTest.cpp TEST lang_ordering_test SOURCES OrderingTest.cpp
TEST lang_pretty_test SOURCES PrettyTest.cpp TEST lang_pretty_test SOURCES PrettyTest.cpp
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <type_traits>
// FOLLY_CREATE_EXTERN_ACCESSOR
//
// Defines a variable template which holds the address of an object or null
// given a condition which identifies whether that object is expected to have
// a definition.
//
// Example:
//
// extern void foobar(int); // may be defined in another library
// inline constexpr bool has_foobar = // ...
//
// namespace myapp {
// FOLLY_CREATE_EXTERN_ACCESSOR(access_foobar_v, ::foobar);
// void try_foobar(int num) {
// if (auto ptr = access_foobar_v<has_foobar>) {
// ptr(num);
// }
// }
// }
//
// Remarks:
//
// This can be done more simply using weak symbols. But weak symbols are non-
// portable and the rules around the use of weak symbols are complex.
//
// This can be done more simply using if-constexpr. But if-constexpr requires
// C++17 and there remain specific narrow cases to be supported on prior
// versions of C++.
//
// TODO: Remove after C++17.
#define FOLLY_CREATE_EXTERN_ACCESSOR(varname, name) \
struct __folly_extern_accessor_##varname { \
template <bool E, typename N, ::std::enable_if_t<E, int> = 0> \
static constexpr N* get() noexcept { \
return &name; \
} \
template <bool E, typename N, ::std::enable_if_t<!E, int> = 0> \
static constexpr N* get() noexcept { \
return nullptr; \
} \
}; \
template <bool E, typename N = decltype(name)> \
FOLLY_INLINE_VARIABLE constexpr N* varname = \
__folly_extern_accessor_##varname::get<E, N>()
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/Extern.h>
#include <folly/portability/GTest.h>
namespace folly::extern_test {
extern int foobar(int);
}
static int hasdef(int a) {
return -a;
}
FOLLY_CREATE_EXTERN_ACCESSOR(access_foobar_v, folly::extern_test::foobar);
FOLLY_CREATE_EXTERN_ACCESSOR(access_hasdef_v, hasdef);
class ExternTest : public testing::Test {};
TEST_F(ExternTest, example) {
EXPECT_EQ(nullptr, access_foobar_v<false>);
EXPECT_EQ(nullptr, access_hasdef_v<false>);
EXPECT_EQ(&hasdef, access_hasdef_v<true>);
EXPECT_EQ(-17, access_hasdef_v<true>(17));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment