Skip to content

Commit 64a4747

Browse files
committed
Fix #2727: improve setTokenFactory in Cpp target
Change the setTokenFactory template function in the Lexer, Parser, and TokenSource classes to be parameterized by Factory type rather than by token type. Change the setTokenFactory signature to require the function argument to be a Ref of the Factory template parameter type so it can be correctly converted and assigned to the token factory member. The type of that member is Ref<TokenFactory<CommonToken>>, which implies that the Factory template parameter must be of a type convertible to TokenFactory<CommonToken>.
1 parent 7a3f40b commit 64a4747

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

runtime/Cpp/runtime/src/Lexer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ namespace antlr4 {
9595
virtual void pushMode(size_t m);
9696
virtual size_t popMode();
9797

98-
template<typename T1>
99-
void setTokenFactory(TokenFactory<T1> *factory) {
100-
this->_factory = factory;
98+
template<typename Factory>
99+
void setTokenFactory(Ref<Factory> const& factory) {
100+
this->_factory = std::static_pointer_cast<TokenFactory<CommonToken>>(factory);
101101
}
102102

103103
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;

runtime/Cpp/runtime/src/Parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ namespace antlr4 {
197197

198198
/// <summary>
199199
/// Tell our token source and error strategy about a new way to create tokens. </summary>
200-
template<typename T1>
201-
void setTokenFactory(TokenFactory<T1> *factory) {
200+
template<typename Factory>
201+
void setTokenFactory(Ref<Factory> const& factory) {
202202
_input->getTokenSource()->setTokenFactory(factory);
203203
}
204204

runtime/Cpp/runtime/src/TokenSource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ namespace antlr4 {
7171
/// <seealso cref="Token"/> objects from the input.
7272
/// </summary>
7373
/// <param name="factory"> The <seealso cref="TokenFactory"/> to use for creating tokens. </param>
74-
template<typename T1>
75-
void setTokenFactory(TokenFactory<T1> * /*factory*/) {}
74+
template<typename Factory>
75+
void setTokenFactory(Ref<Factory> const& /*factory*/) {}
7676

7777
/// <summary>
7878
/// Gets the <seealso cref="TokenFactory"/> this token source is currently using for

0 commit comments

Comments
 (0)