Add SIZEOF_MEMBER.

Bug: N/A
Test: ran tests
Change-Id: Icb7e8ae83d242c867b71a990b08eb1a62ed1482c
This commit is contained in:
Elliott Hughes 2018-04-06 17:45:22 -07:00
parent 1b2f2ee621
commit 1b86d41c78
3 changed files with 35 additions and 0 deletions

View file

@ -124,6 +124,7 @@ cc_test {
"errors_test.cpp",
"file_test.cpp",
"logging_test.cpp",
"macros_test.cpp",
"parsedouble_test.cpp",
"parseint_test.cpp",
"parsenetaddress_test.cpp",

View file

@ -20,6 +20,8 @@
#include <stddef.h> // for size_t
#include <unistd.h> // for TEMP_FAILURE_RETRY
#include <utility>
// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(exp) \
@ -114,6 +116,8 @@ char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting)
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
#define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f)
// Changing this definition will cause you a lot of pain. A majority of
// vendor code defines LIKELY and UNLIKELY this way, and includes
// this header through an indirect path.

30
base/macros_test.cpp Normal file
View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 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.
*/
#include "android-base/macros.h"
#include <stdint.h>
#include <gtest/gtest.h>
TEST(macros, SIZEOF_MEMBER_macro) {
struct S {
int32_t i32;
double d;
};
ASSERT_EQ(4U, SIZEOF_MEMBER(S, i32));
ASSERT_EQ(8U, SIZEOF_MEMBER(S, d));
}