Skip to content

Commit

Permalink
Refine ctor definition of CommandParser (apache#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Jan 26, 2024
1 parent 201ba83 commit 4bb6954
Showing 1 changed file with 17 additions and 3 deletions.
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

0 comments on commit 4bb6954

Please sign in to comment.