[mapbox-variant] 02/08: Imported Upstream version 1.1.1
Bas Couwenberg
sebastic at debian.org
Mon Jul 18 23:51:32 UTC 2016
This is an automated email from the git hooks/post-receive script.
sebastic pushed a commit to branch master
in repository mapbox-variant.
commit 90d6ec62d1762f4d564e515f99616509a1327046
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date: Mon Jul 18 18:14:25 2016 +0200
Imported Upstream version 1.1.1
---
.gitignore | 1 +
.gitmodules | 3 +
.travis.yml | 59 ++---
Jamroot | 19 +-
Makefile | 67 +++---
README.md | 14 +-
optional.hpp => include/mapbox/optional.hpp | 4 +-
.../mapbox/recursive_wrapper.hpp | 6 +-
variant.hpp => include/mapbox/variant.hpp | 250 +++++++++++++--------
variant_io.hpp => include/mapbox/variant_io.hpp | 6 +-
scripts/run_compilation_failure_tests.sh | 35 ++-
test/bench_variant.cpp | 15 +-
test/binary_visitor_test.cpp | 4 +-
test/compilation_failure/default_constructor.cpp | 3 +-
test/compilation_failure/empty_typelist.cpp | 3 +-
test/compilation_failure/equality.cpp | 3 +-
test/compilation_failure/get_type.cpp | 3 +-
test/compilation_failure/is_type.cpp | 5 +-
.../mutating_visitor_on_const.cpp | 3 +-
test/compilation_failure/no-reference.cpp | 3 +-
test/include/auto_cpu_timer.hpp | 16 ++
test/our_variant_hello_world.cpp | 2 +-
test/recursive_wrapper_test.cpp | 6 +-
test/reference_wrapper_test.cpp | 2 +-
test/t/binary_visitor_1.cpp | 2 +-
test/t/binary_visitor_2.cpp | 2 +-
test/t/binary_visitor_3.cpp | 2 +-
test/t/binary_visitor_4.cpp | 2 +-
test/t/binary_visitor_5.cpp | 2 +-
test/t/binary_visitor_6.cpp | 2 +-
test/t/binary_visitor_impl.hpp | 2 +-
test/t/issue21.cpp | 4 +-
test/t/mutating_visitor.cpp | 4 +-
test/t/optional.cpp | 2 +-
test/t/recursive_wrapper.cpp | 2 +-
test/t/sizeof.cpp | 4 +-
test/t/unary_visitor.cpp | 4 +-
test/t/variant.cpp | 31 ++-
test/unique_ptr_test.cpp | 6 +-
variant.gyp | 2 +-
40 files changed, 346 insertions(+), 259 deletions(-)
diff --git a/.gitignore b/.gitignore
index a9bc860..e656256 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ deps
*.gcda
*.gcno
.ycm_extra_conf.pyc
+mason_packages
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..44fba9a
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule ".mason"]
+ path = .mason
+ url = https://github.com/mapbox/mason.git
diff --git a/.travis.yml b/.travis.yml
index 99d0b48..4119d9e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,4 +1,4 @@
-language: c
+language: generic
sudo: false
@@ -6,49 +6,50 @@ sudo: false
addons_shortcuts:
addons_clang35: &clang35
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5', 'boost-latest' ]
- packages: [ 'clang-3.5', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5' ]
+ packages: [ 'clang-3.5', 'llvm-3.5-dev' ]
addons_clang36: &clang36
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6', 'boost-latest' ]
- packages: [ 'clang-3.6', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6' ]
+ packages: [ 'clang-3.6' ]
addons_clang37: &clang37
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.7', 'boost-latest' ]
- packages: [ 'clang-3.7', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.7' ]
+ packages: [ 'clang-3.7' ]
addons_clang38: &clang38
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise', 'boost-latest' ]
- packages: [ 'clang-3.8', 'libboost1.55-all-dev']
+ sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.8' ]
+ packages: [ 'clang-3.8']
+ addons_clang39: &clang39
+ apt:
+ sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise' ]
+ packages: [ 'clang-3.9']
addons_gcc47: &gcc47
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'boost-latest' ]
- packages: [ 'g++-4.7', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test' ]
+ packages: [ 'g++-4.7' ]
addons_gcc48: &gcc48
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'boost-latest' ]
- packages: [ 'g++-4.8', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test' ]
+ packages: [ 'g++-4.8' ]
addons_gcc49: &gcc49
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'boost-latest' ]
- packages: [ 'g++-4.9', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test' ]
+ packages: [ 'g++-4.9' ]
addons_gcc5: &gcc5
apt:
- sources: [ 'ubuntu-toolchain-r-test', 'boost-latest' ]
- packages: [ 'g++-5', 'libboost1.55-all-dev' ]
+ sources: [ 'ubuntu-toolchain-r-test' ]
+ packages: [ 'g++-5' ]
matrix:
include:
- os: osx
- osx_image: xcode6
- compiler: clang
- - os: osx
osx_image: xcode7
env: TEST_GYP_BUILD=True
compiler: clang
- os: linux
compiler: "clang35"
- env: CXX=clang++-3.5
+ env: CXX=clang++-3.5 COVERAGE=True
addons: *clang35
- os: linux
compiler: "clang36"
@@ -56,7 +57,7 @@ matrix:
addons: *clang36
- os: linux
compiler: "clang37"
- env: CXX=clang++-3.7 COVERAGE=True
+ env: CXX=clang++-3.7
addons: *clang37
- os: linux
compiler: "clang38"
@@ -66,6 +67,11 @@ matrix:
compiler: "clang38"
env: CXX=clang++-3.8 CXX_STD=c++14
addons: *clang38
+ # not whitelisted yet: https://github.com/travis-ci/apt-package-whitelist/issues/2764
+ #- os: linux
+ # compiler: "clang39"
+ # env: CXX=clang++-3.9
+ # addons: *clang39
- os: linux
compiler: "gcc47"
env: CXX=g++-4.7
@@ -96,7 +102,6 @@ before_install:
- if [[ $(uname -s) == 'Linux' ]]; then
export PYTHONPATH=$(pwd)/.local/lib/python2.7/site-packages;
else
- brew install boost;
export PYTHONPATH=$(pwd)/.local/lib/python/site-packages;
fi
- if [[ ${COVERAGE:-0} == 'True' ]]; then
@@ -106,11 +111,7 @@ before_install:
install:
- make test
- make bench
- - if [[ $(uname -s) == 'Linux' ]]; then
- make sizes /usr/include/boost/variant.hpp;
- else
- make sizes `brew --prefix`/include/boost/variant.hpp;
- fi
+ - make sizes
- scripts/run_compilation_failure_tests.sh
- if [[ ${TEST_GYP_BUILD:-0} == 'True' ]]; then
make clean;
@@ -123,5 +124,5 @@ script:
make coverage;
./out/cov-test;
cp unit*gc* test/;
- ./.local/bin/cpp-coveralls -i optional.hpp -i recursive_wrapper.hpp -i variant.hpp -i variant_io.hpp --gcov-options '\-lp';
+ ./.local/bin/cpp-coveralls --gcov /usr/bin/llvm-cov-3.5 --gcov-options '\-lp' -i optional.hpp -i recursive_wrapper.hpp -i variant.hpp -i variant_io.hpp;
fi
diff --git a/Jamroot b/Jamroot
index 1e98cf5..3e863e2 100644
--- a/Jamroot
+++ b/Jamroot
@@ -1,4 +1,4 @@
-# Inofficial and incomplete build file using Boost build system.
+# Unofficial and incomplete build file using Boost build system.
# You should use make unless you know what you are doing.
local BOOST_DIR = "/usr/local" ;
@@ -17,9 +17,10 @@ exe variant-test
.//chrono
:
<include>$(BOOST_DIR)/include
- <include>./
+ <include>./include
+ <include>./test/include
#<define>SINGLE_THREADED
- <variant>release:<cxxflags>-march=native
+ <variant>release:<cxxflags>"-march=native -Wweak-vtables"
;
@@ -31,7 +32,8 @@ exe binary-visitor-test
.//chrono
:
<include>$(BOOST_DIR)/include
- <include>./
+ <include>./include
+ <include>./test/include
<variant>release:<cxxflags>-march=native
;
@@ -43,7 +45,8 @@ exe recursive-wrapper-test
.//chrono
:
<include>$(BOOST_DIR)/include
- <include>./
+ <include>./include
+ <include>./test/include
<variant>release:<cxxflags>-march=native
;
@@ -55,7 +58,8 @@ exe unique-ptr-test
.//chrono
:
<include>$(BOOST_DIR)/include
- <include>./
+ <include>./include
+ <include>./test/include
<variant>release:<cxxflags>-march=native
;
@@ -68,6 +72,7 @@ exe reference_wrapper_test
.//chrono
:
<include>$(BOOST_DIR)/include
- <include>./
+ <include>./include
+ <include>./test/include
<variant>release:<cxxflags>-march=native
;
diff --git a/Makefile b/Makefile
index 3665251..b20f9ee 100644
--- a/Makefile
+++ b/Makefile
@@ -1,33 +1,24 @@
+MASON = .mason/mason
+BOOST_VERSION = boost 1.60.0
CXX := $(CXX)
CXX_STD ?= c++11
-BOOST_LIBS = -lboost_timer -lboost_system -lboost_chrono
+BOOST_FLAGS = `$(MASON) cflags $(BOOST_VERSION)`
RELEASE_FLAGS = -O3 -DNDEBUG -march=native -DSINGLE_THREADED -fvisibility-inlines-hidden
DEBUG_FLAGS = -O0 -g -DDEBUG -fno-inline-functions
COMMON_FLAGS = -Wall -pedantic -Wextra -Wsign-compare -Wsign-conversion -Wshadow -Wunused-parameter -std=$(CXX_STD)
CXXFLAGS := $(CXXFLAGS)
LDFLAGS := $(LDFLAGS)
-OS:=$(shell uname -s)
-ifeq ($(OS),Darwin)
- CXXFLAGS += -stdlib=libc++
- LDFLAGS += -stdlib=libc++ -F/ -framework CoreFoundation
-else
- BOOST_LIBS += -lrt
-endif
-
-ifeq (sizes,$(firstword $(MAKECMDGOALS)))
- RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
- $(eval $(RUN_ARGS):;@:)
- ifndef RUN_ARGS
- $(error sizes target requires you pass full path to boost variant.hpp)
- endif
- .PHONY: $(RUN_ARGS)
-endif
+ALL_HEADERS = $(shell find include/mapbox/ '(' -name '*.hpp' ')')
all: out/bench-variant out/unique_ptr_test out/unique_ptr_test out/recursive_wrapper_test out/binary_visitor_test
+mason_packages:
+ git submodule update --init .mason
+ $(MASON) install $(BOOST_VERSION)
+
./deps/gyp:
git clone --depth 1 https://chromium.googlesource.com/external/gyp.git ./deps/gyp
@@ -36,25 +27,25 @@ gyp: ./deps/gyp
make V=1 -C ./out tests
./out/Release/tests
-out/bench-variant-debug: Makefile test/bench_variant.cpp variant.hpp recursive_wrapper.hpp
+out/bench-variant-debug: Makefile mason_packages test/bench_variant.cpp
mkdir -p ./out
- $(CXX) -o out/bench-variant-debug test/bench_variant.cpp -I./ -pthreads $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS)
+ $(CXX) -o out/bench-variant-debug test/bench_variant.cpp -I./include -Itest/include -pthreads $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS)
-out/bench-variant: Makefile test/bench_variant.cpp variant.hpp recursive_wrapper.hpp
+out/bench-variant: Makefile mason_packages test/bench_variant.cpp
mkdir -p ./out
- $(CXX) -o out/bench-variant test/bench_variant.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS)
+ $(CXX) -o out/bench-variant test/bench_variant.cpp -I./include -Itest/include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS)
-out/unique_ptr_test: Makefile test/unique_ptr_test.cpp variant.hpp recursive_wrapper.hpp
+out/unique_ptr_test: Makefile mason_packages test/unique_ptr_test.cpp
mkdir -p ./out
- $(CXX) -o out/unique_ptr_test test/unique_ptr_test.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS)
+ $(CXX) -o out/unique_ptr_test test/unique_ptr_test.cpp -I./include -Itest/include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS)
-out/recursive_wrapper_test: Makefile test/recursive_wrapper_test.cpp variant.hpp recursive_wrapper.hpp
+out/recursive_wrapper_test: Makefile mason_packages test/recursive_wrapper_test.cpp
mkdir -p ./out
- $(CXX) -o out/recursive_wrapper_test test/recursive_wrapper_test.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS)
+ $(CXX) -o out/recursive_wrapper_test test/recursive_wrapper_test.cpp -I./include -Itest/include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS)
-out/binary_visitor_test: Makefile test/binary_visitor_test.cpp variant.hpp variant_io.hpp recursive_wrapper.hpp
+out/binary_visitor_test: Makefile mason_packages test/binary_visitor_test.cpp
mkdir -p ./out
- $(CXX) -o out/binary_visitor_test test/binary_visitor_test.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS)
+ $(CXX) -o out/binary_visitor_test test/binary_visitor_test.cpp -I./include -Itest/include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS)
bench: out/bench-variant out/unique_ptr_test out/unique_ptr_test out/recursive_wrapper_test out/binary_visitor_test
./out/bench-variant 100000
@@ -66,9 +57,9 @@ out/unit.o: Makefile test/unit.cpp
mkdir -p ./out
$(CXX) -c -o $@ test/unit.cpp -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS)
-out/%.o: test/t/%.cpp Makefile optional.hpp recursive_wrapper.hpp variant.hpp variant_io.hpp
+out/%.o: test/t/%.cpp Makefile $(ALL_HEADERS)
mkdir -p ./out
- $(CXX) -c -o $@ $< -I. -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS)
+ $(CXX) -c -o $@ $< -Iinclude -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS)
out/unit: out/unit.o out/binary_visitor_1.o out/binary_visitor_2.o out/binary_visitor_3.o out/binary_visitor_4.o out/binary_visitor_5.o out/binary_visitor_6.o out/issue21.o out/mutating_visitor.o out/optional.o out/recursive_wrapper.o out/sizeof.o out/unary_visitor.o out/variant.o
mkdir -p ./out
@@ -79,14 +70,14 @@ test: out/unit
coverage:
mkdir -p ./out
- $(CXX) -o out/cov-test --coverage test/unit.cpp test/t/*.cpp -I./ -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS)
+ $(CXX) -o out/cov-test --coverage test/unit.cpp test/t/*.cpp -I./include -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS)
-sizes: Makefile variant.hpp recursive_wrapper.hpp
+sizes: Makefile
mkdir -p ./out
- @$(CXX) -o ./out/our_variant_hello_world.out variant.hpp $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/our_variant_hello_world.out
- @$(CXX) -o ./out/boost_variant_hello_world.out $(RUN_ARGS) $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/boost_variant_hello_world.out
- @$(CXX) -o ./out/our_variant_hello_world ./test/our_variant_hello_world.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/our_variant_hello_world
- @$(CXX) -o ./out/boost_variant_hello_world ./test/boost_variant_hello_world.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/boost_variant_hello_world
+ @$(CXX) -o ./out/our_variant_hello_world.out include/mapbox/variant.hpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/our_variant_hello_world.out
+ @$(CXX) -o ./out/boost_variant_hello_world.out `$(MASON) prefix boost 1.60.0`/include/boost/variant.hpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(BOOST_FLAGS) && du -h ./out/boost_variant_hello_world.out
+ @$(CXX) -o ./out/our_variant_hello_world ./test/our_variant_hello_world.cpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) && du -h ./out/our_variant_hello_world
+ @$(CXX) -o ./out/boost_variant_hello_world ./test/boost_variant_hello_world.cpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(BOOST_FLAGS) && du -h ./out/boost_variant_hello_world
profile: out/bench-variant-debug
mkdir -p profiling/
@@ -102,9 +93,9 @@ clean:
rm -f test/*gcov
rm -f *.gcda *.gcno
-pgo: out Makefile variant.hpp recursive_wrapper.hpp
- $(CXX) -o out/bench-variant test/bench_variant.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS) -pg -fprofile-generate
+pgo: out Makefile
+ $(CXX) -o out/bench-variant test/bench_variant.cpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS) -pg -fprofile-generate
./test-variant 500000 >/dev/null 2>/dev/null
- $(CXX) -o out/bench-variant test/bench_variant.cpp -I./ $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_LIBS) -fprofile-use
+ $(CXX) -o out/bench-variant test/bench_variant.cpp -I./include $(RELEASE_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(BOOST_FLAGS) -fprofile-use
.PHONY: sizes test
diff --git a/README.md b/README.md
index 24cdb57..51b5ec3 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Mapbox Variant
-An alternative to `boost::variant` for C++11.
+An alternative to `boost::variant` for C++11 and C++14
[![Build Status](https://secure.travis-ci.org/mapbox/variant.svg)](https://travis-ci.org/mapbox/variant)
[![Build status](https://ci.appveyor.com/api/projects/status/v9tatx21j1k0fcgy)](https://ci.appveyor.com/project/Mapbox/variant)
@@ -55,11 +55,12 @@ Tested with:
- g++-4.7
- g++-4.8
- g++-4.9
- - g++-5
+ - g++-5.2
- clang++-3.5
- clang++-3.6
- clang++-3.7
- clang++-3.8
+ - clang++-3.9
- Visual Studio 2015
## Usage
@@ -93,15 +94,6 @@ On Windows run `scripts/build-local.bat`.
## Benchmarks
-The benchmarks depend on:
-
- - Boost headers (for benchmarking against `boost::variant`)
- - Boost built with `--with-timer` (used for benchmark timing)
-
-On Unix systems set your boost includes and libs locations and run `make test`:
-
- export LDFLAGS='-L/opt/boost/lib'
- export CXXFLAGS='-I/opt/boost/include'
make bench
diff --git a/optional.hpp b/include/mapbox/optional.hpp
similarity index 97%
rename from optional.hpp
rename to include/mapbox/optional.hpp
index 1185894..d84705c 100644
--- a/optional.hpp
+++ b/include/mapbox/optional.hpp
@@ -6,7 +6,7 @@
#include <type_traits>
#include <utility>
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
namespace mapbox {
namespace util {
@@ -22,7 +22,7 @@ class optional
variant<none_type, T> variant_;
- public:
+public:
optional() = default;
optional(optional const& rhs)
diff --git a/recursive_wrapper.hpp b/include/mapbox/recursive_wrapper.hpp
similarity index 96%
rename from recursive_wrapper.hpp
rename to include/mapbox/recursive_wrapper.hpp
index 4becdd6..4ffcbd7 100644
--- a/recursive_wrapper.hpp
+++ b/include/mapbox/recursive_wrapper.hpp
@@ -29,7 +29,7 @@ class recursive_wrapper
this->get() = rhs;
}
- public:
+public:
using type = T;
/**
@@ -42,9 +42,9 @@ class recursive_wrapper
* @throws any exception thrown by the default constructur of T.
*/
recursive_wrapper()
- : p_(new T){};
+ : p_(new T){}
- ~recursive_wrapper() noexcept { delete p_; };
+ ~recursive_wrapper() noexcept { delete p_; }
recursive_wrapper(recursive_wrapper const& operand)
: p_(new T(operand.get())) {}
diff --git a/variant.hpp b/include/mapbox/variant.hpp
similarity index 77%
rename from variant.hpp
rename to include/mapbox/variant.hpp
index db5d3c8..d7c1e86 100644
--- a/variant.hpp
+++ b/include/mapbox/variant.hpp
@@ -11,7 +11,7 @@
#include <typeinfo>
#include <utility>
-#include "recursive_wrapper.hpp"
+#include <mapbox/recursive_wrapper.hpp>
// clang-format off
// [[deprecated]] is only available in C++14, use this for the time being
@@ -29,21 +29,26 @@
#ifdef _MSC_VER
- // https://msdn.microsoft.com/en-us/library/bw1hbe6y.aspx
- #ifdef NDEBUG
- #define VARIANT_INLINE __forceinline
- #else
- #define VARIANT_INLINE __declspec(noinline)
- #endif
+// https://msdn.microsoft.com/en-us/library/bw1hbe6y.aspx
+# ifdef NDEBUG
+# define VARIANT_INLINE __forceinline
+# else
+# define VARIANT_INLINE //__declspec(noinline)
+# endif
#else
- #ifdef NDEBUG
- #define VARIANT_INLINE inline __attribute__((always_inline))
- #else
- #define VARIANT_INLINE __attribute__((noinline))
- #endif
+# ifdef NDEBUG
+# define VARIANT_INLINE //inline __attribute__((always_inline))
+# else
+# define VARIANT_INLINE __attribute__((noinline))
+# endif
#endif
// clang-format on
+// Exceptions
+#if defined( __EXCEPTIONS) || defined( _MSC_VER)
+#define HAS_EXCEPTIONS
+#endif
+
#define VARIANT_MAJOR_VERSION 1
#define VARIANT_MINOR_VERSION 1
#define VARIANT_PATCH_VERSION 0
@@ -58,7 +63,7 @@ namespace util {
class bad_variant_access : public std::runtime_error
{
- public:
+public:
explicit bad_variant_access(const std::string& what_arg)
: runtime_error(what_arg) {}
@@ -72,7 +77,7 @@ struct MAPBOX_VARIANT_DEPRECATED static_visitor
{
using result_type = R;
- protected:
+protected:
static_visitor() {}
~static_visitor() {}
};
@@ -88,8 +93,8 @@ template <typename T, typename First, typename... Types>
struct direct_type<T, First, Types...>
{
static constexpr std::size_t index = std::is_same<T, First>::value
- ? sizeof...(Types)
- : direct_type<T, Types...>::index;
+ ? sizeof...(Types)
+ : direct_type<T, Types...>::index;
};
template <typename T>
@@ -105,8 +110,8 @@ template <typename T, typename First, typename... Types>
struct convertible_type<T, First, Types...>
{
static constexpr std::size_t index = std::is_convertible<T, First>::value
- ? sizeof...(Types)
- : convertible_type<T, Types...>::index;
+ ? sizeof...(Types)
+ : convertible_type<T, Types...>::index;
};
template <typename T>
@@ -118,7 +123,7 @@ struct convertible_type<T>
template <typename T, typename... Types>
struct value_traits
{
- using value_type = typename std::remove_reference<T>::type;
+ using value_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
static constexpr std::size_t direct_index = direct_type<value_type, Types...>::index;
static constexpr bool is_direct = direct_index != invalid_value;
static constexpr std::size_t index = is_direct ? direct_index : convertible_type<value_type, Types...>::index;
@@ -127,35 +132,6 @@ struct value_traits
using target_type = typename std::tuple_element<tindex, std::tuple<void, Types...>>::type;
};
-// check if T is in Types...
-template <typename T, typename... Types>
-struct has_type;
-
-template <typename T, typename First, typename... Types>
-struct has_type<T, First, Types...>
-{
- static constexpr bool value = std::is_same<T, First>::value || has_type<T, Types...>::value;
-};
-
-template <typename T>
-struct has_type<T> : std::false_type
-{
-};
-
-template <typename T, typename... Types>
-struct is_valid_type;
-
-template <typename T, typename First, typename... Types>
-struct is_valid_type<T, First, Types...>
-{
- static constexpr bool value = std::is_convertible<T, First>::value || is_valid_type<T, Types...>::value;
-};
-
-template <typename T>
-struct is_valid_type<T> : std::false_type
-{
-};
-
template <typename T, typename R = void>
struct enable_if_type
{
@@ -299,7 +275,7 @@ struct dispatcher<F, V, R, T, Types...>
{
if (v.template is<T>())
{
- return f(unwrapper<T>::apply_const(v.template get<T>()));
+ return f(unwrapper<T>::apply_const(v.template get_unchecked<T>()));
}
else
{
@@ -311,7 +287,7 @@ struct dispatcher<F, V, R, T, Types...>
{
if (v.template is<T>())
{
- return f(unwrapper<T>::apply(v.template get<T>()));
+ return f(unwrapper<T>::apply(v.template get_unchecked<T>()));
}
else
{
@@ -325,12 +301,12 @@ struct dispatcher<F, V, R, T>
{
VARIANT_INLINE static R apply_const(V const& v, F&& f)
{
- return f(unwrapper<T>::apply_const(v.template get<T>()));
+ return f(unwrapper<T>::apply_const(v.template get_unchecked<T>()));
}
VARIANT_INLINE static R apply(V& v, F&& f)
{
- return f(unwrapper<T>::apply(v.template get<T>()));
+ return f(unwrapper<T>::apply(v.template get_unchecked<T>()));
}
};
@@ -344,8 +320,8 @@ struct binary_dispatcher_rhs<F, V, R, T0, T1, Types...>
{
if (rhs.template is<T1>()) // call binary functor
{
- return f(unwrapper<T0>::apply_const(lhs.template get<T0>()),
- unwrapper<T1>::apply_const(rhs.template get<T1>()));
+ return f(unwrapper<T0>::apply_const(lhs.template get_unchecked<T0>()),
+ unwrapper<T1>::apply_const(rhs.template get_unchecked<T1>()));
}
else
{
@@ -357,8 +333,8 @@ struct binary_dispatcher_rhs<F, V, R, T0, T1, Types...>
{
if (rhs.template is<T1>()) // call binary functor
{
- return f(unwrapper<T0>::apply(lhs.template get<T0>()),
- unwrapper<T1>::apply(rhs.template get<T1>()));
+ return f(unwrapper<T0>::apply(lhs.template get_unchecked<T0>()),
+ unwrapper<T1>::apply(rhs.template get_unchecked<T1>()));
}
else
{
@@ -372,14 +348,14 @@ struct binary_dispatcher_rhs<F, V, R, T0, T1>
{
VARIANT_INLINE static R apply_const(V const& lhs, V const& rhs, F&& f)
{
- return f(unwrapper<T0>::apply_const(lhs.template get<T0>()),
- unwrapper<T1>::apply_const(rhs.template get<T1>()));
+ return f(unwrapper<T0>::apply_const(lhs.template get_unchecked<T0>()),
+ unwrapper<T1>::apply_const(rhs.template get_unchecked<T1>()));
}
VARIANT_INLINE static R apply(V& lhs, V& rhs, F&& f)
{
- return f(unwrapper<T0>::apply(lhs.template get<T0>()),
- unwrapper<T1>::apply(rhs.template get<T1>()));
+ return f(unwrapper<T0>::apply(lhs.template get_unchecked<T0>()),
+ unwrapper<T1>::apply(rhs.template get_unchecked<T1>()));
}
};
@@ -393,8 +369,8 @@ struct binary_dispatcher_lhs<F, V, R, T0, T1, Types...>
{
if (lhs.template is<T1>()) // call binary functor
{
- return f(unwrapper<T1>::apply_const(lhs.template get<T1>()),
- unwrapper<T0>::apply_const(rhs.template get<T0>()));
+ return f(unwrapper<T1>::apply_const(lhs.template get_unchecked<T1>()),
+ unwrapper<T0>::apply_const(rhs.template get_unchecked<T0>()));
}
else
{
@@ -406,8 +382,8 @@ struct binary_dispatcher_lhs<F, V, R, T0, T1, Types...>
{
if (lhs.template is<T1>()) // call binary functor
{
- return f(unwrapper<T1>::apply(lhs.template get<T1>()),
- unwrapper<T0>::apply(rhs.template get<T0>()));
+ return f(unwrapper<T1>::apply(lhs.template get_unchecked<T1>()),
+ unwrapper<T0>::apply(rhs.template get_unchecked<T0>()));
}
else
{
@@ -421,14 +397,14 @@ struct binary_dispatcher_lhs<F, V, R, T0, T1>
{
VARIANT_INLINE static R apply_const(V const& lhs, V const& rhs, F&& f)
{
- return f(unwrapper<T1>::apply_const(lhs.template get<T1>()),
- unwrapper<T0>::apply_const(rhs.template get<T0>()));
+ return f(unwrapper<T1>::apply_const(lhs.template get_unchecked<T1>()),
+ unwrapper<T0>::apply_const(rhs.template get_unchecked<T0>()));
}
VARIANT_INLINE static R apply(V& lhs, V& rhs, F&& f)
{
- return f(unwrapper<T1>::apply(lhs.template get<T1>()),
- unwrapper<T0>::apply(rhs.template get<T0>()));
+ return f(unwrapper<T1>::apply(lhs.template get_unchecked<T1>()),
+ unwrapper<T0>::apply(rhs.template get_unchecked<T0>()));
}
};
@@ -444,8 +420,8 @@ struct binary_dispatcher<F, V, R, T, Types...>
{
if (v1.template is<T>())
{
- return f(unwrapper<T>::apply_const(v0.template get<T>()),
- unwrapper<T>::apply_const(v1.template get<T>())); // call binary functor
+ return f(unwrapper<T>::apply_const(v0.template get_unchecked<T>()),
+ unwrapper<T>::apply_const(v1.template get_unchecked<T>())); // call binary functor
}
else
{
@@ -465,8 +441,8 @@ struct binary_dispatcher<F, V, R, T, Types...>
{
if (v1.template is<T>())
{
- return f(unwrapper<T>::apply(v0.template get<T>()),
- unwrapper<T>::apply(v1.template get<T>())); // call binary functor
+ return f(unwrapper<T>::apply(v0.template get_unchecked<T>()),
+ unwrapper<T>::apply(v1.template get_unchecked<T>())); // call binary functor
}
else
{
@@ -486,14 +462,14 @@ struct binary_dispatcher<F, V, R, T>
{
VARIANT_INLINE static R apply_const(V const& v0, V const& v1, F&& f)
{
- return f(unwrapper<T>::apply_const(v0.template get<T>()),
- unwrapper<T>::apply_const(v1.template get<T>())); // call binary functor
+ return f(unwrapper<T>::apply_const(v0.template get_unchecked<T>()),
+ unwrapper<T>::apply_const(v1.template get_unchecked<T>())); // call binary functor
}
VARIANT_INLINE static R apply(V& v0, V& v1, F&& f)
{
- return f(unwrapper<T>::apply(v0.template get<T>()),
- unwrapper<T>::apply(v1.template get<T>())); // call binary functor
+ return f(unwrapper<T>::apply(v0.template get_unchecked<T>()),
+ unwrapper<T>::apply(v1.template get_unchecked<T>())); // call binary functor
}
};
@@ -519,7 +495,7 @@ struct less_comp
template <typename Variant, typename Comp>
class comparer
{
- public:
+public:
explicit comparer(Variant const& lhs) noexcept
: lhs_(lhs) {}
comparer& operator=(comparer const&) = delete;
@@ -527,11 +503,11 @@ class comparer
template <typename T>
bool operator()(T const& rhs_content) const
{
- T const& lhs_content = lhs_.template get<T>();
+ T const& lhs_content = lhs_.template get_unchecked<T>();
return Comp()(lhs_content, rhs_content);
}
- private:
+private:
Variant const& lhs_;
};
@@ -561,7 +537,7 @@ class variant
static_assert(sizeof...(Types) > 0, "Template parameter type list of variant can not be empty");
static_assert(detail::static_none_of<std::is_reference, Types...>::value, "Variant can not hold reference types. Maybe use std::reference?");
- private:
+private:
static const std::size_t data_size = detail::static_max<sizeof(Types)...>::value;
static const std::size_t data_align = detail::static_max<alignof(Types)...>::value;
@@ -572,7 +548,7 @@ class variant
std::size_t type_index;
data_type data;
- public:
+public:
VARIANT_INLINE variant() noexcept(std::is_nothrow_default_constructible<first_type>::value)
: type_index(sizeof...(Types)-1)
{
@@ -604,7 +580,7 @@ class variant
helper_type::move(old.type_index, &old.data, &data);
}
- private:
+private:
VARIANT_INLINE void copy_assign(variant<Types...> const& rhs)
{
helper_type::destroy(type_index, &data);
@@ -621,7 +597,7 @@ class variant
type_index = rhs.type_index;
}
- public:
+public:
VARIANT_INLINE variant<Types...>& operator=(variant<Types...>&& other)
{
move_assign(std::move(other));
@@ -653,13 +629,20 @@ class variant
return *this;
}
- template <typename T>
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE bool is() const
{
- static_assert(detail::has_type<T, Types...>::value, "invalid type in T in `is<T>()` for this variant");
return type_index == detail::direct_type<T, Types...>::index;
}
+ template <typename T,typename std::enable_if<
+ (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE bool is() const
+ {
+ return type_index == detail::direct_type<recursive_wrapper<T>, Types...>::index;
+ }
+
VARIANT_INLINE bool valid() const
{
return type_index != detail::invalid_value;
@@ -674,9 +657,18 @@ class variant
type_index = detail::direct_type<T, Types...>::index;
}
+ // get_unchecked<T>()
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T& get_unchecked()
+ {
+ return *reinterpret_cast<T*>(&data);
+ }
+
+#ifdef HAS_EXCEPTIONS
// get<T>()
template <typename T, typename std::enable_if<
- (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T& get()
{
if (type_index == detail::direct_type<T, Types...>::index)
@@ -688,9 +680,18 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
template <typename T, typename std::enable_if<
- (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T const& get_unchecked() const
+ {
+ return *reinterpret_cast<T const*>(&data);
+ }
+
+#ifdef HAS_EXCEPTIONS
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T const& get() const
{
if (type_index == detail::direct_type<T, Types...>::index)
@@ -702,10 +703,20 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
+
+ // get_unchecked<T>() - T stored as recursive_wrapper<T>
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T& get_unchecked()
+ {
+ return (*reinterpret_cast<recursive_wrapper<T>*>(&data)).get();
+ }
+#ifdef HAS_EXCEPTIONS
// get<T>() - T stored as recursive_wrapper<T>
template <typename T, typename std::enable_if<
- (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T& get()
{
if (type_index == detail::direct_type<recursive_wrapper<T>, Types...>::index)
@@ -717,9 +728,18 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
template <typename T, typename std::enable_if<
- (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T const& get_unchecked() const
+ {
+ return (*reinterpret_cast<recursive_wrapper<T> const*>(&data)).get();
+ }
+
+#ifdef HAS_EXCEPTIONS
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<recursive_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T const& get() const
{
if (type_index == detail::direct_type<recursive_wrapper<T>, Types...>::index)
@@ -731,10 +751,20 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
+ // get_unchecked<T>() - T stored as std::reference_wrapper<T>
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<std::reference_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T& get_unchecked()
+ {
+ return (*reinterpret_cast<std::reference_wrapper<T>*>(&data)).get();
+ }
+
+#ifdef HAS_EXCEPTIONS
// get<T>() - T stored as std::reference_wrapper<T>
template <typename T, typename std::enable_if<
- (detail::direct_type<std::reference_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<std::reference_wrapper<T>, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T& get()
{
if (type_index == detail::direct_type<std::reference_wrapper<T>, Types...>::index)
@@ -746,9 +776,18 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
+
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<std::reference_wrapper<T const>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE T const& get_unchecked() const
+ {
+ return (*reinterpret_cast<std::reference_wrapper<T const> const*>(&data)).get();
+ }
+#ifdef HAS_EXCEPTIONS
template <typename T, typename std::enable_if<
- (detail::direct_type<std::reference_wrapper<T const>, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ (detail::direct_type<std::reference_wrapper<T const>, Types...>::index != detail::invalid_value)>::type* = nullptr>
VARIANT_INLINE T const& get() const
{
if (type_index == detail::direct_type<std::reference_wrapper<T const>, Types...>::index)
@@ -760,6 +799,7 @@ class variant
throw bad_variant_access("in get<T>()");
}
}
+#endif
// This function is deprecated because it returns an internal index field.
// Use which() instead.
@@ -773,6 +813,13 @@ class variant
return static_cast<int>(sizeof...(Types)-type_index - 1);
}
+ template <typename T, typename std::enable_if<
+ (detail::direct_type<T, Types...>::index != detail::invalid_value)>::type* = nullptr>
+ VARIANT_INLINE static constexpr int which() noexcept
+ {
+ return static_cast<int>(sizeof...(Types)-detail::direct_type<T, Types...>::index - 1);
+ }
+
// visitor
// unary
template <typename F, typename V, typename R = typename detail::result_of_unary_visit<F, first_type>::type>
@@ -884,17 +931,34 @@ auto VARIANT_INLINE apply_visitor(F&& f, V& v0, V& v1) -> decltype(V::binary_vis
}
// getter interface
+
+#ifdef HAS_EXCEPTIONS
template <typename ResultType, typename T>
-ResultType& get(T& var)
+auto get(T& var)->decltype(var.template get<ResultType>())
{
return var.template get<ResultType>();
}
+#endif
template <typename ResultType, typename T>
-ResultType const& get(T const& var)
+ResultType& get_unchecked(T& var)
+{
+ return var.template get_unchecked<ResultType>();
+}
+
+#ifdef HAS_EXCEPTIONS
+template <typename ResultType, typename T>
+auto get(T const& var)->decltype(var.template get<ResultType>())
{
return var.template get<ResultType>();
}
+#endif
+
+template <typename ResultType, typename T>
+ResultType const& get_unchecked(T const& var)
+{
+ return var.template get_unchecked<ResultType>();
+}
} // namespace util
} // namespace mapbox
diff --git a/variant_io.hpp b/include/mapbox/variant_io.hpp
similarity index 94%
rename from variant_io.hpp
rename to include/mapbox/variant_io.hpp
index e64cbad..1456cc5 100644
--- a/variant_io.hpp
+++ b/include/mapbox/variant_io.hpp
@@ -3,7 +3,7 @@
#include <iosfwd>
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
namespace mapbox {
namespace util {
@@ -13,7 +13,7 @@ namespace detail {
template <typename Out>
class printer
{
- public:
+public:
explicit printer(Out& out)
: out_(out) {}
printer& operator=(printer const&) = delete;
@@ -25,7 +25,7 @@ class printer
out_ << operand;
}
- private:
+private:
Out& out_;
};
}
diff --git a/scripts/run_compilation_failure_tests.sh b/scripts/run_compilation_failure_tests.sh
index c2608fe..5a14061 100755
--- a/scripts/run_compilation_failure_tests.sh
+++ b/scripts/run_compilation_failure_tests.sh
@@ -14,7 +14,7 @@ fi
error_msg() {
if [ ! -z "$1" ]; then
- printf 'output was:\n=======\n%s\n=======\n' "$1"
+ printf 'output was:\n=======\n%s\n=======\n' "$1"
fi
}
@@ -22,29 +22,28 @@ exit_code=0
for test_code in $DIR/*.cpp; do
name=`basename $test_code .cpp`
- result=`${CXX} -std=c++11 -c -o /dev/null -I. ${CXXFLAGS} ${test_code} 2>&1`
+ result=`${CXX} -std=c++11 -c -o /dev/null -I./include ${CXXFLAGS} ${test_code} 2>&1`
status=$?
if [ $status = 1 ]; then
- expected=`sed -n -e '/@EXPECTED/s/.*: \+//p' ${test_code}`
- if echo $result | grep -q "$expected"; then
- echo "$name [OK]"
- else
- echo "$name [FAILED - wrong error message]"
- echo "Expected error message: $expected"
- error_msg "$result"
- exit_code=1
- fi
+ expected=`sed -n -e '/@EXPECTED/s/.*: \+//p' ${test_code}`
+ if echo $result | grep -q "$expected"; then
+ echo "$name [OK]"
+ else
+ echo "$name [FAILED - wrong error message]"
+ echo "Expected error message: $expected"
+ error_msg "$result"
+ exit_code=1
+ fi
elif [ $status = 0 ]; then
- echo "$name [FAILED - compile was successful]"
- error_msg "$result"
- exit_code=1
+ echo "$name [FAILED - compile was successful]"
+ error_msg "$result"
+ exit_code=1
else
- echo "$name [FAILED - unknown error in compile]"
- error_msg "$result"
- exit_code=1
+ echo "$name [FAILED - unknown error in compile]"
+ error_msg "$result"
+ exit_code=1
fi
done
exit ${exit_code}
-
diff --git a/test/bench_variant.cpp b/test/bench_variant.cpp
index 700dac7..567bfe4 100644
--- a/test/bench_variant.cpp
+++ b/test/bench_variant.cpp
@@ -1,4 +1,3 @@
-
#include <algorithm>
#include <cstdlib>
#include <iostream>
@@ -8,10 +7,10 @@
#include <utility>
#include <vector>
-#include <boost/timer/timer.hpp>
-#include <boost/variant.hpp>
+#include "auto_cpu_timer.hpp"
-#include "variant.hpp"
+#include <boost/variant.hpp>
+#include <mapbox/variant.hpp>
#define TEXT_SHORT "Test"
#define TEXT_LONG "Testing various variant implementations with a longish string ........................................."
@@ -140,12 +139,12 @@ int main(int argc, char** argv)
{
std::cerr << "custom variant: ";
- boost::timer::auto_cpu_timer t;
+ auto_cpu_timer t;
run_variant_test(NUM_RUNS);
}
{
std::cerr << "boost variant: ";
- boost::timer::auto_cpu_timer t;
+ auto_cpu_timer t;
run_boost_test(NUM_RUNS);
}
}
@@ -158,7 +157,7 @@ int main(int argc, char** argv)
typedef thread_group::value_type value_type;
thread_group tg;
std::cerr << "custom variant: ";
- boost::timer::auto_cpu_timer timer;
+ auto_cpu_timer timer;
for (std::size_t i = 0; i < THREADS; ++i)
{
tg.emplace_back(new std::thread(run_variant_test, NUM_RUNS));
@@ -171,7 +170,7 @@ int main(int argc, char** argv)
typedef thread_group::value_type value_type;
thread_group tg;
std::cerr << "boost variant: ";
- boost::timer::auto_cpu_timer timer;
+ auto_cpu_timer timer;
for (std::size_t i = 0; i < THREADS; ++i)
{
tg.emplace_back(new std::thread(run_boost_test, NUM_RUNS));
diff --git a/test/binary_visitor_test.cpp b/test/binary_visitor_test.cpp
index fa0f2ea..8a73592 100644
--- a/test/binary_visitor_test.cpp
+++ b/test/binary_visitor_test.cpp
@@ -9,8 +9,8 @@
#include <utility>
#include <vector>
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
using namespace mapbox;
diff --git a/test/compilation_failure/default_constructor.cpp b/test/compilation_failure/default_constructor.cpp
index c75a8c1..b4e83d4 100644
--- a/test/compilation_failure/default_constructor.cpp
+++ b/test/compilation_failure/default_constructor.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED: First type in variant must be default constructible to allow default construction of variant
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
// Checks that the first type in a variant must be default constructible to
// make the variant default constructible.
diff --git a/test/compilation_failure/empty_typelist.cpp b/test/compilation_failure/empty_typelist.cpp
index 69a631c..9b8a125 100644
--- a/test/compilation_failure/empty_typelist.cpp
+++ b/test/compilation_failure/empty_typelist.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED: Template parameter type list of variant can not be empty
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
// Empty type list should not work.
diff --git a/test/compilation_failure/equality.cpp b/test/compilation_failure/equality.cpp
index b99a308..36a1990 100644
--- a/test/compilation_failure/equality.cpp
+++ b/test/compilation_failure/equality.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED:
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
int main()
{
diff --git a/test/compilation_failure/get_type.cpp b/test/compilation_failure/get_type.cpp
index 5123eb1..4903389 100644
--- a/test/compilation_failure/get_type.cpp
+++ b/test/compilation_failure/get_type.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED: enable_if
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
int main()
{
diff --git a/test/compilation_failure/is_type.cpp b/test/compilation_failure/is_type.cpp
index a79638e..95ccff4 100644
--- a/test/compilation_failure/is_type.cpp
+++ b/test/compilation_failure/is_type.cpp
@@ -1,7 +1,6 @@
+// @EXPECTED:
-// @EXPECTED: invalid type in T in `is<T>()` for this variant
-
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
int main()
{
diff --git a/test/compilation_failure/mutating_visitor_on_const.cpp b/test/compilation_failure/mutating_visitor_on_const.cpp
index ee77b56..1706cbf 100644
--- a/test/compilation_failure/mutating_visitor_on_const.cpp
+++ b/test/compilation_failure/mutating_visitor_on_const.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED: const int
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
struct mutating_visitor
{
diff --git a/test/compilation_failure/no-reference.cpp b/test/compilation_failure/no-reference.cpp
index 17123ce..2f547fc 100644
--- a/test/compilation_failure/no-reference.cpp
+++ b/test/compilation_failure/no-reference.cpp
@@ -1,7 +1,6 @@
-
// @EXPECTED: Variant can not hold reference types
-#include <variant.hpp>
+#include <mapbox/variant.hpp>
int main()
{
diff --git a/test/include/auto_cpu_timer.hpp b/test/include/auto_cpu_timer.hpp
new file mode 100644
index 0000000..b41935f
--- /dev/null
+++ b/test/include/auto_cpu_timer.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <chrono>
+#include <iostream>
+
+struct auto_cpu_timer {
+ std::chrono::time_point<std::chrono::high_resolution_clock> start;
+ auto_cpu_timer() : start(std::chrono::high_resolution_clock::now()) {
+ }
+ ~auto_cpu_timer() {
+ auto end = std::chrono::high_resolution_clock::now();
+ std::chrono::microseconds elapsed =
+ std::chrono::duration_cast<std::chrono::microseconds>(end - start);
+ std::cerr << elapsed.count() << "us" << std::endl;
+ }
+};
diff --git a/test/our_variant_hello_world.cpp b/test/our_variant_hello_world.cpp
index be5996b..07aeb62 100644
--- a/test/our_variant_hello_world.cpp
+++ b/test/our_variant_hello_world.cpp
@@ -1,4 +1,4 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#include <stdexcept>
diff --git a/test/recursive_wrapper_test.cpp b/test/recursive_wrapper_test.cpp
index 0492af4..57143cb 100644
--- a/test/recursive_wrapper_test.cpp
+++ b/test/recursive_wrapper_test.cpp
@@ -5,9 +5,9 @@
#include <typeinfo>
#include <utility>
-#include <boost/timer/timer.hpp>
+#include "auto_cpu_timer.hpp"
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
using namespace mapbox;
@@ -111,7 +111,7 @@ int main(int argc, char** argv)
int total = 0;
{
- boost::timer::auto_cpu_timer t;
+ auto_cpu_timer t;
for (std::size_t i = 0; i < NUM_ITER; ++i)
{
total += util::apply_visitor(test::calculator(), result);
diff --git a/test/reference_wrapper_test.cpp b/test/reference_wrapper_test.cpp
index dc1209f..18eaecb 100644
--- a/test/reference_wrapper_test.cpp
+++ b/test/reference_wrapper_test.cpp
@@ -6,7 +6,7 @@
#include <utility>
#include <vector>
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
using namespace mapbox;
diff --git a/test/t/binary_visitor_1.cpp b/test/t/binary_visitor_1.cpp
index 298a40b..3579226 100644
--- a/test/t/binary_visitor_1.cpp
+++ b/test/t/binary_visitor_1.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " i-d"
using variant_type = mapbox::util::variant<int, double>;
diff --git a/test/t/binary_visitor_2.cpp b/test/t/binary_visitor_2.cpp
index 33768b6..9863ac5 100644
--- a/test/t/binary_visitor_2.cpp
+++ b/test/t/binary_visitor_2.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " b-i-d"
using variant_type = mapbox::util::variant<bool, int, double>;
diff --git a/test/t/binary_visitor_3.cpp b/test/t/binary_visitor_3.cpp
index d35af4e..a1c367e 100644
--- a/test/t/binary_visitor_3.cpp
+++ b/test/t/binary_visitor_3.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " i-d-b"
using variant_type = mapbox::util::variant<int, double, bool>;
diff --git a/test/t/binary_visitor_4.cpp b/test/t/binary_visitor_4.cpp
index daacc1b..8cc66ab 100644
--- a/test/t/binary_visitor_4.cpp
+++ b/test/t/binary_visitor_4.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " b-i-d-c"
using variant_type = mapbox::util::variant<bool, int, double, char>;
diff --git a/test/t/binary_visitor_5.cpp b/test/t/binary_visitor_5.cpp
index 28669be..f6673c8 100644
--- a/test/t/binary_visitor_5.cpp
+++ b/test/t/binary_visitor_5.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " b-i-c-d-i"
using variant_type = mapbox::util::variant<bool, int, char, double, int>;
diff --git a/test/t/binary_visitor_6.cpp b/test/t/binary_visitor_6.cpp
index c881b0f..d259d73 100644
--- a/test/t/binary_visitor_6.cpp
+++ b/test/t/binary_visitor_6.cpp
@@ -1,5 +1,5 @@
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
#define NAME_EXT " b-i-i-d-c-u"
using variant_type = mapbox::util::variant<bool, int, int, double, char, short int>;
diff --git a/test/t/binary_visitor_impl.hpp b/test/t/binary_visitor_impl.hpp
index 4d9a43f..f2db68b 100644
--- a/test/t/binary_visitor_impl.hpp
+++ b/test/t/binary_visitor_impl.hpp
@@ -3,7 +3,7 @@
#include "catch.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant_io.hpp>
struct add_visitor
{
diff --git a/test/t/issue21.cpp b/test/t/issue21.cpp
index b952313..e979027 100644
--- a/test/t/issue21.cpp
+++ b/test/t/issue21.cpp
@@ -1,8 +1,8 @@
#include "catch.hpp"
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
// https://github.com/mapbox/variant/issues/21
diff --git a/test/t/mutating_visitor.cpp b/test/t/mutating_visitor.cpp
index f07afb8..cd4f162 100644
--- a/test/t/mutating_visitor.cpp
+++ b/test/t/mutating_visitor.cpp
@@ -1,8 +1,8 @@
#include "catch.hpp"
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
#include <string>
diff --git a/test/t/optional.cpp b/test/t/optional.cpp
index b77beda..8f08f7c 100644
--- a/test/t/optional.cpp
+++ b/test/t/optional.cpp
@@ -1,7 +1,7 @@
#include "catch.hpp"
-#include "optional.hpp"
+#include <mapbox/optional.hpp>
struct dummy
{
diff --git a/test/t/recursive_wrapper.cpp b/test/t/recursive_wrapper.cpp
index b2dec45..c3d53f0 100644
--- a/test/t/recursive_wrapper.cpp
+++ b/test/t/recursive_wrapper.cpp
@@ -1,7 +1,7 @@
#include "catch.hpp"
-#include "recursive_wrapper.hpp"
+#include <mapbox/recursive_wrapper.hpp>
#include <type_traits>
#include <utility>
diff --git a/test/t/sizeof.cpp b/test/t/sizeof.cpp
index 0e74ce5..607fabc 100644
--- a/test/t/sizeof.cpp
+++ b/test/t/sizeof.cpp
@@ -5,8 +5,8 @@
#include "catch.hpp"
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
struct some_struct
{
diff --git a/test/t/unary_visitor.cpp b/test/t/unary_visitor.cpp
index 8df6110..e447bfa 100644
--- a/test/t/unary_visitor.cpp
+++ b/test/t/unary_visitor.cpp
@@ -1,8 +1,8 @@
#include "catch.hpp"
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
#include <string>
diff --git a/test/t/variant.cpp b/test/t/variant.cpp
index 36655a5..a192269 100644
--- a/test/t/variant.cpp
+++ b/test/t/variant.cpp
@@ -1,7 +1,7 @@
#include "catch.hpp"
-#include "variant.hpp"
-#include "variant_io.hpp"
+#include <mapbox/variant.hpp>
+#include <mapbox/variant_io.hpp>
#include <algorithm>
#include <cstdint>
@@ -543,9 +543,9 @@ TEST_CASE("storing reference wrappers to consts works")
int a = 1;
variant_type v{std::cref(a)};
REQUIRE(v.get<int const>() == 1);
- REQUIRE(v.get<int>() == 1); // this works (see #82)
+ REQUIRE(v.get<int>() == 1);
REQUIRE(mapbox::util::get<int const>(v) == 1);
- // REQUIRE(mapbox::util::get<int>(v) == 1); // this doesn't work (see #82)
+ REQUIRE(mapbox::util::get<int>(v) == 1);
REQUIRE_THROWS_AS({
v.get<double const>();
},
@@ -568,3 +568,26 @@ TEST_CASE("storing reference wrappers to consts works")
},
mapbox::util::bad_variant_access&);
}
+
+TEST_CASE("recursive wrapper")
+{
+ using variant_type = mapbox::util::variant<mapbox::util::recursive_wrapper<int>>;
+ variant_type v(1);
+ REQUIRE(v.is<int>());
+ REQUIRE(v.get<int>() == 1);
+}
+
+
+TEST_CASE("variant : direct_type helper should match T, references (T&) and const references (T const&) to the original type T)")
+{
+ using value = mapbox::util::variant<bool, std::uint64_t>;
+
+ std::uint64_t u(1234);
+ REQUIRE(value(u).is<std::uint64_t>()); // matches T
+
+ std::uint64_t& ur(u);
+ REQUIRE(value(ur).is<std::uint64_t>()); // matches T&
+
+ std::uint64_t const& ucr(u);
+ REQUIRE(value(ucr).is<std::uint64_t>()); // matches T const&
+}
diff --git a/test/unique_ptr_test.cpp b/test/unique_ptr_test.cpp
index 6578991..f075900 100644
--- a/test/unique_ptr_test.cpp
+++ b/test/unique_ptr_test.cpp
@@ -6,9 +6,9 @@
#include <typeinfo>
#include <utility>
-#include <boost/timer/timer.hpp>
+#include "auto_cpu_timer.hpp"
-#include "variant.hpp"
+#include <mapbox/variant.hpp>
using namespace mapbox;
@@ -112,7 +112,7 @@ int main(int argc, char** argv)
int total = 0;
{
- boost::timer::auto_cpu_timer t;
+ auto_cpu_timer t;
for (std::size_t i = 0; i < NUM_ITER; ++i)
{
total += util::apply_visitor(test::calculator(), result);
diff --git a/variant.gyp b/variant.gyp
index b1f3801..7d7dca8 100644
--- a/variant.gyp
+++ b/variant.gyp
@@ -27,7 +27,7 @@
"SUPPORTED_PLATFORMS":["macosx"]
},
"include_dirs": [
- "./",
+ "./include",
"test/include"
]
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/mapbox-variant.git
More information about the Pkg-grass-devel
mailing list