Skip to content

Commit

Permalink
[FEATURE] Add sharg::istreamable concept. (#53)
Browse files Browse the repository at this point in the history
* [MISC] Move argument_parser_compatible_option to concept.hpp

* [FEATURE] Add sharg istreamable concept.

* [MISC] Remove dependency on seqan3::input_stream_over.

* add return type constraint

* Update include/sharg/concept.hpp

* Update include/sharg/concept.hpp

* remove todo

Co-authored-by: Enrico Seiler <[email protected]>
  • Loading branch information
smehringer and eseiler authored Mar 9, 2022
1 parent 5db7c35 commit 3acbd1d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
19 changes: 0 additions & 19 deletions include/sharg/auxiliary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <seqan3/core/debug_stream/debug_stream_type.hpp>
#include <seqan3/core/detail/customisation_point.hpp>
#include <seqan3/io/stream/concept.hpp>

#include <sharg/platform.hpp>

Expand Down Expand Up @@ -186,24 +185,6 @@ concept named_enumeration = requires
{ sharg::enumeration_names<option_type> };
};

/*!\concept sharg::argument_parser_compatible_option
* \brief Checks whether the the type can be used in an add_(positional_)option call on the argument parser.
* \ingroup argument_parser
* \tparam option_type The type to check.
*
* ### Requirements
*
* In order to model this concept, the type must either be streamable to std::istringstream or
* model sharg::named_enumeration<option_type>.
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename option_type>
concept argument_parser_compatible_option = seqan3::input_stream_over<std::istringstream, option_type> ||
named_enumeration<option_type>;



/*!\brief Used to further specify argument_parser options/flags.
* \ingroup argument_parser
*
Expand Down
52 changes: 52 additions & 0 deletions include/sharg/concept.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// -----------------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/sharg-parser/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------------

/*!\file
* \author Svenja Mehringer <svenja.mehringer AT fu-berlin.de>
* \brief Provides helper concepts.
*/

#pragma once

#include <seqan3/std/concepts>

#include <sharg/auxiliary.hpp>

namespace sharg
{

/*!\concept sharg::istreamable
* \ingroup argument_parser
* \brief Concept for types that can be parsed from a std::istream via the stream operator.
* \tparam value_type The type to check whether it's stremable via std::istream.
*
* ### Requirements
*
* `std::istream` must support the (un)formatted input function (`operator>>`) for an l-value of a given `value_type`.
*/
template <typename value_type>
concept istreamable = requires (std::istream & is, value_type & val)
{
SHARG_RETURN_TYPE_CONSTRAINT(is >> val, std::same_as, std::istream&);
};

/*!\concept sharg::argument_parser_compatible_option
* \brief Checks whether the the type can be used in an add_(positional_)option call on the argument parser.
* \ingroup argument_parser
* \tparam option_type The type to check.
*
* ### Requirements
*
* In order to model this concept, the type must either model sharg::istreamable and sharg::ostreamable or
* model sharg::named_enumeration<option_type>.
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename option_type>
concept argument_parser_compatible_option = sharg::istreamable<option_type> || named_enumeration<option_type>;

} // namespace sharg
7 changes: 4 additions & 3 deletions include/sharg/detail/format_parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sharg/std/charconv>

#include <sharg/detail/format_base.hpp>
#include <sharg/concept.hpp>

namespace sharg::detail
{
Expand Down Expand Up @@ -284,15 +285,15 @@ class format_parse : public format_base
}

/*!\brief Tries to parse an input string into a value using the stream `operator>>`.
* \tparam option_t Must model seqan3::input_stream_over.
* \tparam option_t Must model sharg::istreamable.
* \param[out] value Stores the parsed value.
* \param[in] in The input argument to be parsed.
* \returns sharg::option_parse_result::error if `in` could not be parsed via the stream
* operator and otherwise sharg::option_parse_result::success.
*/
template <typename option_t>
//!\cond
requires seqan3::input_stream_over<std::istringstream, option_t>
requires istreamable<option_t>
//!\endcond
option_parse_result parse_option_value(option_t & value, std::string const & in)
{
Expand Down Expand Up @@ -407,7 +408,7 @@ class format_parse : public format_base
*/
template <typename option_t>
//!\cond
requires std::is_arithmetic_v<option_t> && seqan3::input_stream_over<std::istringstream, option_t>
requires std::is_arithmetic_v<option_t> && istreamable<option_t>
//!\endcond
option_parse_result parse_option_value(option_t & value, std::string const & in)
{
Expand Down

1 comment on commit 3acbd1d

@vercel
Copy link

@vercel vercel bot commented on 3acbd1d Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.