util/Cast: reimplement as template without macro

This commit is contained in:
Max Kellermann 2014-07-14 16:24:07 +02:00
parent f8da8b0261
commit 7a1f3177c9
3 changed files with 33 additions and 16 deletions

View File

@ -152,19 +152,13 @@ public:
HttpdOutput(EventLoop &_loop); HttpdOutput(EventLoop &_loop);
~HttpdOutput(); ~HttpdOutput();
#if GCC_CHECK_VERSION(4,6) || defined(__clang__) #if defined(__clang__) || GCC_CHECK_VERSION(4,7)
#pragma GCC diagnostic push constexpr
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#endif #endif
static HttpdOutput *Cast(AudioOutput *ao) {
static constexpr HttpdOutput *Cast(AudioOutput *ao) { return &ContainerCast(*ao, &HttpdOutput::base);
return ContainerCast(ao, HttpdOutput, base);
} }
#if GCC_CHECK_VERSION(4,6) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
using DeferredMonitor::GetEventLoop; using DeferredMonitor::GetEventLoop;
bool Init(const config_param &param, Error &error); bool Init(const config_param &param, Error &error);

View File

@ -87,10 +87,13 @@ calc_hash(TagType type, const char *p)
return hash ^ type; return hash ^ type;
} }
static inline constexpr TagPoolSlot * #if defined(__clang__) || GCC_CHECK_VERSION(4,7)
constexpr
#endif
static inline TagPoolSlot *
tag_item_to_slot(TagItem *item) tag_item_to_slot(TagItem *item)
{ {
return ContainerCast(item, TagPoolSlot, item); return &ContainerCast(*item, &TagPoolSlot::item);
} }
static inline TagPoolSlot ** static inline TagPoolSlot **

View File

@ -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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * 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)); 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. * Cast the given pointer to a struct member to its parent structure.
*/ */
#define ContainerCast(p, container, attribute) \ template<class C, class A>
OffsetCast<container, decltype(((container*)nullptr)->attribute)> \ #if defined(__clang__) || GCC_CHECK_VERSION(4,7)
((p), -ptrdiff_t(offsetof(container, attribute))) constexpr
#endif
static inline C &
ContainerCast(A &a, A C::*member)
{
return *OffsetCast<C, A>(&a, ContainerAttributeOffset<C, A>(member));
}
#endif #endif