1 /**********************************************************************
2 
3    Audacity - A Digital Audio Editor
4    Copyright 1999-2009 Audacity Team
5    License: wxWidgets
6 
7    Dan Horgan
8 
9 ******************************************************************//**
10 
11 \file ResponseQueue.h
12 \brief Contains declarations for Response and ResponseQueue classes
13 
14 *//****************************************************************//**
15 
16 \class Response
17 \brief Stores a command response string (and other response data if it becomes
18 necessary)
19 
20 The string is internally stored as a std::string rather than wxString
21 because of thread-safety concerns.
22 
23 *//****************************************************************//**
24 
25 \class ResponseQueue
26 \brief Allow messages to be sent from the main thread to the script thread
27 
28 Based roughly on wxMessageQueue<T> (which hasn't reached the stable wxwidgets
29 yet). Wraps a std::queue<Response> inside a wxMutex with a wxCondition to
30 force the script thread to wait until a response is available.
31 
32 *//*******************************************************************/
33 
34 #ifndef __RESPONSEQUEUE__
35 #define __RESPONSEQUEUE__
36 
37 #include <queue>
38 #include <string>
39 #include <wx/thread.h> // member variable
40 #include <wx/string.h> // member variable
41 
42 class wxMutex;
43 class wxCondition;
44 class wxMutexLocker;
45 
46 class Response {
47    private:
48       std::string mMessage;
49    public:
Response(const wxString & response)50       Response(const wxString &response)
51          : mMessage(response.utf8_str())
52       { }
53 
GetMessage()54       wxString GetMessage()
55       {
56          return wxString(mMessage.c_str(), wxConvUTF8);
57       }
58 };
59 
60 class ResponseQueue {
61    private:
62       std::queue<Response> mResponses;
63       wxMutex mMutex;
64       wxCondition mCondition;
65 
66    public:
67       ResponseQueue();
68       ~ResponseQueue();
69 
70       void AddResponse(Response response);
71       Response WaitAndGetResponse();
72 };
73 
74 #endif /* End of include guard: __RESPONSEQUEUE__ */
75