Skip to content

Commit

Permalink
✨ add address_of
Browse files Browse the repository at this point in the history
  • Loading branch information
zie87 committed Oct 6, 2023
1 parent 489471d commit d939b79
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/zll/memory/address_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef ZLL_MEMORY_ADDRESSOF_HPP
#define ZLL_MEMORY_ADDRESSOF_HPP

#include "zll/meta/enable_if.hpp"
#include "zll/meta/is_object.hpp"
#include "zll/utils/attribute_macros.hpp"

namespace zll {

template <typename T>
ZLL_FORCE_INLINE typename meta::enable_if<meta::is_object<T>::value, T*>::type addressof(T& arg) {
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(arg)));
}

template <typename T>
ZLL_FORCE_INLINE typename meta::enable_if<!meta::is_object<T>::value, T*>::type addressof(T& arg) {
return &arg;
}

} // namespace zll

#endif // ZLL_MEMORY_ADDRESSOF_HPP
13 changes: 13 additions & 0 deletions include/zll/utils/attribute_macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef ZLL_UTILS_ATTRIBUTEMACROS_HPP
#define ZLL_UTILS_ATTRIBUTEMACROS_HPP

#define ZLL_FORCE_INLINE inline __attribute__ ((__always_inline__))
#define ZLl_NEVER_INLINE __attribute__ ((__noinline__))

#endif // ZLL_UTILS_ATTRIBUTEMACROS_HPP
44 changes: 44 additions & 0 deletions tests/unit_tests/memory/address_of_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#include "zll/memory/address_of.hpp"

#include <utest.h>

// clang-format off
void test_fn0() {}
void test_fn1(int) {}
void test_fn2(int, int) {}
void test_fn3(int, int, int) {}
// clang-format on

UTEST(memory_address_of, scalar) {
float* ptr = new float();
float& ref = *ptr;

ASSERT_TRUE(zll::addressof(ref) == ptr);
delete ptr;
}

UTEST(memory_address_of, function) {
ASSERT_TRUE(zll::addressof(test_fn0) == &test_fn0);
ASSERT_TRUE(zll::addressof(test_fn1) == &test_fn1);
ASSERT_TRUE(zll::addressof(test_fn2) == &test_fn2);
ASSERT_TRUE(zll::addressof(test_fn3) == &test_fn3);
}

UTEST(memory_address_of, array) {
{
int arr[3] = {17, 12, 32};
int(*ptr)[3] = &arr;
ASSERT_TRUE(zll::addressof(arr) == ptr);
}
{
const char arr[3] = {17, 12, 32};
const char(*ptr)[3] = &arr;
ASSERT_TRUE(zll::addressof(arr) == ptr);
}
}
2 changes: 2 additions & 0 deletions tests/unit_tests/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

unit_tests_srcs = [
'memory/address_of_tests.cpp',

'meta/enable_if_tests.cpp',
'meta/integral_constant_tests.cpp',
'meta/is_arithmetic_tests.cpp',
Expand Down

0 comments on commit d939b79

Please sign in to comment.