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