Skip to content

Commit 63b7d6d

Browse files
committed
Make use of concepts replacing template magic
1 parent ddbdd12 commit 63b7d6d

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

include/helper/ConsolePrinter.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ class ConsolePrinter {
1616
bool last_was_endl = true;
1717

1818
public:
19-
template <class T, std::enable_if_t<!ConsolePrinter::is_printer<T>, bool> = true>
19+
template <class T>
20+
requires(!ConsolePrinter::is_printer<T>)
2021
ConsolePrinter &operator<<(const T &output);
2122
ConsolePrinter &operator<<(const NestedPrinter &nested_printer);
2223
ConsolePrinter &operator<<(const EndlPrinter &endl_printer);
2324

2425
friend void endl(ConsolePrinter &printer);
2526
};
2627

27-
template <class T, std::enable_if_t<!ConsolePrinter::is_printer<T>, bool>>
28+
template <class T>
29+
requires(!ConsolePrinter::is_printer<T>)
2830
ConsolePrinter &ConsolePrinter::operator<<(const T &output) {
2931
if (last_was_endl) {
3032
std::cout << std::string(2 * indent, ' ');

include/lexer/Lexer.hpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#include <string>
1111
#include <type_traits>
1212

13-
template <class InputIterator>
13+
template <class I>
14+
concept CharIterator = std::same_as<typename std::iterator_traits<I>::value_type, char>;
15+
16+
template <CharIterator InputIterator>
1417
class Lexer {
1518

1619
public:
@@ -21,9 +24,6 @@ class Lexer {
2124
using reference = Token &;
2225

2326
private:
24-
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type, char>(),
25-
"Expecting iterator over 'char' type.");
26-
2727
InputIterator current_character;
2828
InputIterator characters_end;
2929

@@ -42,13 +42,14 @@ class Lexer {
4242
private:
4343
std::optional<Token> next();
4444
template <class TokenMatcher>
45+
requires std::is_invocable_r_v<bool, TokenMatcher, char>
4546
std::string get_while_matching(const TokenMatcher &matcher);
4647

4748
static bool is_operator(char c);
4849
static bool is_identifier(char c);
4950
};
5051

51-
template <class InputIterator>
52+
template <CharIterator InputIterator>
5253
Lexer<InputIterator>::Lexer(InputIterator begin, InputIterator end)
5354
: current_character(begin)
5455
, characters_end(end) {
@@ -57,34 +58,34 @@ Lexer<InputIterator>::Lexer(InputIterator begin, InputIterator end)
5758
}
5859
}
5960

60-
template <class InputIterator>
61+
template <CharIterator InputIterator>
6162
Token Lexer<InputIterator>::operator*() const {
6263
return *current_token;
6364
}
6465

65-
template <class InputIterator>
66+
template <CharIterator InputIterator>
6667
Token &Lexer<InputIterator>::operator++() {
6768
return *(current_token = next());
6869
}
6970

70-
template <class InputIterator>
71+
template <CharIterator InputIterator>
7172
Token Lexer<InputIterator>::operator++(int) {
7273
auto tmp_token = std::move(current_token);
7374
++(*this);
7475
return *tmp_token;
7576
}
7677

77-
template <class InputIterator>
78+
template <CharIterator InputIterator>
7879
bool Lexer<InputIterator>::operator==(const Lexer &other) const {
7980
return current_token.has_value() == other.current_token.has_value();
8081
}
8182

82-
template <class InputIterator>
83+
template <CharIterator InputIterator>
8384
bool Lexer<InputIterator>::operator!=(const Lexer &other) const {
8485
return !(*this == other);
8586
}
8687

87-
template <class InputIterator>
88+
template <CharIterator InputIterator>
8889
std::optional<Token> Lexer<InputIterator>::next() {
8990
while (current_character != characters_end) {
9091
if (isspace(*current_character) != 0) {
@@ -123,8 +124,9 @@ std::optional<Token> Lexer<InputIterator>::next() {
123124
return {};
124125
}
125126

126-
template <class InputIterator>
127+
template <CharIterator InputIterator>
127128
template <class TokenMatcher>
129+
requires std::is_invocable_r_v<bool, TokenMatcher, char>
128130
std::string Lexer<InputIterator>::get_while_matching(const TokenMatcher &matcher) {
129131
std::string value;
130132
do {
@@ -133,12 +135,12 @@ std::string Lexer<InputIterator>::get_while_matching(const TokenMatcher &matcher
133135
return value;
134136
}
135137

136-
template <class InputIterator>
138+
template <CharIterator InputIterator>
137139
bool Lexer<InputIterator>::is_operator(const char c) {
138140
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
139141
}
140142

141-
template <class InputIterator>
143+
template <CharIterator InputIterator>
142144
bool Lexer<InputIterator>::is_identifier(const char c) {
143145
return (isalnum(c) != 0) || c == '_';
144146
}

0 commit comments

Comments
 (0)