-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters