Skip to content

Commit

Permalink
Move FOLLY_DEFINE_CPO to folly/lang/CustomizationPoint.h
Browse files Browse the repository at this point in the history
Summary:
[Folly] Move `FOLLY_DEFINE_CPO` to `folly/lang/CustomizationPoint.h`.

And move `StaticConst` to top-level `folly` namespace and into `folly/lang/`.

And Americanize the spellings of words in the comments.

Reviewed By: ericniebler, lewissbaker

Differential Revision: D14402315

fbshipit-source-id: e13dbc9d471ee73ba0a289d375c235734334b920
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Mar 12, 2019
1 parent 4008ab2 commit ca91b0e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
25 changes: 0 additions & 25 deletions folly/Portability.h
Expand Up @@ -457,31 +457,6 @@ constexpr auto kCpplibVer = 0;
#define FOLLY_STORAGE_CONSTEXPR constexpr
#endif

// Helpers for portably defining customisation-point objects (CPOs).
//
// The customisation-point object must be placed in a nested namespace to
// avoid potential conflicts with customisations defined as friend-functions
// of types defined in the same namespace as the CPO.
//
// In C++17 and later we can define the object using 'inline constexpr' to
// avoid ODR issues. However, prior to that we need to use the StaticConst<T>
// helper to ensure that there is only a single instance of the CPO created
// and then we need to put a named reference to this object in an anonymous
// namespace to avoid duplicate symbol definitions.
#if __cpp_inline_variables >= 201606L
#define FOLLY_DEFINE_CPO(Type, Name) \
namespace __hidden { \
inline constexpr Type Name{}; \
} \
using namespace __hidden;
#else
#include <folly/detail/StaticConst.h>
#define FOLLY_DEFINE_CPO(Type, Name) \
namespace { \
constexpr auto& Name = ::folly::detail::StaticConst<Type>::value; \
}
#endif

#if __cpp_coroutines >= 201703L && __has_include(<experimental/coroutine>)
#define FOLLY_HAS_COROUTINES 1
#elif _MSC_VER && _RESUMABLE_FUNCTIONS_SUPPORTED
Expand Down
2 changes: 1 addition & 1 deletion folly/detail/PolyDetail.h
Expand Up @@ -25,10 +25,10 @@

#include <folly/Traits.h>
#include <folly/Utility.h>
#include <folly/detail/StaticConst.h>
#include <folly/detail/TypeList.h>
#include <folly/functional/Invoke.h>
#include <folly/lang/Exception.h>
#include <folly/lang/StaticConst.h>

#include <folly/PolyException.h>

Expand Down
1 change: 1 addition & 0 deletions folly/experimental/coro/ViaIfAsync.h
Expand Up @@ -22,6 +22,7 @@
#include <folly/Traits.h>
#include <folly/experimental/coro/Traits.h>
#include <folly/io/async/Request.h>
#include <folly/lang/CustomizationPoint.h>

#include <glog/logging.h>

Expand Down
45 changes: 45 additions & 0 deletions folly/lang/CustomizationPoint.h
@@ -0,0 +1,45 @@
/*
* Copyright 2019-present Facebook, Inc.
*
* 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 <folly/lang/StaticConst.h>

// FOLLY_DEFINE_CPO
//
// Helper for portably defining customization-point objects (CPOs).
//
// The customization-point object must be placed in a nested namespace to avoid
// potential conflicts with customizations defined as friend-functions of types
// defined in the same namespace as the CPO.
//
// In C++17 and later the object may be defined using 'inline constexpr' to
// avoid ODR issues. However, prior to that a helper template is required to
// ensure that there is only a single instance of the CPO created and then a
// named reference in an anonymous namespace is required to avoid duplicate
// symbol definitions.
#if __cpp_inline_variables >= 201606L
#define FOLLY_DEFINE_CPO(Type, Name) \
namespace folly_cpo__ { \
inline constexpr Type Name{}; \
} \
using namespace folly_cpo__;
#else
#define FOLLY_DEFINE_CPO(Type, Name) \
namespace { \
constexpr auto& Name = ::folly::StaticConst<Type>::value; \
}
#endif
10 changes: 3 additions & 7 deletions folly/detail/StaticConst.h → folly/lang/StaticConst.h
Expand Up @@ -17,14 +17,11 @@
#pragma once

namespace folly {
namespace detail {

// The StaticConst<T> class is a helper for defining constexpr objects at
// namespace scope in an ODR-safe and initialisation-order fiasco-safe way
// in C++ versions earlier than C++17.
// StaticConst
//
// For example, see the FOLLY_DEFINE_CPO() macro in folly/Portability.h for
// usage in defining customisation-point objects (CPOs).
// A template for defining ODR-usable constexpr instances. Safe from ODR
// violations and initialization-order problems.

template <typename T>
struct StaticConst {
Expand All @@ -34,5 +31,4 @@ struct StaticConst {
template <typename T>
constexpr T StaticConst<T>::value;

} // namespace detail
} // namespace folly

0 comments on commit ca91b0e

Please sign in to comment.