Browse Source

Disallow default construction of tag types.

Caused ambiguous overload problems when using {} in function calls. The tags
should be always specified explicitly.
pull/122/merge
Vladimír Vondruš 11 years ago
parent
commit
d91cb8f9e8
  1. 4
      src/Magnum/Math/Matrix.h
  2. 2
      src/Magnum/Math/RectangularMatrix.h
  3. 21
      src/Magnum/Math/Tags.h
  4. 1
      src/Magnum/Math/Test/CMakeLists.txt
  5. 49
      src/Magnum/Math/Test/TagsTest.cpp
  6. 10
      src/Magnum/Tags.h
  7. 1
      src/Magnum/Test/CMakeLists.txt
  8. 48
      src/Magnum/Test/TagsTest.cpp

4
src/Magnum/Math/Matrix.h

@ -65,7 +65,7 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
enum ZeroType { Zero };
#else
CORRADE_DEPRECATED("use Math::ZeroInitT instead") typedef ZeroInitT ZeroType;
CORRADE_DEPRECATED("use Math::ZeroInit instead") constexpr static ZeroInitT Zero{};
CORRADE_DEPRECATED("use Math::ZeroInit instead") constexpr static ZeroInitT Zero{ZeroInitT::Init{}};
#endif
/**
@ -76,7 +76,7 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
enum IdentityType { Identity };
#else
CORRADE_DEPRECATED("use Math::IdentityInitT instead") typedef IdentityInitT IdentityType;
CORRADE_DEPRECATED("use Math::IdentityInit instead") constexpr static IdentityInitT Identity{};
CORRADE_DEPRECATED("use Math::IdentityInit instead") constexpr static IdentityInitT Identity{IdentityInitT::Init{}};
#endif
#endif
#endif

2
src/Magnum/Math/RectangularMatrix.h

@ -400,7 +400,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(ZeroInitT) and RectangularMatrix<cols, rows, T>::RectangularMatrix(NoInitT) */
/* MSVC 2015 can't handle {} here */
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, U): _data{Vector<rows, T>((static_cast<void>(sequence), U{}))...} {}
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, U): _data{Vector<rows, T>((static_cast<void>(sequence), U{typename U::Init{}}))...} {}
template<std::size_t ...sequence> constexpr Vector<DiagonalSize, T> diagonalInternal(Implementation::Sequence<sequence...>) const;

21
src/Magnum/Math/Tags.h

@ -47,7 +47,13 @@ typedef Corrade::Containers::NoInitT NoInitT;
Used to distinguish construction with all elements set to zero.
@see @ref ZeroInit
*/
struct ZeroInitT {};
/* Explicit constructor to avoid ambiguous calls when using {} */
struct ZeroInitT {
#ifndef DOXYGEN_GENERATING_OUTPUT
struct Init{};
constexpr explicit ZeroInitT(Init) {}
#endif
};
/**
@brief Identity initialization tag type
@ -55,7 +61,13 @@ struct ZeroInitT {};
Used to distinguish construction with transformation set to identity.
@see @ref IdentityInit
*/
struct IdentityInitT {};
/* Explicit constructor to avoid ambiguous calls when using {} */
struct IdentityInitT {
#ifndef DOXYGEN_GENERATING_OUTPUT
struct Init{};
constexpr explicit IdentityInitT(Init) {}
#endif
};
/**
@brief No initialization tag
@ -63,6 +75,7 @@ struct IdentityInitT {};
Use for construction with no initialization at all.
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
/* Explicit constructor to avoid ambiguous calls when using {} */
constexpr NoInitT NoInit{};
#else
using Corrade::Containers::NoInit;
@ -73,14 +86,14 @@ using Corrade::Containers::NoInit;
Use for construction with all elements set to zero.
*/
constexpr ZeroInitT ZeroInit{};
constexpr ZeroInitT ZeroInit{ZeroInitT::Init{}};
/**
@brief Identity initialization tag
Use for construction with transformation set to identity.
*/
constexpr IdentityInitT IdentityInit{};
constexpr IdentityInitT IdentityInit{IdentityInitT::Init{}};
}}

1
src/Magnum/Math/Test/CMakeLists.txt

@ -26,6 +26,7 @@
corrade_add_test(MathBoolVectorTest BoolVectorTest.cpp)
corrade_add_test(MathConstantsTest ConstantsTest.cpp)
corrade_add_test(MathFunctionsTest FunctionsTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathTagsTest TagsTest.cpp)
corrade_add_test(MathTypeTraitsTest TypeTraitsTest.cpp)
corrade_add_test(MathVectorTest VectorTest.cpp LIBRARIES MagnumMathTestLib)

49
src/Magnum/Math/Test/TagsTest.cpp

@ -0,0 +1,49 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "Magnum/Math/Tags.h"
#include "Corrade/TestSuite/Tester.h"
namespace Magnum { namespace Math { namespace Test {
struct TagsTest: Corrade::TestSuite::Tester {
explicit TagsTest();
void noDefaultConstructor();
};
TagsTest::TagsTest() {
addTests({&TagsTest::noDefaultConstructor});
}
void TagsTest::noDefaultConstructor() {
CORRADE_VERIFY(!std::is_default_constructible<NoInitT>::value);
CORRADE_VERIFY(!std::is_default_constructible<ZeroInitT>::value);
CORRADE_VERIFY(!std::is_default_constructible<IdentityInitT>::value);
}
}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::TagsTest)

10
src/Magnum/Tags.h

@ -37,14 +37,20 @@ namespace Magnum {
Used to distinguish construction without creating the underlying OpenGL object.
@see @ref NoCreate
*/
struct NoCreateT {};
/* Explicit constructor to avoid ambiguous calls when using {} */
struct NoCreateT {
#ifndef DOXYGEN_GENERATING_OUTPUT
struct Init{};
constexpr explicit NoCreateT(Init) {}
#endif
};
/**
@brief No creation tag
Use for construction without creating the underlying OpenGL object.
*/
constexpr NoCreateT NoCreate{};
constexpr NoCreateT NoCreate{NoCreateT::Init{}};
}

1
src/Magnum/Test/CMakeLists.txt

@ -39,6 +39,7 @@ corrade_add_test(ResourceManagerTest ResourceManagerTest.cpp LIBRARIES Magnum)
corrade_add_test(SamplerTest SamplerTest.cpp LIBRARIES Magnum)
corrade_add_test(ShaderTest ShaderTest.cpp LIBRARIES Magnum)
corrade_add_test(VersionTest VersionTest.cpp LIBRARIES Magnum)
corrade_add_test(TagsTest TagsTest.cpp)
add_library(ResourceManagerLocalInstanceTestLib ${SHARED_OR_STATIC} ResourceManagerLocalInstanceTestLib.cpp)
target_link_libraries(ResourceManagerLocalInstanceTestLib Magnum)

48
src/Magnum/Test/TagsTest.cpp

@ -0,0 +1,48 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "Magnum/Tags.h"
#include "Magnum/Magnum.h"
#include "Corrade/TestSuite/Tester.h"
namespace Magnum { namespace Test {
struct TagsTest: TestSuite::Tester {
explicit TagsTest();
void noDefaultConstructor();
};
TagsTest::TagsTest() {
addTests({&TagsTest::noDefaultConstructor});
}
void TagsTest::noDefaultConstructor() {
CORRADE_VERIFY(!std::is_default_constructible<NoCreateT>::value);
}
}}
CORRADE_TEST_MAIN(Magnum::Test::TagsTest)
Loading…
Cancel
Save