mirror of https://github.com/mosra/magnum.git
Browse Source
And removing the bundled std::optional implementation. This finally makes this library compatible with C++17. Since this would be a huge backwards-incompatible change that would make everyone angry, the following had to be done in case both CORRADE_BUILD_DEPRECATED and MAGNUM_BUILD_DEPRECATED is defined: * Under C++11 and C++14, Containers::Optional / Containers::NullOpt is aliased to std::optional / std::nullopt. This is no worse than the state before, when we also provided these symbols. * Under C++17, where standard <optional> header is available, Containers::Optional provides implicit conversion to it. Only one-way conversion is supported, as there was fortunately no Magnum API that took std::optional via parameter, and there might be some corner cases that this doesn't cover. The goal is to have all examples compiling with the old API, at least. * There's a new test especially for this, which checks that both the C++11 and C++17 ways of doing things work as they should. The typedef and conversion is marked as deprecated, so it will spit out many warnings to push users to upgrade. I hope I can completely remove this mess soon :/pull/225/head
23 changed files with 366 additions and 1098 deletions
@ -1,23 +0,0 @@ |
|||||||
Boost Software License - Version 1.0 - August 17th, 2003 |
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person or organization |
|
||||||
obtaining a copy of the software and accompanying documentation covered by |
|
||||||
this license (the "Software") to use, reproduce, display, distribute, |
|
||||||
execute, and transmit the Software, and to prepare derivative works of the |
|
||||||
Software, and to permit third-parties to whom the Software is furnished to |
|
||||||
do so, all subject to the following: |
|
||||||
|
|
||||||
The copyright notices in the Software and this entire statement, including |
|
||||||
the above license grant, this restriction and the following disclaimer, |
|
||||||
must be included in all copies of the Software, in whole or in part, and |
|
||||||
all derivative works of the Software, unless such copies or derivative |
|
||||||
works are solely in the form of machine-executable object code generated by |
|
||||||
a source language processor. |
|
||||||
|
|
||||||
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
|
||||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
|
||||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
||||||
DEALINGS IN THE SOFTWARE. |
|
||||||
@ -0,0 +1,64 @@ |
|||||||
|
#ifndef MagnumExternal_Optional_OptionalWrapper_hpp |
||||||
|
#define MagnumExternal_Optional_OptionalWrapper_hpp |
||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 |
||||||
|
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. |
||||||
|
*/ |
||||||
|
|
||||||
|
#if !defined(Corrade_Containers_Optional_h) || !defined(Corrade_Utility_Macros_h) || !defined(MAGNUM_BUILD_DEPRECATED) |
||||||
|
#error This file is not meant to be used directly. |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Cover your eyes. This will hurt. And wash your hands after. */ |
||||||
|
#ifdef MAGNUM_BUILD_DEPRECATED |
||||||
|
#ifdef __has_include |
||||||
|
#if __has_include(<optional>) && __cplusplus >= 201703L |
||||||
|
#define _MAGNUM_HAS_STD_OPTIONAL |
||||||
|
#include <optional> |
||||||
|
|
||||||
|
namespace Corrade { namespace Containers { namespace Implementation { |
||||||
|
template<class T> struct OptionalConverter<T, std::optional<T>> { |
||||||
|
CORRADE_DEPRECATED("use Corrade::Containers::Optional instead") static std::optional<T> to(const Optional<T>& other) { |
||||||
|
if(other) return std::optional<T>{*other}; |
||||||
|
else return std::nullopt; |
||||||
|
} |
||||||
|
CORRADE_DEPRECATED("use Corrade::Containers::Optional instead") static std::optional<T> to(Optional<T>&& other) { |
||||||
|
if(other) return std::optional<T>{std::move(*other)}; |
||||||
|
else return std::nullopt; |
||||||
|
} |
||||||
|
}; |
||||||
|
}}} |
||||||
|
#endif |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifdef _MAGNUM_HAS_STD_OPTIONAL |
||||||
|
#else |
||||||
|
namespace std { |
||||||
|
template<class T> using optional CORRADE_DEPRECATED_ALIAS("use Corrade::Containers::Optional instead") = Corrade::Containers::Optional<T>; |
||||||
|
|
||||||
|
constexpr CORRADE_DEPRECATED("use Corrade::Containers::NullOpt instead") Corrade::Containers::NullOptT nullopt{Corrade::Containers::NullOptT::Init{}}; |
||||||
|
} |
||||||
|
#endif |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
# |
||||||
|
# This file is part of Magnum. |
||||||
|
# |
||||||
|
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 |
||||||
|
# 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. |
||||||
|
# |
||||||
|
|
||||||
|
corrade_add_test(StdOptionalTest StdOptionalTest.cpp) |
||||||
|
target_include_directories(StdOptionalTest PRIVATE |
||||||
|
${PROJECT_SOURCE_DIR}/src |
||||||
|
${PROJECT_BINARY_DIR}/src) |
||||||
|
|
||||||
|
# Try using C++17 on newer compilers. Can't use VERSION_GREATER_EQUAL because |
||||||
|
# older CMake doesn't support these. No idea how to detect libc++ presence on |
||||||
|
# non-Apple platforms (or even version), so I'm giving up there, testing just |
||||||
|
# elsewhere. |
||||||
|
# |
||||||
|
# Compiler versions where std::optional is supported: |
||||||
|
# MSVC 2017 (trust me, I know) |
||||||
|
# GCC libstdc++ 7.1 (see https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#table.cxx17_status) |
||||||
|
# libc++ 4.0 (see https://launchpad.net/ubuntu/+source/libc++) |
||||||
|
# Apple Clang 9.0 correspons to LLVM 4.0 (see https://gist.github.com/yamaya/2924292#gistcomment-2289472) |
||||||
|
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.1") OR |
||||||
|
#(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.0") OR |
||||||
|
(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0") OR |
||||||
|
(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.10")) |
||||||
|
set_target_properties(StdOptionalTest PROPERTIES CORRADE_CXX_STANDARD 17) |
||||||
|
endif() |
||||||
@ -0,0 +1,84 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 |
||||||
|
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 <memory> |
||||||
|
#include <Corrade/Containers/Optional.h> |
||||||
|
#include <Corrade/TestSuite/Tester.h> |
||||||
|
|
||||||
|
#include "Magnum/Magnum.h" |
||||||
|
#include "MagnumExternal/Optional/OptionalWrapper.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Test { |
||||||
|
|
||||||
|
struct StdOptionalTest: TestSuite::Tester { |
||||||
|
explicit StdOptionalTest(); |
||||||
|
|
||||||
|
void conversion(); |
||||||
|
}; |
||||||
|
|
||||||
|
StdOptionalTest::StdOptionalTest() { |
||||||
|
addTests({&StdOptionalTest::conversion}); |
||||||
|
} |
||||||
|
|
||||||
|
#ifdef __GNUC__ |
||||||
|
#pragma GCC diagnostic push |
||||||
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
||||||
|
#elif defined(_MSC_VER) |
||||||
|
#pragma warning(push) |
||||||
|
#pragma warning(disable: 4996) |
||||||
|
#endif |
||||||
|
void StdOptionalTest::conversion() { |
||||||
|
Debug{} << "Using C++ version" << __cplusplus; |
||||||
|
#ifdef _MAGNUM_HAS_STD_OPTIONAL |
||||||
|
Debug{} << "Using a conversion to std::optional, C++17 should be present"; |
||||||
|
CORRADE_VERIFY(__cplusplus >= 201703L); |
||||||
|
#else |
||||||
|
Debug{} << "Using a typedef to std::optional, C++17 should not be present"; |
||||||
|
CORRADE_VERIFY(__cplusplus < 201703L); |
||||||
|
#endif |
||||||
|
|
||||||
|
Containers::Optional<int> a{5}; |
||||||
|
Containers::Optional<int> b; |
||||||
|
|
||||||
|
std::optional<int> sa = a; |
||||||
|
CORRADE_COMPARE(*sa, 5); |
||||||
|
std::optional<int> sb = b; |
||||||
|
CORRADE_VERIFY(!sb); |
||||||
|
std::optional<int> empty = std::nullopt; |
||||||
|
CORRADE_VERIFY(!empty); |
||||||
|
|
||||||
|
Containers::Optional<std::unique_ptr<int>> c{std::unique_ptr<int>{new int{7}}}; |
||||||
|
std::optional<std::unique_ptr<int>> sc = std::move(c); |
||||||
|
CORRADE_COMPARE(**sc, 7); |
||||||
|
} |
||||||
|
#ifdef __GNUC__ |
||||||
|
#pragma GCC diagnostic pop |
||||||
|
#elif defined(_MSC_VER) |
||||||
|
#pragma warning(pop) |
||||||
|
#endif |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::Test::StdOptionalTest) |
||||||
Loading…
Reference in new issue