From 3c33cbde8cac23dcab004c07effcf4b7f6fe4eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 23 Oct 2023 17:50:59 +0200 Subject: [PATCH 1/3] Add failing test that Attribute constructors work properly --- test/CoreTest.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/CoreTest.cpp b/test/CoreTest.cpp index d660e29ec4..f6f93e065c 100644 --- a/test/CoreTest.cpp +++ b/test/CoreTest.cpp @@ -47,6 +47,14 @@ TEST_CASE("attribute_dtype_test", "[core]") { Attribute a = Attribute(static_cast(' ')); REQUIRE(Datatype::CHAR == a.dtype); + { + // check that copy constructor works + [[maybe_unused]] Attribute b = a; + } + { + // check that move constructor works + [[maybe_unused]] Attribute b = std::move(a); + } a = Attribute(static_cast(' ')); REQUIRE(Datatype::UCHAR == a.dtype); a = Attribute(static_cast(0)); From c44abf501b140e3e8c166885ca1869c177a5ab05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 23 Oct 2023 17:53:41 +0200 Subject: [PATCH 2/3] Ensure that constructor doesnt override default constructors --- include/openPMD/backend/Attribute.hpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/openPMD/backend/Attribute.hpp b/include/openPMD/backend/Attribute.hpp index b2cee727ae..ecc445fff5 100644 --- a/include/openPMD/backend/Attribute.hpp +++ b/include/openPMD/backend/Attribute.hpp @@ -85,9 +85,9 @@ class Attribute std::vector, std::vector, std::vector, - std::vector >, - std::vector >, - std::vector >, + std::vector>, + std::vector>, + std::vector>, std::vector, std::vector, std::array, @@ -107,7 +107,14 @@ class Attribute * Fix by explicitly instantiating resource */ template - Attribute(T &&val) : Variant(resource(std::forward(val))) + Attribute(std::enable_if_t< + // If T is `Attribute` or `Attribute const &`, this constructor + // should not be used, but instead the move/copy constructors + !std::is_same_v< + std::remove_cv_t>, + Attribute>, + T> &&val) + : Variant(resource(std::forward(val))) {} /** Retrieve a stored specific Attribute and cast if convertible. From c18622fd4aea15c1f8fc4c927ed841a7cdd07727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Tue, 24 Oct 2023 11:27:41 +0200 Subject: [PATCH 3/3] Use macro instead --- include/openPMD/DatatypeMacros.hpp | 113 ++++++++++++++++++++++++ include/openPMD/UndefDatatypeMacros.hpp | 24 +++++ include/openPMD/backend/Attribute.hpp | 19 ++-- 3 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 include/openPMD/DatatypeMacros.hpp create mode 100644 include/openPMD/UndefDatatypeMacros.hpp diff --git a/include/openPMD/DatatypeMacros.hpp b/include/openPMD/DatatypeMacros.hpp new file mode 100644 index 0000000000..aa78879aab --- /dev/null +++ b/include/openPMD/DatatypeMacros.hpp @@ -0,0 +1,113 @@ +/* Copyright 2023 Franz Poeschel + * + * This file is part of openPMD-api. + * + * openPMD-api is free software: you can redistribute it and/or modify + * it under the terms of of either the GNU General Public License or + * the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * openPMD-api is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with openPMD-api. + * If not, see . + */ +#pragma once + +#include +#include +#include + +// Need to alias this to avoid the comma +// Cannot use a namespace, otherwise the macro will work either only within +// or outside the namespace +// Defining macros means polluting the global namespace anyway +using openpmd_array_double_7 = std::array; + +#define OPENPMD_FOREACH_DATATYPE(MACRO) \ + MACRO(char) \ + MACRO(unsigned char) \ + MACRO(signed char) \ + MACRO(short) \ + MACRO(int) \ + MACRO(long) \ + MACRO(long long) \ + MACRO(unsigned short) \ + MACRO(unsigned int) \ + MACRO(unsigned long) \ + MACRO(unsigned long long) \ + MACRO(float) \ + MACRO(double) \ + MACRO(long double) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::string) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(std::vector>) \ + MACRO(std::vector>) \ + MACRO(std::vector>) \ + MACRO(std::vector) \ + MACRO(std::vector) \ + MACRO(openpmd_array_double_7) \ + MACRO(bool) + +#define OPENPMD_FOREACH_NONVECTOR_DATATYPE(MACRO) \ + MACRO(char) \ + MACRO(unsigned char) \ + MACRO(signed char) \ + MACRO(short) \ + MACRO(int) \ + MACRO(long) \ + MACRO(long long) \ + MACRO(unsigned short) \ + MACRO(unsigned int) \ + MACRO(unsigned long) \ + MACRO(unsigned long long) \ + MACRO(float) \ + MACRO(double) \ + MACRO(long double) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::string) \ + MACRO(array_double_7) \ + MACRO(bool) + +#define OPENPMD_FOREACH_DATASET_DATATYPE(MACRO) \ + MACRO(char) \ + MACRO(unsigned char) \ + MACRO(signed char) \ + MACRO(short) \ + MACRO(int) \ + MACRO(long) \ + MACRO(long long) \ + MACRO(unsigned short) \ + MACRO(unsigned int) \ + MACRO(unsigned long) \ + MACRO(unsigned long long) \ + MACRO(float) \ + MACRO(double) \ + MACRO(long double) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::complex) \ + MACRO(std::array) diff --git a/include/openPMD/UndefDatatypeMacros.hpp b/include/openPMD/UndefDatatypeMacros.hpp new file mode 100644 index 0000000000..f55303ca11 --- /dev/null +++ b/include/openPMD/UndefDatatypeMacros.hpp @@ -0,0 +1,24 @@ +/* Copyright 2023 Franz Poeschel + * + * This file is part of openPMD-api. + * + * openPMD-api is free software: you can redistribute it and/or modify + * it under the terms of of either the GNU General Public License or + * the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * openPMD-api is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with openPMD-api. + * If not, see . + */ + +#undef OPENPMD_FOREACH_DATATYPE +#undef OPENPMD_FOREACH_NONVECTOR_DATATYPE +#undef OPENPMD_FOREACH_DATASET_DATATYPE diff --git a/include/openPMD/backend/Attribute.hpp b/include/openPMD/backend/Attribute.hpp index ecc445fff5..c34d7c1847 100644 --- a/include/openPMD/backend/Attribute.hpp +++ b/include/openPMD/backend/Attribute.hpp @@ -21,6 +21,7 @@ #pragma once #include "openPMD/Datatype.hpp" +#include "openPMD/DatatypeMacros.hpp" #include "openPMD/auxiliary/TypeTraits.hpp" #include "openPMD/auxiliary/Variant.hpp" @@ -106,17 +107,15 @@ class Attribute * * Fix by explicitly instantiating resource */ - template - Attribute(std::enable_if_t< - // If T is `Attribute` or `Attribute const &`, this constructor - // should not be used, but instead the move/copy constructors - !std::is_same_v< - std::remove_cv_t>, - Attribute>, - T> &&val) - : Variant(resource(std::forward(val))) + +#define OPENPMD_ATTRIBUTE_CONSTRUCTOR_FROM_VARIANT(TYPE) \ + Attribute(TYPE val) : Variant(resource(std::move(val))) \ {} + OPENPMD_FOREACH_DATATYPE(OPENPMD_ATTRIBUTE_CONSTRUCTOR_FROM_VARIANT) + +#undef OPENPMD_ATTRIBUTE_CONSTRUCTOR_FROM_VARIANT + /** Retrieve a stored specific Attribute and cast if convertible. * * @note This performs a static_cast and might introduce precision loss if @@ -304,3 +303,5 @@ std::optional Attribute::getOptional() const std::move(eitherValueOrError)); } } // namespace openPMD + +#include "openPMD/UndefDatatypeMacros.hpp"