summaryrefslogtreecommitdiff
path: root/src/util-inl.h
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2014-05-27 23:31:31 +0200
committerFedor Indutny <fedor@indutny.com>2014-05-30 11:45:37 +0100
commit820aaf5b3d2728d900ba0ff8903d343840766912 (patch)
tree7d92e15e13be35090b64746efe5e05fc9e3aa735 /src/util-inl.h
parentc7b02034ef80313564c50c59c63a5b640c24e234 (diff)
downloadnode-new-820aaf5b3d2728d900ba0ff8903d343840766912.tar.gz
src: replace CONTAINER_OF with type-safe function
Replace the CONTAINER_OF macro with a template function that is as type-safe as a reinterpret_cast<> of an arbitrary pointer can be made. Signed-off-by: Fedor Indutny <fedor@indutny.com>
Diffstat (limited to 'src/util-inl.h')
-rw-r--r--src/util-inl.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util-inl.h b/src/util-inl.h
index 76015ccb3b..3709208f16 100644
--- a/src/util-inl.h
+++ b/src/util-inl.h
@@ -28,6 +28,26 @@
namespace node {
+template <typename Inner, typename Outer>
+ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field,
+ Inner* pointer)
+ : pointer_(reinterpret_cast<Outer*>(
+ reinterpret_cast<uintptr_t>(pointer) -
+ reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field)))) {
+}
+
+template <typename Inner, typename Outer>
+template <typename TypeName>
+ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
+ return static_cast<TypeName*>(pointer_);
+}
+
+template <typename Inner, typename Outer>
+inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
+ Inner* pointer) {
+ return ContainerOfHelper<Inner, Outer>(field, pointer);
+}
+
template <class TypeName>
inline v8::Local<TypeName> PersistentToLocal(
v8::Isolate* isolate,