Tidy object management

This commit is contained in:
David Rowe 2014-10-22 11:27:06 -07:00
parent a45d4b2d7d
commit 69b1c4273f
2 changed files with 16 additions and 12 deletions

View file

@ -21,7 +21,6 @@ SpeechRecognizer::SpeechRecognizer() :
_enabled(false), _enabled(false),
_commands(), _commands(),
_comInitialized(false), _comInitialized(false),
_speechRecognizer(NULL),
_speechRecognizerContext(NULL), _speechRecognizerContext(NULL),
_speechRecognizerGrammar(NULL), _speechRecognizerGrammar(NULL),
_commandRecognizedEvent(NULL), _commandRecognizedEvent(NULL),
@ -32,17 +31,23 @@ SpeechRecognizer::SpeechRecognizer() :
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
_comInitialized = true; _comInitialized = true;
} }
_commandRecognizedNotifier = new QWinEventNotifier();
connect(_commandRecognizedNotifier, SIGNAL(activated(HANDLE)), SLOT(notifyCommandRecognized(HANDLE)));
} }
SpeechRecognizer::~SpeechRecognizer() { SpeechRecognizer::~SpeechRecognizer() {
if (_speechRecognizerGrammar) {
_speechRecognizerGrammar.Release();
}
if (_enabled) {
_speechRecognizerContext.Release();
}
if (_comInitialized) { if (_comInitialized) {
::CoUninitialize(); ::CoUninitialize();
} }
if (_speechRecognizerGrammar) {
_speechRecognizerGrammar.Release();
}
} }
void SpeechRecognizer::handleCommandRecognized(const char* command) { void SpeechRecognizer::handleCommandRecognized(const char* command) {
@ -71,14 +76,13 @@ void SpeechRecognizer::setEnabled(bool enabled) {
} }
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
_commandRecognizedEvent = _speechRecognizerContext->GetNotifyEventHandle(); _commandRecognizedEvent = _speechRecognizerContext->GetNotifyEventHandle();
if (_commandRecognizedEvent == NULL) { if (_commandRecognizedEvent) {
_commandRecognizedNotifier->setHandle(_commandRecognizedEvent);
_commandRecognizedNotifier->setEnabled(true);
} else {
hr = S_FALSE; hr = S_FALSE;
} }
} }
if (SUCCEEDED(hr)) {
_commandRecognizedNotifier = new QWinEventNotifier(_commandRecognizedEvent);
connect(_commandRecognizedNotifier, SIGNAL(activated(HANDLE)), SLOT(notifyCommandRecognized(HANDLE)));
}
// Set which events to be notified of. // Set which events to be notified of.
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
@ -96,6 +100,7 @@ void SpeechRecognizer::setEnabled(bool enabled) {
_enabled = SUCCEEDED(hr); _enabled = SUCCEEDED(hr);
} else { } else {
_commandRecognizedNotifier->setEnabled(false);
_speechRecognizerContext.Release(); _speechRecognizerContext.Release();
} }
@ -144,7 +149,7 @@ void SpeechRecognizer::reloadCommands() {
} }
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
hr = _speechRecognizerGrammar->Commit(0); hr = _speechRecognizerGrammar->Commit(NULL);
} }
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {

View file

@ -52,7 +52,6 @@ private:
void* _speechRecognizer; void* _speechRecognizer;
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
bool _comInitialized; bool _comInitialized;
CComPtr<ISpRecognizer> _speechRecognizer;
CComPtr<ISpRecoContext> _speechRecognizerContext; CComPtr<ISpRecoContext> _speechRecognizerContext;
CComPtr<ISpRecoGrammar> _speechRecognizerGrammar; CComPtr<ISpRecoGrammar> _speechRecognizerGrammar;
HANDLE _commandRecognizedEvent; HANDLE _commandRecognizedEvent;