refactor!: bump to C++17 and C17
Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
parent
e78c7af715
commit
c666c3e251
@ -29,10 +29,10 @@ set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${PROJECT_BINARY_DIR}/jars)
|
||||
######## Set compiler flags ########
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED true)
|
||||
set(CMAKE_C_STANDARD_REQUIRED true)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
include(GenerateExportHeader)
|
||||
set(CMAKE_CXX_FLAGS "-Wall -pedantic -D_GLIBCXX_USE_CXX11_ABI=0 -fstack-protector-strong --param=ssp-buffer-size=4 ${CMAKE_CXX_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "-Wall -pedantic -fstack-protector-strong --param=ssp-buffer-size=4 ${CMAKE_CXX_FLAGS}")
|
||||
if(UNIX AND APPLE)
|
||||
set(CMAKE_CXX_FLAGS "-stdlib=libc++ ${CMAKE_CXX_FLAGS}")
|
||||
endif()
|
||||
@ -319,7 +319,6 @@ endif()
|
||||
add_subdirectory(libraries/rainbow) # Qt extension for colors
|
||||
add_subdirectory(libraries/LocalPeer) # fork of a library from Qt solutions
|
||||
add_subdirectory(libraries/classparser) # class parser library
|
||||
add_subdirectory(libraries/optional-bare)
|
||||
add_subdirectory(libraries/tomlc99) # toml parser
|
||||
add_subdirectory(libraries/katabasis) # An OAuth2 library that tried to do too much
|
||||
add_subdirectory(libraries/gamemode)
|
||||
|
28
COPYING.md
28
COPYING.md
@ -295,34 +295,6 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
# optional-bare
|
||||
|
||||
Code from https://github.com/martinmoene/optional-bare/
|
||||
|
||||
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.
|
||||
|
||||
# tomlc99
|
||||
|
||||
MIT License
|
||||
|
@ -987,7 +987,6 @@ target_link_libraries(Launcher_logic
|
||||
Launcher_murmur2
|
||||
nbt++
|
||||
${ZLIB_LIBRARIES}
|
||||
optional-bare
|
||||
tomlc99
|
||||
BuildConfig
|
||||
Katabasis
|
||||
|
@ -44,7 +44,7 @@
|
||||
#include "QObjectPtr.h"
|
||||
#include "modplatform/flame/PackManifest.h"
|
||||
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
class QuaZip;
|
||||
namespace Flame
|
||||
@ -90,8 +90,8 @@ private: /* data */
|
||||
QString m_archivePath;
|
||||
bool m_downloadRequired = false;
|
||||
std::unique_ptr<QuaZip> m_packZip;
|
||||
QFuture<nonstd::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<nonstd::optional<QStringList>> m_extractFutureWatcher;
|
||||
QFuture<std::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<std::optional<QStringList>> m_extractFutureWatcher;
|
||||
QVector<Flame::File> m_blockedMods;
|
||||
enum class ModpackType{
|
||||
Unknown,
|
||||
|
@ -267,7 +267,7 @@ bool MMCZip::findFilesInZip(QuaZip * zip, const QString & what, QStringList & re
|
||||
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
||||
std::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString & subdir, const QString &target)
|
||||
{
|
||||
QDir directory(target);
|
||||
QStringList extracted;
|
||||
@ -276,7 +276,7 @@ nonstd::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString &
|
||||
auto numEntries = zip->getEntriesCount();
|
||||
if(numEntries < 0) {
|
||||
qWarning() << "Failed to enumerate files in archive";
|
||||
return nonstd::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
else if(numEntries == 0) {
|
||||
qDebug() << "Extracting empty archives seems odd...";
|
||||
@ -285,7 +285,7 @@ nonstd::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString &
|
||||
else if (!zip->goToFirstFile())
|
||||
{
|
||||
qWarning() << "Failed to seek to first file in zip";
|
||||
return nonstd::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
do
|
||||
@ -322,7 +322,7 @@ nonstd::optional<QStringList> MMCZip::extractSubDir(QuaZip *zip, const QString &
|
||||
{
|
||||
qWarning() << "Failed to extract file" << original_name << "to" << absFilePath;
|
||||
JlCompress::removeFile(extracted);
|
||||
return nonstd::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
extracted.append(absFilePath);
|
||||
@ -340,7 +340,7 @@ bool MMCZip::extractRelFile(QuaZip *zip, const QString &file, const QString &tar
|
||||
}
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
||||
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
@ -351,13 +351,13 @@ nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString
|
||||
return QStringList();
|
||||
}
|
||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
||||
return nonstd::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, "", dir);
|
||||
}
|
||||
|
||||
// ours
|
||||
nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
||||
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip))
|
||||
@ -368,7 +368,7 @@ nonstd::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString
|
||||
return QStringList();
|
||||
}
|
||||
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();;
|
||||
return nonstd::nullopt;
|
||||
return std::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, subdir, dir);
|
||||
}
|
||||
|
@ -42,7 +42,7 @@
|
||||
#include <functional>
|
||||
|
||||
#include <quazip/JlCompress.h>
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
namespace MMCZip
|
||||
{
|
||||
@ -95,7 +95,7 @@ namespace MMCZip
|
||||
/**
|
||||
* Extract a subdirectory from an archive
|
||||
*/
|
||||
nonstd::optional<QStringList> extractSubDir(QuaZip *zip, const QString & subdir, const QString &target);
|
||||
std::optional<QStringList> extractSubDir(QuaZip *zip, const QString & subdir, const QString &target);
|
||||
|
||||
bool extractRelFile(QuaZip *zip, const QString & file, const QString &target);
|
||||
|
||||
@ -106,7 +106,7 @@ namespace MMCZip
|
||||
* \param dir The directory to extract to, the current directory if left empty.
|
||||
* \return The list of the full paths of the files extracted, empty on failure.
|
||||
*/
|
||||
nonstd::optional<QStringList> extractDir(QString fileCompressed, QString dir);
|
||||
std::optional<QStringList> extractDir(QString fileCompressed, QString dir);
|
||||
|
||||
/**
|
||||
* Extract a subdirectory from an archive
|
||||
@ -116,7 +116,7 @@ namespace MMCZip
|
||||
* \param dir The directory to extract to, the current directory if left empty.
|
||||
* \return The list of the full paths of the files extracted, empty on failure.
|
||||
*/
|
||||
nonstd::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir);
|
||||
std::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir);
|
||||
|
||||
/**
|
||||
* Extract a single file from an archive into a directory
|
||||
|
@ -53,12 +53,12 @@
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
using nonstd::optional;
|
||||
using nonstd::nullopt;
|
||||
using std::optional;
|
||||
using std::nullopt;
|
||||
|
||||
GameType::GameType(nonstd::optional<int> original):
|
||||
GameType::GameType(std::optional<int> original):
|
||||
original(original)
|
||||
{
|
||||
if(!original) {
|
||||
|
@ -16,11 +16,11 @@
|
||||
#pragma once
|
||||
#include <QFileInfo>
|
||||
#include <QDateTime>
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
struct GameType {
|
||||
GameType() = default;
|
||||
GameType (nonstd::optional<int> original);
|
||||
GameType (std::optional<int> original);
|
||||
|
||||
QString toTranslatedString() const;
|
||||
QString toLogString() const;
|
||||
@ -33,7 +33,7 @@ struct GameType {
|
||||
Adventure,
|
||||
Spectator
|
||||
} type = Unknown;
|
||||
nonstd::optional<int> original;
|
||||
std::optional<int> original;
|
||||
};
|
||||
|
||||
class World
|
||||
|
@ -46,7 +46,7 @@
|
||||
#include "minecraft/PackProfile.h"
|
||||
#include "meta/Version.h"
|
||||
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
namespace ATLauncher {
|
||||
|
||||
@ -131,8 +131,8 @@ private:
|
||||
Meta::VersionPtr minecraftVersion;
|
||||
QMap<QString, Meta::VersionPtr> componentsToInstall;
|
||||
|
||||
QFuture<nonstd::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<nonstd::optional<QStringList>> m_extractFutureWatcher;
|
||||
QFuture<std::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<std::optional<QStringList>> m_extractFutureWatcher;
|
||||
|
||||
QFuture<bool> m_modExtractFuture;
|
||||
QFutureWatcher<bool> m_modExtractFutureWatcher;
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include "net/NetJob.h"
|
||||
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
namespace LegacyFTB {
|
||||
|
||||
@ -46,8 +46,8 @@ private: /* data */
|
||||
shared_qobject_ptr<QNetworkAccessManager> m_network;
|
||||
bool abortable = false;
|
||||
std::unique_ptr<QuaZip> m_packZip;
|
||||
QFuture<nonstd::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<nonstd::optional<QStringList>> m_extractFutureWatcher;
|
||||
QFuture<std::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<std::optional<QStringList>> m_extractFutureWatcher;
|
||||
NetJob::Ptr netJobContainer;
|
||||
QString archivePath;
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
|
||||
#include <nonstd/optional>
|
||||
#include <optional>
|
||||
|
||||
namespace Technic {
|
||||
|
||||
@ -57,8 +57,8 @@ private:
|
||||
QString m_archivePath;
|
||||
NetJob::Ptr m_filesNetJob;
|
||||
std::unique_ptr<QuaZip> m_packZip;
|
||||
QFuture<nonstd::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<nonstd::optional<QStringList>> m_extractFutureWatcher;
|
||||
QFuture<std::optional<QStringList>> m_extractFuture;
|
||||
QFutureWatcher<std::optional<QStringList>> m_extractFutureWatcher;
|
||||
};
|
||||
|
||||
} // namespace Technic
|
||||
|
@ -155,14 +155,6 @@ Canonical implementation of the murmur2 hash, taken from [SMHasher](https://gith
|
||||
|
||||
Public domain (the author disclaimed the copyright).
|
||||
|
||||
## optional-bare
|
||||
|
||||
A simple single-file header-only version of a C++17-like optional for default-constructible, copyable types, for C++98 and later.
|
||||
|
||||
Imported from: https://github.com/martinmoene/optional-bare/commit/0bb1d183bcee1e854c4ea196b533252c51f98b81
|
||||
|
||||
Boost Software License - Version 1.0
|
||||
|
||||
## quazip
|
||||
|
||||
A zip manipulation library, forked for MultiMC's use.
|
||||
|
@ -1,5 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.9.4)
|
||||
project(optional-bare)
|
||||
|
||||
add_library(optional-bare INTERFACE)
|
||||
target_include_directories(optional-bare INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
@ -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.
|
@ -1,5 +0,0 @@
|
||||
# optional bare
|
||||
|
||||
A simple single-file header-only version of a C++17-like optional for default-constructible, copyable types, for C++98 and later.
|
||||
|
||||
Imported from: https://github.com/martinmoene/optional-bare/commit/0bb1d183bcee1e854c4ea196b533252c51f98b81
|
@ -1,508 +0,0 @@
|
||||
//
|
||||
// Copyright 2017-2019 by Martin Moene
|
||||
//
|
||||
// https://github.com/martinmoene/optional-bare
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef NONSTD_OPTIONAL_BARE_HPP
|
||||
#define NONSTD_OPTIONAL_BARE_HPP
|
||||
|
||||
#define optional_bare_MAJOR 1
|
||||
#define optional_bare_MINOR 1
|
||||
#define optional_bare_PATCH 0
|
||||
|
||||
#define optional_bare_VERSION optional_STRINGIFY(optional_bare_MAJOR) "." optional_STRINGIFY(optional_bare_MINOR) "." optional_STRINGIFY(optional_bare_PATCH)
|
||||
|
||||
#define optional_STRINGIFY( x ) optional_STRINGIFY_( x )
|
||||
#define optional_STRINGIFY_( x ) #x
|
||||
|
||||
// optional-bare configuration:
|
||||
|
||||
#define optional_OPTIONAL_DEFAULT 0
|
||||
#define optional_OPTIONAL_NONSTD 1
|
||||
#define optional_OPTIONAL_STD 2
|
||||
|
||||
#if !defined( optional_CONFIG_SELECT_OPTIONAL )
|
||||
# define optional_CONFIG_SELECT_OPTIONAL ( optional_HAVE_STD_OPTIONAL ? optional_OPTIONAL_STD : optional_OPTIONAL_NONSTD )
|
||||
#endif
|
||||
|
||||
// Control presence of exception handling (try and auto discover):
|
||||
|
||||
#ifndef optional_CONFIG_NO_EXCEPTIONS
|
||||
# if _MSC_VER
|
||||
# include <cstddef> // for _HAS_EXCEPTIONS
|
||||
# endif
|
||||
# if _MSC_VER
|
||||
# include <cstddef> // for _HAS_EXCEPTIONS
|
||||
# endif
|
||||
# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
|
||||
# define optional_CONFIG_NO_EXCEPTIONS 0
|
||||
# else
|
||||
# define optional_CONFIG_NO_EXCEPTIONS 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// C++ language version detection (C++20 is speculative):
|
||||
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
|
||||
|
||||
#ifndef optional_CPLUSPLUS
|
||||
# if defined(_MSVC_LANG ) && !defined(__clang__)
|
||||
# define optional_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
|
||||
# else
|
||||
# define optional_CPLUSPLUS __cplusplus
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define optional_CPP98_OR_GREATER ( optional_CPLUSPLUS >= 199711L )
|
||||
#define optional_CPP11_OR_GREATER ( optional_CPLUSPLUS >= 201103L )
|
||||
#define optional_CPP14_OR_GREATER ( optional_CPLUSPLUS >= 201402L )
|
||||
#define optional_CPP17_OR_GREATER ( optional_CPLUSPLUS >= 201703L )
|
||||
#define optional_CPP20_OR_GREATER ( optional_CPLUSPLUS >= 202000L )
|
||||
|
||||
// C++ language version (represent 98 as 3):
|
||||
|
||||
#define optional_CPLUSPLUS_V ( optional_CPLUSPLUS / 100 - (optional_CPLUSPLUS > 200000 ? 2000 : 1994) )
|
||||
|
||||
// Use C++17 std::optional if available and requested:
|
||||
|
||||
#if optional_CPP17_OR_GREATER && defined(__has_include )
|
||||
# if __has_include( <optional> )
|
||||
# define optional_HAVE_STD_OPTIONAL 1
|
||||
# else
|
||||
# define optional_HAVE_STD_OPTIONAL 0
|
||||
# endif
|
||||
#else
|
||||
# define optional_HAVE_STD_OPTIONAL 0
|
||||
#endif
|
||||
|
||||
#define optional_USES_STD_OPTIONAL ( (optional_CONFIG_SELECT_OPTIONAL == optional_OPTIONAL_STD) || ((optional_CONFIG_SELECT_OPTIONAL == optional_OPTIONAL_DEFAULT) && optional_HAVE_STD_OPTIONAL) )
|
||||
|
||||
//
|
||||
// Using std::optional:
|
||||
//
|
||||
|
||||
#if optional_USES_STD_OPTIONAL
|
||||
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace nonstd {
|
||||
|
||||
using std::in_place;
|
||||
using std::in_place_type;
|
||||
using std::in_place_index;
|
||||
using std::in_place_t;
|
||||
using std::in_place_type_t;
|
||||
using std::in_place_index_t;
|
||||
|
||||
using std::optional;
|
||||
using std::bad_optional_access;
|
||||
using std::hash;
|
||||
|
||||
using std::nullopt;
|
||||
using std::nullopt_t;
|
||||
|
||||
using std::operator==;
|
||||
using std::operator!=;
|
||||
using std::operator<;
|
||||
using std::operator<=;
|
||||
using std::operator>;
|
||||
using std::operator>=;
|
||||
using std::make_optional;
|
||||
using std::swap;
|
||||
}
|
||||
|
||||
#else // optional_USES_STD_OPTIONAL
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#if ! optional_CONFIG_NO_EXCEPTIONS
|
||||
# include <stdexcept>
|
||||
#endif
|
||||
|
||||
namespace nonstd { namespace optional_bare {
|
||||
|
||||
// type for nullopt
|
||||
|
||||
struct nullopt_t
|
||||
{
|
||||
struct init{};
|
||||
nullopt_t( init ) {}
|
||||
};
|
||||
|
||||
// extra parenthesis to prevent the most vexing parse:
|
||||
|
||||
const nullopt_t nullopt(( nullopt_t::init() ));
|
||||
|
||||
// optional access error.
|
||||
|
||||
#if ! optional_CONFIG_NO_EXCEPTIONS
|
||||
|
||||
class bad_optional_access : public std::logic_error
|
||||
{
|
||||
public:
|
||||
explicit bad_optional_access()
|
||||
: logic_error( "bad optional access" ) {}
|
||||
};
|
||||
|
||||
#endif // optional_CONFIG_NO_EXCEPTIONS
|
||||
|
||||
// Simplistic optional: requires T to be default constructible, copyable.
|
||||
|
||||
template< typename T >
|
||||
class optional
|
||||
{
|
||||
private:
|
||||
typedef void (optional::*safe_bool)() const;
|
||||
|
||||
public:
|
||||
typedef T value_type;
|
||||
|
||||
optional()
|
||||
: has_value_( false )
|
||||
{}
|
||||
|
||||
optional( nullopt_t )
|
||||
: has_value_( false )
|
||||
{}
|
||||
|
||||
optional( T const & arg )
|
||||
: has_value_( true )
|
||||
, value_ ( arg )
|
||||
{}
|
||||
|
||||
template< class U >
|
||||
optional( optional<U> const & other )
|
||||
: has_value_( other.has_value() )
|
||||
, value_ ( other.value() )
|
||||
{}
|
||||
|
||||
optional & operator=( nullopt_t )
|
||||
{
|
||||
reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template< class U >
|
||||
optional & operator=( optional<U> const & other )
|
||||
{
|
||||
has_value_ = other.has_value();
|
||||
value_ = other.value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap( optional & rhs )
|
||||
{
|
||||
using std::swap;
|
||||
if ( has_value() == true && rhs.has_value() == true ) { swap( **this, *rhs ); }
|
||||
else if ( has_value() == false && rhs.has_value() == true ) { initialize( *rhs ); rhs.reset(); }
|
||||
else if ( has_value() == true && rhs.has_value() == false ) { rhs.initialize( **this ); reset(); }
|
||||
}
|
||||
|
||||
// observers
|
||||
|
||||
value_type const * operator->() const
|
||||
{
|
||||
return assert( has_value() ),
|
||||
&value_;
|
||||
}
|
||||
|
||||
value_type * operator->()
|
||||
{
|
||||
return assert( has_value() ),
|
||||
&value_;
|
||||
}
|
||||
|
||||
value_type const & operator*() const
|
||||
{
|
||||
return assert( has_value() ),
|
||||
value_;
|
||||
}
|
||||
|
||||
value_type & operator*()
|
||||
{
|
||||
return assert( has_value() ),
|
||||
value_;
|
||||
}
|
||||
|
||||
#if optional_CPP11_OR_GREATER
|
||||
explicit operator bool() const
|
||||
{
|
||||
return has_value();
|
||||
}
|
||||
#else
|
||||
operator safe_bool() const
|
||||
{
|
||||
return has_value() ? &optional::this_type_does_not_support_comparisons : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool has_value() const
|
||||
{
|
||||
return has_value_;
|
||||
}
|
||||
|
||||
value_type const & value() const
|
||||
{
|
||||
#if optional_CONFIG_NO_EXCEPTIONS
|
||||
assert( has_value() );
|
||||
#else
|
||||
if ( ! has_value() )
|
||||
throw bad_optional_access();
|
||||
#endif
|
||||
return value_;
|
||||
}
|
||||
|
||||
value_type & value()
|
||||
{
|
||||
#if optional_CONFIG_NO_EXCEPTIONS
|
||||
assert( has_value() );
|
||||
#else
|
||||
if ( ! has_value() )
|
||||
throw bad_optional_access();
|
||||
#endif
|
||||
return value_;
|
||||
}
|
||||
|
||||
template< class U >
|
||||
value_type value_or( U const & v ) const
|
||||
{
|
||||
return has_value() ? value() : static_cast<value_type>( v );
|
||||
}
|
||||
|
||||
// modifiers
|
||||
|
||||
void reset()
|
||||
{
|
||||
has_value_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
void this_type_does_not_support_comparisons() const {}
|
||||
|
||||
template< typename V >
|
||||
void initialize( V const & value )
|
||||
{
|
||||
assert( ! has_value() );
|
||||
value_ = value;
|
||||
has_value_ = true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool has_value_;
|
||||
value_type value_;
|
||||
};
|
||||
|
||||
// Relational operators
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator==( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator!=( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return (!y) ? false : (!x) ? true : *x < *y;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return (y < x);
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<=( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return !(y < x);
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>=( optional<T> const & x, optional<U> const & y )
|
||||
{
|
||||
return !(x < y);
|
||||
}
|
||||
|
||||
// Comparison with nullopt
|
||||
|
||||
template< typename T >
|
||||
inline bool operator==( optional<T> const & x, nullopt_t )
|
||||
{
|
||||
return (!x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator==( nullopt_t, optional<T> const & x )
|
||||
{
|
||||
return (!x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator!=( optional<T> const & x, nullopt_t )
|
||||
{
|
||||
return bool(x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator!=( nullopt_t, optional<T> const & x )
|
||||
{
|
||||
return bool(x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator<( optional<T> const &, nullopt_t )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator<( nullopt_t, optional<T> const & x )
|
||||
{
|
||||
return bool(x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator<=( optional<T> const & x, nullopt_t )
|
||||
{
|
||||
return (!x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator<=( nullopt_t, optional<T> const & )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator>( optional<T> const & x, nullopt_t )
|
||||
{
|
||||
return bool(x);
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator>( nullopt_t, optional<T> const & )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator>=( optional<T> const &, nullopt_t )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
inline bool operator>=( nullopt_t, optional<T> const & x )
|
||||
{
|
||||
return (!x);
|
||||
}
|
||||
|
||||
// Comparison with T
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator==( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x == v : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator==( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v == *x : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator!=( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x != v : true;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator!=( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v != *x : true;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x < v : true;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v < *x : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<=( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x <= v : true;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator<=( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v <= *x : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x > v : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v > *x : true;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>=( optional<T> const & x, U const & v )
|
||||
{
|
||||
return bool(x) ? *x >= v : false;
|
||||
}
|
||||
|
||||
template< typename T, typename U >
|
||||
inline bool operator>=( U const & v, optional<T> const & x )
|
||||
{
|
||||
return bool(x) ? v >= *x : true;
|
||||
}
|
||||
|
||||
// Specialized algorithms
|
||||
|
||||
template< typename T >
|
||||
void swap( optional<T> & x, optional<T> & y )
|
||||
{
|
||||
x.swap( y );
|
||||
}
|
||||
|
||||
// Convenience function to create an optional.
|
||||
|
||||
template< typename T >
|
||||
inline optional<T> make_optional( T const & v )
|
||||
{
|
||||
return optional<T>( v );
|
||||
}
|
||||
|
||||
} // namespace optional-bare
|
||||
|
||||
using namespace optional_bare;
|
||||
|
||||
} // namespace nonstd
|
||||
|
||||
#endif // optional_USES_STD_OPTIONAL
|
||||
|
||||
#endif // NONSTD_OPTIONAL_BARE_HPP
|
Loading…
Reference in New Issue
Block a user