-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Containers: STL compatibility for Optional.
- Loading branch information
Showing
10 changed files
with
373 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
This file is part of Corrade. | ||
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, | ||
2017, 2018, 2019 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 "Corrade/Containers/OptionalStl.h" | ||
|
||
using namespace Corrade; | ||
|
||
int main() { | ||
{ | ||
/* [Optional] */ | ||
std::optional<int> a{5}; | ||
Containers::Optional<int> b(a); | ||
|
||
std::optional<std::string> c(Containers::optional<std::string>("hello")); | ||
/* [Optional] */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#ifndef Corrade_Containers_PointerStl_h | ||
#define Corrade_Containers_PointerStl_h | ||
/* | ||
This file is part of Corrade. | ||
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, | ||
2017, 2018, 2019 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. | ||
*/ | ||
|
||
/** @file | ||
@brief STL compatibility for @ref Corrade::Containers::Optional | ||
Including this header allows you to *explicitly* convert between | ||
@ref Corrade::Containers::Optional and @cpp std::optional @ce using copy / move | ||
construction and assignment. See @ref Containers-Optional-stl for more | ||
information. | ||
*/ | ||
|
||
#include <optional> | ||
|
||
#include "Corrade/Containers/Optional.h" | ||
|
||
/* Listing these namespaces doesn't anything to the docs, so don't */ | ||
#ifndef DOXYGEN_GENERATING_OUTPUT | ||
namespace Corrade { namespace Containers { namespace Implementation { | ||
|
||
template<class T> struct OptionalConverter<std::optional<T>> { | ||
static Optional<T> from(const std::optional<T>& other) { | ||
return other ? Optional<T>{*other} : Containers::NullOpt; | ||
} | ||
|
||
static Optional<T> from(std::optional<T>&& other) { | ||
return other ? Optional<T>{*std::move(other)} : Containers::NullOpt; | ||
} | ||
|
||
static std::optional<T> to(const Optional<T>& other) { | ||
return other ? std::optional<T>{*other} : std::nullopt; | ||
} | ||
|
||
static std::optional<T> to(Optional<T>&& other) { | ||
return other ? std::optional<T>{*std::move(other)} : std::nullopt; | ||
} | ||
}; | ||
|
||
}}} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
/* | ||
This file is part of Corrade. | ||
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, | ||
2017, 2018, 2019 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 "Corrade/Containers/OptionalStl.h" | ||
#include "Corrade/Containers/Pointer.h" | ||
#include "Corrade/TestSuite/Tester.h" | ||
|
||
namespace Corrade { namespace Containers { namespace Test { namespace { | ||
|
||
struct OptionalStlTest: TestSuite::Tester { | ||
explicit OptionalStlTest(); | ||
|
||
void convertCopy(); | ||
void convertCopyNull(); | ||
void convertMove(); | ||
void convertMoveNull(); | ||
}; | ||
|
||
OptionalStlTest::OptionalStlTest() { | ||
addTests({&OptionalStlTest::convertCopy, | ||
&OptionalStlTest::convertCopyNull, | ||
&OptionalStlTest::convertMove, | ||
&OptionalStlTest::convertMoveNull}); | ||
} | ||
|
||
void OptionalStlTest::convertCopy() { | ||
std::optional<int> a = 5; | ||
CORRADE_VERIFY(a); | ||
CORRADE_COMPARE(*a, 5); | ||
|
||
Optional<int> b{a}; | ||
CORRADE_VERIFY(b); | ||
CORRADE_COMPARE(*b, 5); | ||
|
||
std::optional<int> c{b}; | ||
CORRADE_VERIFY(c); | ||
CORRADE_COMPARE(*c, 5); | ||
} | ||
|
||
void OptionalStlTest::convertCopyNull() { | ||
std::optional<int> a; | ||
CORRADE_VERIFY(!a); | ||
|
||
Optional<int> b(a); | ||
CORRADE_VERIFY(!b); | ||
|
||
std::optional<int> c(b); | ||
CORRADE_VERIFY(!c); | ||
} | ||
|
||
void OptionalStlTest::convertMove() { | ||
std::optional<Pointer<int>> a{pointer(new int{15})}; | ||
CORRADE_VERIFY(a); | ||
CORRADE_COMPARE(**a, 15); | ||
|
||
Optional<Pointer<int>> b(std::move(a)); | ||
CORRADE_VERIFY(b); | ||
CORRADE_VERIFY(!*a); | ||
CORRADE_COMPARE(**b, 15); | ||
|
||
std::optional<Pointer<int>> c(std::move(b)); | ||
CORRADE_VERIFY(c); | ||
CORRADE_VERIFY(!*b); | ||
CORRADE_COMPARE(**c, 15); | ||
} | ||
|
||
void OptionalStlTest::convertMoveNull() { | ||
std::optional<Pointer<int>> a; | ||
CORRADE_VERIFY(!a); | ||
|
||
Optional<Pointer<int>> b(std::move(a)); | ||
CORRADE_VERIFY(!b); | ||
|
||
std::optional<Pointer<int>> c(std::move(b)); | ||
CORRADE_VERIFY(!c); | ||
} | ||
|
||
}}}} | ||
|
||
CORRADE_TEST_MAIN(Corrade::Containers::Test::OptionalStlTest) |
Oops, something went wrong.