Access.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <initializer_list>
  18. #include <iterator>
  19. /**
  20. * include or backport:
  21. * * std::size
  22. * * std::empty
  23. * * std::data
  24. */
  25. #if __cpp_lib_nonmember_container_access >= 201411 || _MSC_VER
  26. namespace folly {
  27. /* using override */ using std::data;
  28. /* using override */ using std::empty;
  29. /* using override */ using std::size;
  30. } // namespace folly
  31. #else
  32. namespace folly {
  33. // mimic: std::size, C++17
  34. template <typename C>
  35. constexpr auto size(C const& c) -> decltype(c.size()) {
  36. return c.size();
  37. }
  38. template <typename T, std::size_t N>
  39. constexpr std::size_t size(T const (&)[N]) noexcept {
  40. return N;
  41. }
  42. // mimic: std::empty, C++17
  43. template <typename C>
  44. constexpr auto empty(C const& c) -> decltype(c.empty()) {
  45. return c.empty();
  46. }
  47. template <typename T, std::size_t N>
  48. constexpr bool empty(T const (&)[N]) noexcept {
  49. // while zero-length arrays are not allowed in the language, some compilers
  50. // may permit them in some cases
  51. return N == 0;
  52. }
  53. template <typename E>
  54. constexpr bool empty(std::initializer_list<E> il) noexcept {
  55. return il.size() == 0;
  56. }
  57. // mimic: std::data, C++17
  58. template <typename C>
  59. constexpr auto data(C& c) -> decltype(c.data()) {
  60. return c.data();
  61. }
  62. template <typename C>
  63. constexpr auto data(C const& c) -> decltype(c.data()) {
  64. return c.data();
  65. }
  66. template <typename T, std::size_t N>
  67. constexpr T* data(T (&a)[N]) noexcept {
  68. return a;
  69. }
  70. template <typename E>
  71. constexpr E const* data(std::initializer_list<E> il) noexcept {
  72. return il.begin();
  73. }
  74. } // namespace folly
  75. #endif