diff options
author | Jaromir Wysoglad | 2019-07-18 11:27:17 +0200 |
---|---|---|
committer | Filippos Karapetis | 2019-09-01 22:47:55 +0300 |
commit | 5d9f03e71d75a307c8f8e538803fe2f09f38c5ae (patch) | |
tree | e472499cd024f74e211dc6cd63c578d300f20e92 /common | |
parent | 8e4a24f55e2986de6c0556ab1a0faeeca20c724f (diff) | |
download | scummvm-rg350-5d9f03e71d75a307c8f8e538803fe2f09f38c5ae.tar.gz scummvm-rg350-5d9f03e71d75a307c8f8e538803fe2f09f38c5ae.tar.bz2 scummvm-rg350-5d9f03e71d75a307c8f8e538803fe2f09f38c5ae.zip |
TTS: Add reference counting to TTSVoice
Also refactor TTSVoice destruction to use this reference counting.
Diffstat (limited to 'common')
-rw-r--r-- | common/text-to-speech.cpp | 56 | ||||
-rw-r--r-- | common/text-to-speech.h | 22 |
2 files changed, 65 insertions, 13 deletions
diff --git a/common/text-to-speech.cpp b/common/text-to-speech.cpp index e8e0d9b870..ce9f4a9076 100644 --- a/common/text-to-speech.cpp +++ b/common/text-to-speech.cpp @@ -22,9 +22,62 @@ #include "common/text-to-speech.h" +#include "common/system.h" #if defined(USE_TTS) namespace Common { + +TTSVoice::TTSVoice() + : _gender(UNKNOWN_GENDER) + , _age(UNKNOWN_AGE) + , _data(nullptr) + , _description("") { + _refCount = new int; + *_refCount = 1; +} + +TTSVoice::TTSVoice(Gender gender, Age age, void *data, String description) + : _gender(gender) + , _age(age) + , _data(data) + , _description(description) { + _refCount = new int; + *_refCount = 1; +} + +TTSVoice::TTSVoice(const TTSVoice& voice) + : _gender(voice._gender) + , _age(voice._age) + , _data(voice._data) + , _refCount(voice._refCount) + , _description(voice._description) { + if (_data) + (*_refCount)++; +} + +TTSVoice::~TTSVoice() { + // _data is a platform specific field and so it the + // way it is freed differs from platform to platform + if (--(*_refCount) == 0) { + if (_data) + g_system->getTextToSpeechManager()->freeVoiceData(_data); + delete _refCount; + } +} + +TTSVoice& TTSVoice::operator=(const TTSVoice& voice) { + if (&voice != this) { + _gender = voice._gender; + _data = voice._data; + _age = voice._age; + _refCount = voice._refCount; + if (_data) + (*_refCount)++; + _description = voice._description; + } + return *this; +} + TextToSpeechManager::TextToSpeechManager() { _ttsState = new TTSState; _ttsState->_pitch = 0; @@ -39,9 +92,6 @@ TextToSpeechManager::~TextToSpeechManager() { TTSState *tmp = _ttsState; while (tmp != nullptr) { tmp = _ttsState->_next; - for (TTSVoice *i = _ttsState->_availaibleVoices.begin(); i < _ttsState->_availaibleVoices.end(); i++) { - free(i->_data); - } delete _ttsState; _ttsState = tmp; } diff --git a/common/text-to-speech.h b/common/text-to-speech.h index 63ea1a95c0..606f8d2d4b 100644 --- a/common/text-to-speech.h +++ b/common/text-to-speech.h @@ -52,16 +52,15 @@ class TTSVoice { }; public: - TTSVoice() - : _gender(UNKNOWN_GENDER) - , _age(UNKNOWN_AGE) - , _data(nullptr) - , _description("") {} - TTSVoice(Gender gender, Age age, void *data, String description) - : _gender(gender) - , _age(age) - , _data(data) - , _description(description) {} + TTSVoice(); + + TTSVoice(Gender gender, Age age, void *data, String description) ; + + TTSVoice(const TTSVoice& voice); + + ~TTSVoice(); + + TTSVoice& operator=(const TTSVoice& voice); /** * Returns the gender of the used voice. @@ -120,6 +119,7 @@ class TTSVoice { Age _age; void *_data; String _description; + int *_refCount; }; struct TTSState { @@ -261,6 +261,8 @@ public: */ virtual bool popState() { return true; } + virtual void freeVoiceData(void *data) {} + protected: TTSState *_ttsState; |