summaryrefslogtreecommitdiff
path: root/base/ArraySize.h
blob: 9650e6729a7815eae1bdd3e117107a6b05a6b674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2019 The Android Open Source Project
//
// 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

// This file defines macroes ARRAY_SIZE() and STRING_LITERAL_LENGTH for static
// array size and string literal length detection
// If used from C, it's just a standard division of sizes. In C++ though, it
// would give you a compile-time error if used with something other than
// a built-in array or std::array<>
// Also, C++ defines corresponding constexpr functions for the same purpose

#ifndef __cplusplus
#   define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#   define STRING_LITERAL_LENGTH(str) (ARRAY_SIZE(str) - 1))
#else

#include <array>
#include <stddef.h>

namespace android {
namespace base {

template <class T, size_t N>
static constexpr size_t arraySize(const T (&arr)[N]) {
    return N;
}

template <class T, size_t N>
static constexpr size_t arraySize(const std::array<T, N>& arr) {
    return N;
}

template <size_t N>
static constexpr size_t stringLiteralLength(const char (&str)[N]) {
    return N - 1;
}

}  // namespace base
}  // namespace android

// for those who like macros, define it to be a simple function call
#define ARRAY_SIZE(arr) (::android::base::arraySize(arr))
#define STRING_LITERAL_LENGTH(str) (::android::base::stringLiteralLength(str))

#endif  // __cplusplus