[base] Move container_of to own file
This commit is contained in:
parent
a5a53648c9
commit
6637a30618
24
base/container_of.h
Normal file
24
base/container_of.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef BASE_CONTAINER_OF_H
|
||||
#define BASE_CONTAINER_OF_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace base {
|
||||
template<class P, class M>
|
||||
size_t offsetof__(const M P::*member)
|
||||
{
|
||||
return (size_t) &( reinterpret_cast<P*>(0)->*member);
|
||||
}
|
||||
|
||||
template<class P, class M>
|
||||
P *container_of(M *ptr, M const P::*member)
|
||||
{
|
||||
return (P *)((char *)(ptr) - offsetof__(member));
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user