Skip to content

Add Transparent Comparator Support to variables_map #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions include/boost/program_options/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#if BOOST_VERSION >= 103100 // works beginning from Boost V1.31.0

///////////////////////////////////////////////////////////////////////////////
// enable automatic library variant selection
// enable automatic library variant selection
#if !defined(BOOST_PROGRAM_OPTIONS_SOURCE) && !defined(BOOST_ALL_NO_LIB) && \
!defined(BOOST_PROGRAM_OPTIONS_NO_LIB)

Expand Down Expand Up @@ -47,6 +47,4 @@
#define BOOST_PROGRAM_OPTIONS_DECL
#endif


#endif // PROGRAM_OPTIONS_CONFIG_HK_2004_01_11

13 changes: 12 additions & 1 deletion include/boost/program_options/variables_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,24 @@ namespace boost { namespace program_options {
const abstract_variables_map* m_next;
};

namespace details
{
typedef
#if !defined(BOOST_PROGRAM_OPTIONS_NO_TRANSPARENT_COMPARATOR) && BOOST_CXX_VERSION >= 201402L
std::less<>
#else
std::less<std::string>
#endif
comparator_t;
}

/** Concrete variables map which store variables in real map.

This class is derived from std::map<std::string, variable_value>,
so you can use all map operators to examine its content.
*/
class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,
public std::map<std::string, variable_value>
public std::map<std::string, variable_value, details::comparator_t>
{
public:
variables_map();
Expand Down
8 changes: 4 additions & 4 deletions src/variables_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace boost { namespace program_options {

// We need to access map's operator[], not the overriden version
// variables_map. Ehmm.. messy.
std::map<std::string, variable_value>& m = xm;
std::map<std::string, variable_value, details::comparator_t>& m = xm;

std::set<std::string> new_final;

Expand Down Expand Up @@ -194,7 +194,7 @@ namespace boost { namespace program_options {

void variables_map::clear()
{
std::map<std::string, variable_value>::clear();
std::map<std::string, variable_value, details::comparator_t>::clear();
m_final.clear();
m_required.clear();
}
Expand All @@ -220,7 +220,7 @@ namespace boost { namespace program_options {
{
const string& opt = r->first;
const string& display_opt = r->second;
map<string, variable_value>::const_iterator iter = find(opt);
map<string, variable_value, details::comparator_t>::const_iterator iter = find(opt);
if (iter == end() || iter->second.empty())
{
boost::throw_exception(required_option(display_opt));
Expand All @@ -229,7 +229,7 @@ namespace boost { namespace program_options {
}

// Lastly, run notify actions.
for (map<string, variable_value>::iterator k = begin();
for (map<string, variable_value, details::comparator_t>::iterator k = begin();
k != end();
++k)
{
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ boost_test(TYPE run SOURCES unrecognized_test.cpp)
boost_test(TYPE run SOURCES required_test.cpp ARGUMENTS ${CMAKE_CURRENT_SOURCE_DIR}/required_test.cfg)
boost_test(TYPE run SOURCES exception_txt_test.cpp)
boost_test(TYPE run SOURCES optional_test.cpp)
boost_test(TYPE run SOURCES transparent_comparator_test.cpp)

boost_test(TYPE run SOURCES quick.cpp ARGUMENTS --path=initial LINK_LIBRARIES Boost::core)
56 changes: 56 additions & 0 deletions test/transparent_comparator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <boost/config.hpp>
#include <boost/core/noncopyable.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/utility/string_view.hpp>

#ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
#include <string_view>
#endif

namespace {
// Contrived class that proves no extra copies are being made and only operator< is being used
struct Dummy : private boost::noncopyable
{};

bool operator<(const Dummy&, const std::string& rhs) { return "test" < rhs; }
bool operator<(const std::string& lhs, const Dummy&) { return lhs < "test"; }
}

void test_transparent_comparator()
{
boost::program_options::variables_map vm;
vm.insert({"test", boost::program_options::variable_value(42, false)});

boost::program_options::variables_map::iterator iter;

// c-string literal.
// If pre c++14, a std::string object will be implicitly created on the call to find()
// after c++14 no extra object will be created
iter = vm.find("test");
BOOST_REQUIRE(iter != vm.end());
BOOST_CHECK(boost::any_cast<int>(iter->second.value()) == 42);

// Rest of the checks require c++14 for a transparent comparator
#ifdef BOOST_CXX_VERSION > 201402L
// boost::string_view
const boost::string_view bsv("test");
iter = vm.find(bsv);
BOOST_REQUIRE(iter != vm.end());
BOOST_CHECK(boost::any_cast<int>(iter->second.value()) == 42);

#ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
// std::string_view
const std::string_view sv("test");
iter = vm.find(sv);
BOOST_REQUIRE(iter != vm.end());
BOOST_CHECK(boost::any_cast<int>(iter->second.value()) == 42);
#endif

// custom class
Dummy d;
iter = vm.find(d);
BOOST_REQUIRE(iter != vm.end());
BOOST_CHECK(boost::any_cast<int>(iter->second.value()) == 42);
#endif
}