From b595f703daa8df0f089f68d350f96e6c21dc1f43 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 1 Jul 2014 19:26:39 +0200 Subject: [PATCH] Added OffsetOf header and function Designed to replace the old offsetof macro Former-commit-id: faa45ebfd3c1f9cc817f6fccf5c94d2c1527349d --- include/Nazara/Core/OffsetOf.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 include/Nazara/Core/OffsetOf.hpp diff --git a/include/Nazara/Core/OffsetOf.hpp b/include/Nazara/Core/OffsetOf.hpp new file mode 100644 index 000000000..0393f7fc7 --- /dev/null +++ b/include/Nazara/Core/OffsetOf.hpp @@ -0,0 +1,24 @@ +// Copyright (C) 2014 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_OFFSETOF_HPP +#define NAZARA_OFFSETOF_HPP + +// Par "Jesse Good" de SO: +// http://stackoverflow.com/questions/12811330/c-compile-time-offsetof-inside-a-template?answertab=votes#tab-top + +template T NzImplGetClassType(M T::*); +template M NzImplGetMemberType(M T::*); + +template +constexpr std::size_t NzImplOffsetOf() +{ + return reinterpret_cast(&(((T*)0)->*M)); +} + +#define NzOffsetOf(type, member) NzImplOffsetOf() + +#endif // NAZARA_OFFSETOF_HPP