Commit 93f4fb77 authored by Eike Ziller's avatar Eike Ziller

Add std::experimental::optional as Utils::optional

Uses the reference implementation of the proposal, which later
can be replaced by the std lib implementation depending on compiler
and used C++ version.

Change-Id: I23f2f8077f4cb26c3d9a403b1ce438b6cdb163f2
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent 42580c7d
...@@ -274,6 +274,21 @@ http://llvm.org/docs/GettingStarted.html#git-mirror: ...@@ -274,6 +274,21 @@ http://llvm.org/docs/GettingStarted.html#git-mirror:
Qt Creator includes the following third-party components, Qt Creator includes the following third-party components,
we thank the authors who made this possible: we thank the authors who made this possible:
### Reference implementation for std::experimental::optional
https://github.com/akrzemi1/Optional
QtCreator/src/libs/3rdparty/optional
Copyright (C) 2011-2012 Andrzej Krzemienski
Distributed under the Boost Software License, Version 1.0
(see accompanying file LICENSE_1_0.txt or a copy at
http://www.boost.org/LICENSE_1_0.txt)
The idea and interface is based on Boost.Optional library
authored by Fernando Luis Cacciola Carballal
### Open Source front-end for C++ (license MIT), enhanced for use in Qt Creator ### Open Source front-end for C++ (license MIT), enhanced for use in Qt Creator
Roberto Raggi <roberto.raggi@gmail.com> Roberto Raggi <roberto.raggi@gmail.com>
......
...@@ -41,6 +41,24 @@ ...@@ -41,6 +41,24 @@
\QC contains the following third-party components: \QC contains the following third-party components:
\list \list
\li \b{Reference implementation for std::experimental::optional}
Copyright (C) 2011-2012 Andrzej Krzemienski
Distributed under the Boost Software License, Version 1.0
(see accompanying file LICENSE_1_0.txt or a copy at
http://www.boost.org/LICENSE_1_0.txt)
The idea and interface is based on Boost.Optional library
authored by Fernando Luis Cacciola Carballal
The source code of std::experimental::optional can be found
here:
\list
\li \l{https://github.com/akrzemi1/Optional}
\li QtCreator/src/libs/3rdparty/optional
\li \l{https://code.qt.io/cgit/qt-creator/qt-creator.git/tree/src/libs/3rdparty/optional}
\endlist
\li \b{Open Source front-end for C++ (license MIT)}, enhanced for use \li \b{Open Source front-end for C++ (license MIT)}, enhanced for use
in \QC.\br in \QC.\br
......
project(optional)
cmake_minimum_required(VERSION 2.8)
enable_testing()
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra")
add_executable(test_optional test_optional.cpp)
add_executable(test_type_traits test_type_traits.cpp)
add_test(test_optional test_optional)
add_test(test_type_traits test_type_traits)
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.
Optional
========
A single-header header-only library for representing optional (nullable) objects for C++14 (and C++11 to some extent) and passing them by value. This is the reference implementation of proposal N3793 (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3793.html). Optional is now accepted into Library Fundamentals Technical Specification (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3848.html). The interface is based on Fernando Cacciola's Boost.Optional library (see http://www.boost.org/doc/libs/1_52_0/libs/optional/doc/html/index.html)
Usage
-----
```cpp
optional<int> readInt(); // this function may return int or a not-an-int
if (optional<int> oi = readInt()) // did I get a real int
cout << "my int is: " << *oi; // use my int
else
cout << "I have no int";
```
For more usage examples and the overview see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3527.html
Supported compilers
-------------------
Clang 3.2, Clang 3.4, G++ 4.7.2, G++ 4.8.1. Tested only with libstdc++, versions 20130531, 20120920, 20110428. Others have reported it also works with libc++.
Known Issues
------------
- Currently, the reference (and the only known) implementation of certain pieces of functionality explore what C++11 identifies as undefined behavior (see national body comment FI 15: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3770.html#FI15). This is mostly why Optional was removed from C++14 and put into Library Fundamentals TS. Luckily what the Standard identifies as UB is well defined on all known platforms. We expect that the C++14 wil fix this problem, so that our trick becomes well-defined.
- In libstdc++ versions below 20130531 the constructor taking `initializer_list` argument is not `constexpr`. This is because `initializer_list` operations are not `constexpr` in C++11. This works however in version 20130531. It is also not enabled for libc++ because I do not have access to it and do nto know if it provides `constexpr` `initializer_list`.
- In G++ 4.7.2 and 4.8.0 member function `value_or` does not have rvalue reference overload. These compilers do not support rvalue overloding on `*this`.
- In order for the library to work with slightly older compilers, we emulate some missing type traits. On some platforms we cannot correctly detect the available features, and attempts at workarounds for missing type trait definitions can cause compile-time errors. Define macro `TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS` if you know that all the necessary type traits are defined, and no emulation is required.
License
-------
Distributed under the [Boost Software License, Version 1.0](http://www.boost.org/LICENSE_1_0.txt).
Copyright (C) 2011-2012 Andrzej Krzemienski
Distributed under the Boost Software License, Version 1.0
(see accompanying file LICENSE_1_0.txt or a copy at
http://www.boost.org/LICENSE_1_0.txt)
The idea and interface is based on Boost.Optional library
authored by Fernando Luis Cacciola Carballal
Home at https://github.com/akrzemi1/Optional
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
// Copyright (C) 2011 - 2012 Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if (defined __clang__)
namespace std { class type_info; }
#endif
# include "optional.hpp"
namespace std { namespace experimental {
struct Val
{
Val(){}
Val( Val const & ){}
Val( Val && ) noexcept {}
Val & operator=( Val const & ) = delete;
Val & operator=( Val && ) noexcept = delete;
};
struct Safe
{
Safe(){}
Safe( Safe const & ){}
Safe( Safe && ) noexcept {}
Safe & operator=( Safe const & ){ return *this; }
Safe & operator=( Safe && ) noexcept { return *this; }
};
struct Unsafe
{
Unsafe(){}
Unsafe( Unsafe const & ){}
Unsafe( Unsafe && ){}
Unsafe & operator=( Unsafe const & ){ return *this; }
Unsafe & operator=( Unsafe && ) { return *this; }
};
struct VoidNothrowBoth
{
VoidNothrowBoth(VoidNothrowBoth&&) noexcept(true) {};
void operator=(VoidNothrowBoth&&) noexcept(true) {}; // note void return type
};
static_assert(is_nothrow_move_constructible<Safe>::value, "WTF!");
static_assert(!is_nothrow_move_constructible<Unsafe>::value, "WTF!");
static_assert(is_assignable<Safe&, Safe&&>::value, "WTF!");
static_assert(!is_assignable<Val&, Val&&>::value, "WTF!");
static_assert(is_nothrow_move_assignable<Safe>::value, "WTF!");
static_assert(!is_nothrow_move_assignable<Unsafe>::value, "WTF!");
static_assert(is_nothrow_move_constructible<VoidNothrowBoth>::value, "WTF!");
static_assert(is_nothrow_move_assignable<VoidNothrowBoth>::value, "WTF!");
}} // namespace std::experimental
int main() { }
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
/*
optional<T>
make_optional(t)
See std(::experimental)::optional.
*/
// TODO: replace by #include <(experimental/)optional> depending on compiler and C++ version
#include <3rdparty/optional/optional.hpp>
namespace Utils {
// --> Utils::optional
using std::experimental::optional;
// --> Utils::nullopt
using std::experimental::nullopt;
// TODO: make_optional is a copy, since there is no sensible way to import functions in C++
template <class T>
constexpr optional<typename std::decay<T>::type> make_optional(T&& v)
{
return optional<typename std::decay<T>::type>(std::experimental::constexpr_forward<T>(v));
}
template <class X>
constexpr optional<X&> make_optional(std::reference_wrapper<X> v)
{
return optional<X&>(v.get());
}
} // Utils
...@@ -237,7 +237,9 @@ HEADERS += \ ...@@ -237,7 +237,9 @@ HEADERS += \
$$PWD/smallstringio.h \ $$PWD/smallstringio.h \
$$PWD/guard.h \ $$PWD/guard.h \
$$PWD/asconst.h \ $$PWD/asconst.h \
$$PWD/smallstringfwd.h $$PWD/smallstringfwd.h \
$$PWD/optional.h \
$$PWD/../3rdparty/optional/optional.hpp
FORMS += $$PWD/filewizardpage.ui \ FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \ $$PWD/projectintropage.ui \
......
...@@ -142,6 +142,8 @@ Project { ...@@ -142,6 +142,8 @@ Project {
"newclasswidget.cpp", "newclasswidget.cpp",
"newclasswidget.h", "newclasswidget.h",
"newclasswidget.ui", "newclasswidget.ui",
"optional.h",
"../3rdparty/optional/optional.hpp",
"osspecificaspects.h", "osspecificaspects.h",
"outputformat.h", "outputformat.h",
"outputformatter.cpp", "outputformatter.cpp",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment