mirror of
https://github.com/falk-werner/webfuse
synced 2024-10-27 20:34:10 +00:00
adds container_of
This commit is contained in:
parent
87735c3ee2
commit
7069478408
@ -272,6 +272,7 @@ pkg_check_modules(GMOCK gmock)
|
||||
add_executable(alltests
|
||||
test/msleep.cc
|
||||
test/mock_authenticator.cc
|
||||
test/test_container_of.cc
|
||||
test/test_response_parser.cc
|
||||
test/test_server.cc
|
||||
test/test_timepoint.cc
|
||||
|
16
lib/webfuse/core/container_of.h
Normal file
16
lib/webfuse/core/container_of.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef WF_CONTAINER_OF_H
|
||||
#define WF_CONTAINER_OF_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#define WF_CONTAINER_OF(pointer, type, member) \
|
||||
({ \
|
||||
const typeof( ((type *)0)->member ) *__mptr = (pointer); \
|
||||
(type *)( (char *)__mptr - offsetof(type, member) ); \
|
||||
})
|
||||
|
||||
#endif
|
29
test/test_container_of.cc
Normal file
29
test/test_container_of.cc
Normal file
@ -0,0 +1,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/core/container_of.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct MyStruct
|
||||
{
|
||||
int first;
|
||||
int second;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
TEST(ContainerOf, FirstMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * first = &my_struct.first;
|
||||
ASSERT_EQ(&my_struct, WF_CONTAINER_OF(first, MyStruct, first));
|
||||
}
|
||||
|
||||
TEST(ContainerOf, SecondMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * second = &my_struct.second;
|
||||
ASSERT_EQ(&my_struct, WF_CONTAINER_OF(second, MyStruct, second));
|
||||
}
|
Loading…
Reference in New Issue
Block a user