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.
This commit is contained in:
Steve Vinoski 2020-01-22 13:58:46 -05:00
parent 7a3f40bc34
commit 38200b6e31
10 changed files with 14 additions and 14 deletions

View File

@ -11,7 +11,7 @@
using namespace antlr4;
const Ref<TokenFactory<CommonToken>> CommonTokenFactory::DEFAULT = std::make_shared<CommonTokenFactory>();
const std::unique_ptr<TokenFactory<CommonToken>> CommonTokenFactory::DEFAULT(new CommonTokenFactory);
CommonTokenFactory::CommonTokenFactory(bool copyText_) : copyText(copyText_) {
}

View File

@ -22,7 +22,7 @@ namespace antlr4 {
* This token factory does not explicitly copy token text when constructing
* tokens.</p>
*/
static const Ref<TokenFactory<CommonToken>> DEFAULT;
static const std::unique_ptr<TokenFactory<CommonToken>> DEFAULT;
protected:
/**

View File

@ -136,7 +136,7 @@ size_t Lexer::popMode() {
}
Ref<TokenFactory<CommonToken>> Lexer::getTokenFactory() {
TokenFactory<CommonToken>* Lexer::getTokenFactory() {
return _factory;
}
@ -284,7 +284,7 @@ size_t Lexer::getNumberOfSyntaxErrors() {
void Lexer::InitializeInstanceFields() {
_syntaxErrors = 0;
token = nullptr;
_factory = CommonTokenFactory::DEFAULT;
_factory = CommonTokenFactory::DEFAULT.get();
tokenStartCharIndex = INVALID_INDEX;
tokenStartLine = 0;
tokenStartCharPositionInLine = 0;

View File

@ -31,7 +31,7 @@ namespace antlr4 {
protected:
/// How to create token objects.
Ref<TokenFactory<CommonToken>> _factory;
TokenFactory<CommonToken> *_factory;
public:
/// The goal of all lexer rules/methods is to create a token object.
@ -100,7 +100,7 @@ namespace antlr4 {
this->_factory = factory;
}
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
virtual TokenFactory<CommonToken>* getTokenFactory() override;
/// Set the char stream and reset the lexer
virtual void setInputStream(IntStream *input) override;

View File

@ -82,11 +82,11 @@ std::string ListTokenSource::getSourceName() {
return "List";
}
Ref<TokenFactory<CommonToken>> ListTokenSource::getTokenFactory() {
TokenFactory<CommonToken>* ListTokenSource::getTokenFactory() {
return _factory;
}
void ListTokenSource::InitializeInstanceFields() {
i = 0;
_factory = CommonTokenFactory::DEFAULT;
_factory = CommonTokenFactory::DEFAULT.get();
}

View File

@ -40,7 +40,7 @@ namespace antlr4 {
private:
/// This is the backing field for <seealso cref="#getTokenFactory"/> and
/// <seealso cref="setTokenFactory"/>.
Ref<TokenFactory<CommonToken>> _factory = CommonTokenFactory::DEFAULT;
TokenFactory<CommonToken> *_factory = CommonTokenFactory::DEFAULT.get();
public:
/// Constructs a new <seealso cref="ListTokenSource"/> instance from the specified
@ -79,7 +79,7 @@ namespace antlr4 {
this->_factory = factory;
}
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
virtual TokenFactory<CommonToken>* getTokenFactory() override;
private:
void InitializeInstanceFields();

View File

@ -208,7 +208,7 @@ size_t Parser::getNumberOfSyntaxErrors() {
return _syntaxErrors;
}
Ref<TokenFactory<CommonToken>> Parser::getTokenFactory() {
TokenFactory<CommonToken>* Parser::getTokenFactory() {
return _input->getTokenSource()->getTokenFactory();
}

View File

@ -193,7 +193,7 @@ namespace antlr4 {
/// <seealso cref= #notifyErrorListeners </seealso>
virtual size_t getNumberOfSyntaxErrors();
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() override;
virtual TokenFactory<CommonToken>* getTokenFactory() override;
/// <summary>
/// Tell our token source and error strategy about a new way to create tokens. </summary>

View File

@ -138,7 +138,7 @@ namespace antlr4 {
virtual void setInputStream(IntStream *input) = 0;
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() = 0;
virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
template<typename T1>
void setTokenFactory(TokenFactory<T1> *input);

View File

@ -79,7 +79,7 @@ namespace antlr4 {
/// creating <seealso cref="Token"/> objects from the input.
/// </summary>
/// <returns> The <seealso cref="TokenFactory"/> currently used by this token source. </returns>
virtual Ref<TokenFactory<CommonToken>> getTokenFactory() = 0;
virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
};
} // namespace antlr4