From 02d5b57ff472d5c429c46643a553f2fcb77950ba Mon Sep 17 00:00:00 2001 From: Franck W Date: Sun, 11 Apr 2021 15:58:08 +0200 Subject: [PATCH] Replace 0-sized array by templated base class that contains array or not --- include/mockutils/FakeObject.hpp | 116 ++++++++++++++++--------------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/include/mockutils/FakeObject.hpp b/include/mockutils/FakeObject.hpp index d2122631..bf4260d7 100644 --- a/include/mockutils/FakeObject.hpp +++ b/include/mockutils/FakeObject.hpp @@ -3,80 +3,84 @@ * Copyright (c) 2014 Eran Pe'er. * * This program is made available under the terms of the MIT License. - * + * * Created on Jun 8, 2014 */ #pragma once #include -#include "mockutils/VirtualTable.hpp" - -namespace fakeit { -// silent GCC compiler warning: iso c++ forbids zero-size array [-Wpedantic] -#ifdef __GNUG__ -#ifndef __clang__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" -#endif -#endif - -// silent MSC++ compiler warning: C4200: nonstandard extension used : zero-sized array in struct/union. -#ifdef _MSC_VER -#pragma warning( push ) -#pragma warning( disable : 4200 ) -#endif - - - template - class FakeObject { - - VirtualTable vtable; - static const size_t SIZE = sizeof(C) - sizeof(VirtualTable); - char instanceArea[SIZE ? SIZE : 0]; +#include "mockutils/VirtualTable.hpp" - FakeObject(FakeObject const &) = delete; // undefined - FakeObject &operator=(FakeObject const &) = delete; // undefined +namespace fakeit +{ + namespace details + { + template + class FakeObjectImpl + { + public: + void initializeDataMembersArea() + { + for (size_t i = 0; i < instanceAreaSize; ++i) + { + instanceArea[i] = (char) 0; + } + } + + protected: + VirtualTable vtable; + char instanceArea[instanceAreaSize]; + }; + + template + class FakeObjectImpl<0, C, BaseClasses...> + { + public: + void initializeDataMembersArea() + {} + + protected: + VirtualTable vtable; + }; + } // namespace details + + template + class FakeObject + : public details::FakeObjectImpl), C, BaseClasses...> + { + FakeObject(FakeObject const&) = delete; // undefined + FakeObject& operator=(FakeObject const&) = delete; // undefined public: - - FakeObject() : vtable() { - initializeDataMembersArea(); - } - - ~FakeObject() { - vtable.dispose(); + FakeObject() + { + this->initializeDataMembersArea(); } - void initializeDataMembersArea() { - for (size_t i = 0; i < SIZE; ++i) instanceArea[i] = (char) 0; + ~FakeObject() + { + this->vtable.dispose(); } - void setMethod(unsigned int index, void *method) { - vtable.setMethod(index, method); + void setMethod(unsigned int index, void* method) + { + this->vtable.setMethod(index, method); } - VirtualTable &getVirtualTable() { - return vtable; + VirtualTable& getVirtualTable() + { + return this->vtable; } - void setVirtualTable(VirtualTable &t) { - vtable = t; + void setVirtualTable(VirtualTable& t) + { + this->vtable = t; } - void setDtor(void *dtor) { - vtable.setDtor(dtor); + void setDtor(void* dtor) + { + this->vtable.setDtor(dtor); } }; - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#ifdef __GNUG__ -#ifndef __clang__ -#pragma GCC diagnostic pop -#endif -#endif - -} +} // namespace fakeit