Utilities.h 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef BENCHMARK_UTILITIES_H
  10. #define BENCHMARK_UTILITIES_H
  11. #include <cassert>
  12. #include <type_traits>
  13. #include "benchmark/benchmark.h"
  14. namespace UtilitiesInternal {
  15. template <class Container>
  16. auto HaveDataImpl(int) -> decltype((std::declval<Container&>().data(), std::true_type{}));
  17. template <class Container>
  18. auto HaveDataImpl(long) -> std::false_type;
  19. template <class T>
  20. using HasData = decltype(HaveDataImpl<T>(0));
  21. } // namespace UtilitiesInternal
  22. template <class Container, std::enable_if_t<UtilitiesInternal::HasData<Container>::value>* = nullptr>
  23. void DoNotOptimizeData(Container &c) { benchmark::DoNotOptimize(c.data()); }
  24. template <class Container, std::enable_if_t<!UtilitiesInternal::HasData<Container>::value>* = nullptr>
  25. void DoNotOptimizeData(Container &c) { benchmark::DoNotOptimize(&c); }
  26. #endif // BENCHMARK_UTILITIES_H