Skip to content

Commit 38200b6

Browse files
committed
Fix #2727: improve setTokenFactory in Cpp target
Change the setTokenFactory template functions and the getTokenFactory functions to use a plain pointer rather than a Ref. This makes the caller of setTokenFactory responsible for managing the lifetime and memory of the token factory instance they pass in. Change CommonTokenFactory::DEFAULT to be a unique_ptr, and correct all places using it as a Ref.
1 parent 7a3f40b commit 38200b6

File tree

10 files changed

+14
-14
lines changed

10 files changed

+14
-14
lines changed

runtime/Cpp/runtime/src/CommonTokenFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
using namespace antlr4;
1313

14-
const Ref<TokenFactory<CommonToken>> CommonTokenFactory::DEFAULT = std::make_shared<CommonTokenFactory>();
14+
const std::unique_ptr<TokenFactory<CommonToken>> CommonTokenFactory::DEFAULT(new CommonTokenFactory);
1515

1616
CommonTokenFactory::CommonTokenFactory(bool copyText_) : copyText(copyText_) {
1717
}

runtime/Cpp/runtime/src/CommonTokenFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace antlr4 {
2222
* This token factory does not explicitly copy token text when constructing
2323
* tokens.</p>
2424
*/
25-
static const Ref<TokenFactory<CommonToken>> DEFAULT;
25+
static const std::unique_ptr<TokenFactory<CommonToken>> DEFAULT;
2626

2727
protected:
2828
/**

runtime/Cpp/runtime/src/Lexer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ size_t Lexer::popMode() {
136136
}
137137

138138

139-
Ref<TokenFactory<CommonToken>> Lexer::getTokenFactory() {
139+
TokenFactory<CommonToken>* Lexer::getTokenFactory() {
140140
return _factory;
141141
}
142142

@@ -284,7 +284,7 @@ size_t Lexer::getNumberOfSyntaxErrors() {
284284
void Lexer::InitializeInstanceFields() {
285285
_syntaxErrors = 0;
286286
token = nullptr;
287-
_factory = CommonTokenFactory::DEFAULT;
287+
_factory = CommonTokenFactory::DEFAULT.get();
288288
tokenStartCharIndex = INVALID_INDEX;
289289
tokenStartLine = 0;
290290
tokenStartCharPositionInLine = 0;

runtime/Cpp/runtime/src/Lexer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace antlr4 {
3131

3232
protected:
3333
/// How to create token objects.
34-
Ref<TokenFactory<CommonToken>> _factory;
34+
TokenFactory<CommonToken> *_factory;
3535

3636
public:
3737
/// The goal of all lexer rules/methods is to create a token object.
@@ -100,7 +100,7 @@ namespace antlr4 {
100100
this->_factory = factory;
101101
}
102102

103-
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
103+
virtual TokenFactory<CommonToken>* getTokenFactory() override;
104104

105105
/// Set the char stream and reset the lexer
106106
virtual void setInputStream(IntStream *input) override;

runtime/Cpp/runtime/src/ListTokenSource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ std::string ListTokenSource::getSourceName() {
8282
return "List";
8383
}
8484

85-
Ref<TokenFactory<CommonToken>> ListTokenSource::getTokenFactory() {
85+
TokenFactory<CommonToken>* ListTokenSource::getTokenFactory() {
8686
return _factory;
8787
}
8888

8989
void ListTokenSource::InitializeInstanceFields() {
9090
i = 0;
91-
_factory = CommonTokenFactory::DEFAULT;
91+
_factory = CommonTokenFactory::DEFAULT.get();
9292
}

runtime/Cpp/runtime/src/ListTokenSource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace antlr4 {
4040
private:
4141
/// This is the backing field for <seealso cref="#getTokenFactory"/> and
4242
/// <seealso cref="setTokenFactory"/>.
43-
Ref<TokenFactory<CommonToken>> _factory = CommonTokenFactory::DEFAULT;
43+
TokenFactory<CommonToken> *_factory = CommonTokenFactory::DEFAULT.get();
4444

4545
public:
4646
/// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
@@ -79,7 +79,7 @@ namespace antlr4 {
7979
this->_factory = factory;
8080
}
8181

82-
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
82+
virtual TokenFactory<CommonToken>* getTokenFactory() override;
8383

8484
private:
8585
void InitializeInstanceFields();

runtime/Cpp/runtime/src/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ size_t Parser::getNumberOfSyntaxErrors() {
208208
return _syntaxErrors;
209209
}
210210

211-
Ref<TokenFactory<CommonToken>> Parser::getTokenFactory() {
211+
TokenFactory<CommonToken>* Parser::getTokenFactory() {
212212
return _input->getTokenSource()->getTokenFactory();
213213
}
214214

runtime/Cpp/runtime/src/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace antlr4 {
193193
/// <seealso cref= #notifyErrorListeners </seealso>
194194
virtual size_t getNumberOfSyntaxErrors();
195195

196-
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
196+
virtual TokenFactory<CommonToken>* getTokenFactory() override;
197197

198198
/// <summary>
199199
/// Tell our token source and error strategy about a new way to create tokens. </summary>

runtime/Cpp/runtime/src/Recognizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace antlr4 {
138138

139139
virtual void setInputStream(IntStream *input) = 0;
140140

141-
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() = 0;
141+
virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
142142

143143
template<typename T1>
144144
void setTokenFactory(TokenFactory<T1> *input);

runtime/Cpp/runtime/src/TokenSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace antlr4 {
7979
/// creating <seealso cref="Token"/> objects from the input.
8080
/// </summary>
8181
/// <returns> The <seealso cref="TokenFactory"/> currently used by this token source. </returns>
82-
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() = 0;
82+
virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
8383
};
8484

8585
} // namespace antlr4

0 commit comments

Comments
 (0)