aboutsummaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-07-14 16:24:07 +0200
committerMax Kellermann <max@duempel.org>2014-07-14 16:24:07 +0200
commit7a1f3177c92556c4ca224d28fc881686845c5052 (patch)
tree158b962f07f6877ad33ebd6e590475fb6c0d6833 /src/util
parentf8da8b02615bc5b8a358407da147c97debc40e06 (diff)
downloadmpd-7a1f3177c92556c4ca224d28fc881686845c5052.tar.gz
mpd-7a1f3177c92556c4ca224d28fc881686845c5052.tar.xz
mpd-7a1f3177c92556c4ca224d28fc881686845c5052.zip
util/Cast: reimplement as template without macro
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Cast.hxx28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/util/Cast.hxx b/src/util/Cast.hxx
index 8eb3479b8..680f81704 100644
--- a/src/util/Cast.hxx
+++ b/src/util/Cast.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Max Kellermann <max@duempel.org>
+ * Copyright (C) 2013-2014 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -64,11 +64,31 @@ OffsetCast(const U *p, ptrdiff_t offset)
return reinterpret_cast<const T *>(OffsetPointer(p, offset));
}
+template<class C, class A>
+static constexpr inline ptrdiff_t
+ContainerAttributeOffset(const C *null_c, const A C::*p)
+{
+ return ptrdiff_t((const char *)null_c - (const char *)&(null_c->*p));
+}
+
+template<class C, class A>
+static constexpr inline ptrdiff_t
+ContainerAttributeOffset(const A C::*p)
+{
+ return ContainerAttributeOffset<C, A>(nullptr, p);
+}
+
/**
* Cast the given pointer to a struct member to its parent structure.
*/
-#define ContainerCast(p, container, attribute) \
- OffsetCast<container, decltype(((container*)nullptr)->attribute)> \
- ((p), -ptrdiff_t(offsetof(container, attribute)))
+template<class C, class A>
+#if defined(__clang__) || GCC_CHECK_VERSION(4,7)
+constexpr
+#endif
+static inline C &
+ContainerCast(A &a, A C::*member)
+{
+ return *OffsetCast<C, A>(&a, ContainerAttributeOffset<C, A>(member));
+}
#endif