Skip to content

Commit

Permalink
Use std::string_view and std::optional when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Porter committed Jun 2, 2018
1 parent 91da7b3 commit a32cb7e
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 37 deletions.
27 changes: 26 additions & 1 deletion .travis.yml
Expand Up @@ -191,6 +191,31 @@ matrix:
./b2 install -d0
popd
# C++17 builds

- compiler: clang
env: EXTRAOPTS="--std=c++17" COMPILER=clang++-5.0
addons: &clang-50
apt:
sources:
- *sources
- llvm-toolchain-trusty-5.0
packages:
- *packages
- clang-5.0
- libstdc++-7-dev

- compiler: gcc
env: >
EXTRAOPTS="--std=c++17" COMPILER=g++-7 BUILD_ARGS="-j1"
EXTRAFLAGS=-Wsuggest-override
addons: &gcc-7
apt:
sources: *sources
packages:
- *packages
- g++-7

# Make-backend builds

- compiler: clang
Expand Down Expand Up @@ -223,6 +248,6 @@ before_install:
script:
- >
CXX=${COMPILER} CPPFLAGS="${COMMONFLAGS} ${EXTRAFLAGS}"
9k build --vendorize --backend=${BACKEND}
9k build --vendorize --backend=${BACKEND} ${EXTRAOPTS}
- cd build
- ${BACKEND} test ${BUILD_ARGS}
2 changes: 1 addition & 1 deletion build.bfg
Expand Up @@ -8,7 +8,7 @@ project('mettle', version='1.0pre')
if env.builder('c++').flavor == 'msvc':
global_options(['/EHsc'], lang='c++')
else:
global_options(['-std=c++14'], lang='c++')
global_options(['-std={}'.format(argv.std)], lang='c++')

includes = header_directory('include', include='*.hpp')

Expand Down
10 changes: 7 additions & 3 deletions doc/install.md
Expand Up @@ -36,8 +36,8 @@ Before you get started with mettle, you'll need to install its dependencies:
* [bencode.hpp](https://github.com/jimporter/bencode.hpp)
* [bfg9000](https://jimporter.github.io/bfg9000/)

To simplify the installation of bencode.hpp, you can run
`scripts/vendorize_bencode.py` from your mettle source directory. This will
To simplify the installation of bencode.hpp, you can pass `--vendorize` during
configuration (or run `scripts/vendorize_bencode.py` directly). This will
download and copy bencode.hpp to mettle's include directory, so you won't have
to install it yourself.

Expand Down Expand Up @@ -73,11 +73,15 @@ Building with bfg9000 is straightforward. Just run the following:

```sh
cd path/to/mettle/
9k build/
9k build/ --vendorize
cd build/
ninja install
```

As noted above, if you already have bencode.hpp installed, you can remove the
`--vendorize` argument. If you'd like to build with C++17 support, you can pass
`--std=c++17` during configuration.

You can specify the compiler to use and its options with the usual (Unix-style)
environment variables, such as `CXX` and `CXXFLAGS`. For further information
about how to use bfg9000, such as changing the build backend, see its
Expand Down
29 changes: 14 additions & 15 deletions include/mettle/driver/detail/optional.hpp
@@ -1,29 +1,28 @@
#ifndef INC_METTLE_DRIVER_DETAIL_OPTIONAL_HPP
#define INC_METTLE_DRIVER_DETAIL_OPTIONAL_HPP

// Get _LIBCPP_VERSION to detect libc++.
#include <ciso646>
// Try to use std::optional, N4480's optional class, or fall back to Boost's.

// Try to use N4480's optional class, or fall back to Boost's.
#if defined(METTLE_USE_STDLIB_EXTS)
# include <experimental/optional>
# define METTLE_OPTIONAL_NS std::experimental
#elif !defined(METTLE_NO_STDLIB_EXTS) && defined(__has_include)
// XXX: clang doesn't support SFINAE in variadic templates, which libstdc++'s
// `optional` relies on. See <https://llvm.org/bugs/show_bug.cgi?id=23840>.
# if !(defined(__clang__) && !defined(_LIBCPP_VERSION) && \
defined(__GLIBCXX__)) && __has_include(<experimental/optional>)
#ifdef __has_include
# if __has_include(<optional>) && __cplusplus >= 201703L
# include <optional>
# define METTLE_OPTIONAL_NS std
# elif __has_include(<experimental/optional>) && \
!defined(METTLE_NO_STDLIB_EXTS)
# include <experimental/optional>
# define METTLE_OPTIONAL_NS std::experimental
# endif
#endif

#ifndef METTLE_OPTIONAL_NS
# ifdef METTLE_USE_STDLIB_EXTS
# include <experimental/optional>
# define METTLE_OPTIONAL_NS std::experimental
# else
# include <boost/optional.hpp>
# include <boost/optional/optional_io.hpp>
# define METTLE_OPTIONAL_NS boost
# endif
#else
# include <boost/optional.hpp>
# include <boost/optional/optional_io.hpp>
# define METTLE_OPTIONAL_NS boost
#endif

#endif
33 changes: 33 additions & 0 deletions include/mettle/output/detail/string_view.hpp
@@ -0,0 +1,33 @@
#ifndef INC_METTLE_OUTPUT_DETAIL_STRING_VIEW_HPP
#define INC_METTLE_OUTPUT_DETAIL_STRING_VIEW_HPP

// Try to use std::string_view, N4480's version, or fall back to Boost's.

#ifdef __has_include
# if __has_include(<string_view>) && __cplusplus >= 201703L
# include <string_view>
# define METTLE_STRING_VIEW std::basic_string_view
# elif __has_include(<experimental/string_view>) && \
!defined(METTLE_NO_STDLIB_EXTS)
# include <experimental/string_view>
# define METTLE_STRING_VIEW std::experimental::basic_string_view
# endif
#endif

#ifndef METTLE_STRING_VIEW
# ifdef METTLE_USE_STDLIB_EXTS
# include <experimental/string_view>
# define METTLE_STRING_VIEW std::experimental::basic_string_view
# else
# include <boost/version.hpp>
# if BOOST_VERSION >= 106100
# include <boost/utility/string_view.hpp>
# define METTLE_STRING_VIEW boost::basic_string_view
# else
# include <boost/utility/string_ref.hpp>
# define METTLE_STRING_VIEW boost::basic_string_ref
# endif
# endif
#endif

#endif
17 changes: 1 addition & 16 deletions include/mettle/output/string.hpp
@@ -1,22 +1,7 @@
#ifndef INC_METTLE_OUTPUT_STRING_HPP
#define INC_METTLE_OUTPUT_STRING_HPP

// Try to use N4480's string_view class, or fall back to Boost's.
#if defined(METTLE_USE_STDLIB_EXTS)
# include <boost/utility/string_ref.hpp>
# define METTLE_STRING_VIEW boost::basic_string_ref
#elif !defined(METTLE_NO_STDLIB_EXTS) && defined(__has_include)
# if __has_include(<experimental/string_view>)
# include <experimental/string_view>
# define METTLE_STRING_VIEW std::experimental::basic_string_view
# else
# include <boost/utility/string_ref.hpp>
# define METTLE_STRING_VIEW boost::basic_string_ref
# endif
#else
# include <boost/utility/string_ref.hpp>
# define METTLE_STRING_VIEW boost::basic_string_ref
#endif
#include "detail/string_view.hpp"

#include <codecvt>
#include <ostream>
Expand Down
1 change: 1 addition & 0 deletions options.bfg
Expand Up @@ -2,3 +2,4 @@

argument('vendorize', action='store_true',
help='vendorize the bencode library')
argument('std', default='c++14', help='set the C++ standard to compile with')
2 changes: 1 addition & 1 deletion src/libmettle/cmd_line.cpp
Expand Up @@ -19,7 +19,7 @@
#endif

#if __cplusplus >= 201703L
# define FALLTHROUGH [[fallthrough]]
# define FALLTHROUGH [[fallthrough]];
#elif defined(__clang__)
# define FALLTHROUGH [[clang::fallthrough]];
#elif defined(__GNUG__) && __GNUC__ >= 7
Expand Down
3 changes: 3 additions & 0 deletions test/suite/test_test_caller.cpp
Expand Up @@ -3,6 +3,8 @@ using namespace mettle;

#include "run_counter.hpp"

// Check if std::apply exists, and if not, use our own implementation.
#if __cplusplus < 201703L
template<typename F, typename Tuple, std::size_t ...I>
decltype(auto) apply_impl(F &&f, Tuple &&t, std::index_sequence<I...>) {
return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
Expand All @@ -15,6 +17,7 @@ decltype(auto) apply(F&& f, Tuple &&t) {
>;
return apply_impl(std::forward<F>(f), std::forward<Tuple>(t), Indices());
}
#endif

template<typename Tuple>
struct run_counter_from_tuple_t;
Expand Down

0 comments on commit a32cb7e

Please sign in to comment.