Skip to content
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

Refine ctor definition of CommandParser #2044

Merged
merged 10 commits into from
Jan 26, 2024
20 changes: 17 additions & 3 deletions src/commands/command_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
#include <cctype>
#include <functional>
#include <iterator>
#include <type_traits>

#include "parse_util.h"
#include "status.h"
#include "string_util.h"
#include "type_util.h"

template <typename Iter>
struct MoveIterator : Iter {
Expand All @@ -46,17 +48,29 @@ struct CommandParser {

CommandParser(Iter begin, Iter end) : begin_(std::move(begin)), end_(std::move(end)) {}

template <typename Container>
explicit CommandParser(const Container& con, size_t skip_num = 0) : CommandParser(std::begin(con), std::end(con)) {
template <typename Container, std::enable_if_t<std::is_lvalue_reference_v<Container> &&
!std::is_same_v<RemoveCVRef<Container>, CommandParser>,
int> = 0>
explicit CommandParser(Container&& con, size_t skip_num = 0) : CommandParser(std::begin(con), std::end(con)) {
std::advance(begin_, skip_num);
}

template <typename Container>
template <typename Container, std::enable_if_t<!std::is_lvalue_reference_v<Container> &&
!std::is_same_v<RemoveCVRef<Container>, CommandParser>,
int> = 0>
explicit CommandParser(Container&& con, size_t skip_num = 0)
: CommandParser(MoveIterator(std::begin(con)), MoveIterator(std::end(con))) {
std::advance(begin_, skip_num);
}

CommandParser(const CommandParser&) = default;
CommandParser(CommandParser&&) noexcept = default;

CommandParser& operator=(const CommandParser&) = default;
CommandParser& operator=(CommandParser&&) noexcept = default;

~CommandParser() = default;

decltype(auto) RawPeek() const { return *begin_; }

decltype(auto) operator[](size_t index) const {
Expand Down
Loading