From: Yedidya Feldblum Date: Tue, 17 Oct 2017 03:10:35 +0000 (-0700) Subject: Make Range.h and FBString.h mutually independent X-Git-Tag: v2017.10.23.00~37 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=1d4de585b9bee671244190cf24df528c13b1fb06;p=folly.git Make Range.h and FBString.h mutually independent Summary: [Folly] Make `Range.h` and `FBString.h` mutually independent. This means that `Range` cannot directly know about `fbstring`, so any interactions between the two types must be indirected through templates. Motivation: `FBString.h` is a relatively heaviweight `#include` for things that need `Range.h` but which do not use `fbstring`. Reviewed By: ericniebler Differential Revision: D6062434 fbshipit-source-id: e2f21c33f482eadffd0a8679eff4ece59bab53e9 --- diff --git a/folly/FBString.h b/folly/FBString.h index 90728601..b566d119 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -2894,3 +2894,13 @@ FOLLY_POP_WARNING #undef FBSTRING_LIKELY #undef FBSTRING_UNLIKELY #undef FBSTRING_ASSERT + +#ifndef _LIBSTDCXX_FBSTRING +namespace folly { +template +struct IsSomeString; + +template <> +struct IsSomeString : std::true_type {}; +} // namespace folly +#endif diff --git a/folly/FixedString.h b/folly/FixedString.h index 1465070c..732ae6ac 100644 --- a/folly/FixedString.h +++ b/folly/FixedString.h @@ -790,29 +790,6 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase { return *this; } - /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** - * Conversion to folly::Range - * \return `Range{begin(), end()}` - */ - template < - class Iter, - class = typename std::enable_if< - std::is_convertible::value>::type> - FOLLY_CPP14_CONSTEXPR /* implicit */ operator Range() noexcept { - return {begin(), end()}; - } - - /** - * \overload - */ - template < - class Iter, - class = typename std::enable_if< - std::is_convertible::value>::type> - constexpr /* implicit */ operator Range() const noexcept { - return {begin(), end()}; - } - /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * Conversion to folly::Range * \return `Range{begin(), end()}` diff --git a/folly/Format-inl.h b/folly/Format-inl.h index 940224c2..60fdf245 100644 --- a/folly/Format-inl.h +++ b/folly/Format-inl.h @@ -931,7 +931,7 @@ template <> struct KeyFromStringPiece : public FormatTraitsBase { typedef fbstring key_type; static fbstring convert(StringPiece s) { - return s.toFbstring(); + return s.to(); } }; diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 4b0b8799..96de8add 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include diff --git a/folly/IPAddressV6.h b/folly/IPAddressV6.h index 23c19e50..f641bdcd 100644 --- a/folly/IPAddressV6.h +++ b/folly/IPAddressV6.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include diff --git a/folly/Range.h b/folly/Range.h index d1fa497c..ce3e1fe8 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -19,7 +19,6 @@ #pragma once -#include #include #include #include @@ -30,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -38,17 +38,6 @@ #include #include -// libc++ doesn't provide this header, nor does msvc -#ifdef FOLLY_HAVE_BITS_CXXCONFIG_H -// This file appears in two locations: inside fbcode and in the -// libstdc++ source code (when embedding fbstring as std::string). -// To aid in this schizophrenic use, two macros are defined in -// c++config.h: -// _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to -// gate use inside fbcode v. libstdc++ -#include -#endif - #include #include #include @@ -61,6 +50,15 @@ FOLLY_GCC_DISABLE_WARNING("-Wshadow") namespace folly { +/** + * Ubiquitous helper template for knowing what's a string. + */ +template +struct IsSomeString : std::false_type {}; + +template <> +struct IsSomeString : std::true_type {}; + template class Range; @@ -251,30 +249,58 @@ class Range : private boost::totally_ordered> { Range(const Range& other, size_type first, size_type length = npos) : Range(other.subpiece(first, length)) {} - template ::const_type = 0> - /* implicit */ Range(const fbstring& str) - : b_(str.data()), e_(b_ + str.size()) {} + template < + class Container, + class = typename std::enable_if< + std::is_same::value>::type, + class = decltype( + Iter(std::declval().data()), + Iter( + std::declval().data() + + std::declval().size()))> + /* implicit */ constexpr Range(Container const& container) + : b_(container.data()), e_(b_ + container.size()) {} - template ::const_type = 0> - Range(const fbstring& str, fbstring::size_type startFrom) { - if (UNLIKELY(startFrom > str.size())) { + template < + class Container, + class = typename std::enable_if< + std::is_same::value>::type, + class = decltype( + Iter(std::declval().data()), + Iter( + std::declval().data() + + std::declval().size()))> + Range(Container const& container, typename Container::size_type startFrom) { + auto const cdata = container.data(); + auto const csize = container.size(); + if (UNLIKELY(startFrom > csize)) { std::__throw_out_of_range("index out of range"); } - b_ = str.data() + startFrom; - e_ = str.data() + str.size(); + b_ = cdata + startFrom; + e_ = cdata + csize; } - template ::const_type = 0> + template < + class Container, + class = typename std::enable_if< + std::is_same::value>::type, + class = decltype( + Iter(std::declval().data()), + Iter( + std::declval().data() + + std::declval().size()))> Range( - const fbstring& str, - fbstring::size_type startFrom, - fbstring::size_type size) { - if (UNLIKELY(startFrom > str.size())) { + Container const& container, + typename Container::size_type startFrom, + typename Container::size_type size) { + auto const cdata = container.data(); + auto const csize = container.size(); + if (UNLIKELY(startFrom > csize)) { std::__throw_out_of_range("index out of range"); } - b_ = str.data() + startFrom; - if (str.size() - startFrom < size) { - e_ = str.data() + str.size(); + b_ = cdata + startFrom; + if (csize - startFrom < size) { + e_ = cdata + csize; } else { e_ = b_ + size; } @@ -448,19 +474,22 @@ class Range : private boost::totally_ordered> { assert(b_ < e_); return detail::value_before(e_); } - // Works only for Range and Range - std::string str() const { - return std::string(b_, size()); - } - std::string toString() const { - return str(); + + template + auto to() const + -> decltype(Tgt(std::declval(), std::declval())) { + return Tgt(b_, size()); } // Works only for Range and Range - fbstring fbstr() const { - return fbstring(b_, size()); + template + auto str() const + -> decltype(Tgt(std::declval(), std::declval())) { + return to(); } - fbstring toFbstring() const { - return fbstr(); + template + auto toString() const + -> decltype(Tgt(std::declval(), std::declval())) { + return to(); } const_range_type castToConst() const { @@ -1314,17 +1343,6 @@ struct hasher< } }; -/** - * Ubiquitous helper template for knowing what's a string - */ -template -struct IsSomeString { - enum { - value = - std::is_same::value || std::is_same::value - }; -}; - /** * _sp is a user-defined literal suffix to make an appropriate Range * specialization from a literal string. diff --git a/folly/ThreadName.cpp b/folly/ThreadName.cpp index 919a0112..ed11c10e 100644 --- a/folly/ThreadName.cpp +++ b/folly/ThreadName.cpp @@ -102,15 +102,20 @@ bool setThreadName(std::thread::id tid, StringPiece name) { #if !FOLLY_HAVE_PTHREAD || _WIN32 return false; #else - auto trimmedName = name.fbstr().substr(0, kMaxThreadNameLength - 1); + auto const piece = name.subpiece(0, kMaxThreadNameLength - 1); + auto const data = piece.data(); + auto const size = piece.size(); + char trimmedName[kMaxThreadNameLength]; + std::memcpy(trimmedName, data, size); + std::memset(trimmedName + size, 0, kMaxThreadNameLength - size); auto id = stdTidToPthreadId(tid); #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME - return 0 == pthread_setname_np(id, trimmedName.c_str()); + return 0 == pthread_setname_np(id, const_cast(trimmedName)); #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME // Since OS X 10.6 it is possible for a thread to set its own name, // but not that of some other thread. if (pthread_equal(pthread_self(), id)) { - return 0 == pthread_setname_np(trimmedName.c_str()); + return 0 == pthread_setname_np(const_cast(trimmedName)); } return false; #else