Skip to content
This repository has been archived by the owner on May 13, 2019. It is now read-only.

Commit

Permalink
testing: replace boost::program_options by cxxopts
Browse files Browse the repository at this point in the history
The new library is header only.  This is important, because otherwise it
is harder to change compiler -- one would need to have `boost` compiled
for every compiler under test.
  • Loading branch information
arximboldi committed Aug 18, 2015
1 parent ca62bab commit 73c479a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -1,3 +1,6 @@
[submodule "third-party/eggs-variant"]
path = third-party/eggs-variant
url = https://github.com/eggs-cpp/variant.git
[submodule "third-party/cxxopts"]
path = third-party/cxxopts
url = https://github.com/jarro2783/cxxopts.git
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -53,7 +53,7 @@ enable_testing()
if(DEFINED Boost_INCLUDE_DIR)
get_filename_component(Boost_INCLUDE_DIR ${Boost_INCLUDE_DIR} ABSOLUTE)
endif()
find_package(Boost 1.54 COMPONENTS program_options REQUIRED)
find_package(Boost 1.54 REQUIRED)

set(GTEST_SOURCE
"${CMAKE_CURRENT_SOURCE_DIR}/../googletest"
Expand Down Expand Up @@ -82,6 +82,7 @@ target_include_directories(atria PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/compat>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party/eggs-variant/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party/cxxopts/src>
${Boost_INCLUDE_DIRS})
target_compile_options(atria
PUBLIC
Expand Down
60 changes: 31 additions & 29 deletions src/atria/testing/benchmark.cpp
Expand Up @@ -6,8 +6,7 @@

#include <ableton/build_system/Warnings.hpp>
ABL_DISABLE_WARNINGS
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <cxxopts.hpp>
ABL_RESTORE_WARNINGS

#include <vector>
Expand Down Expand Up @@ -55,41 +54,44 @@ benchmark_suite_base::~benchmark_suite_base()

} // namespace detail

namespace {

auto optv(bool& x)
-> ABL_DECLTYPE_RETURN(
cxxopts::value<bool>(x))

template <typename T>
auto optv(T& x)
-> ABL_DECLTYPE_RETURN(
cxxopts::value<T>(x)
->default_value(std::to_string(x)))

} // namespace

benchmark_runner::benchmark_runner(int argc, char const*const* argv,
benchmark_settings settings)
: settings_(std::move(settings))
{
#define ABL_BENCHMARK_OPTION_S_(name__, sname__, desc__) \
(#name__ "," #sname__, \
::boost::program_options::value<decltype(settings_.name__)>( \
&settings_.name__) \
->default_value(settings_.name__) \
->value_name("<value>"), \
desc__) \
/**/

namespace po = boost::program_options;
auto desc = po::options_description{};
desc.add_options()
("help,h", "print help message")
ABL_BENCHMARK_OPTION_S_(
iterations, i, "number of times to run the benchmark per measurement")
ABL_BENCHMARK_OPTION_S_(
measurements, m, "number of times to measure each benchmark")
ABL_BENCHMARK_OPTION_S_(
size, s, "amount of data to pass to each benchmark")
auto opts = cxxopts::Options{ argv[0], "" };
auto help = bool{};

opts.add_options()
("h,help", "print help message",
optv(help))
("v,verbose", "show more output",
optv(settings_.verbose))
("i,iterations", "number of times to run the benchmark per measurement",
optv(settings_.iterations), "NUM")
("m,measurements", "number of times to measure each benchmark",
optv(settings_.measurements), "NUM")
;

auto args = po::variables_map{};
po::store(po::parse_command_line(argc, argv, desc), args);
po::notify(args);
auto argv_ = const_cast<char**>(argv);
opts.parse(argc, argv_);

if (args.count("help")) {
throw benchmark_runner_error {
boost::lexical_cast<std::string>(desc) };
if (help) {
throw benchmark_runner_error{ opts.help() };
}

#undef ABL_BENCHMARK_OPTION_S_
}

} // namespace testing
Expand Down
1 change: 1 addition & 0 deletions third-party/cxxopts
Submodule cxxopts added at 884433

0 comments on commit 73c479a

Please sign in to comment.