]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
09e453695481cb7488373dfdc5608a0a77e9dc93
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include <stdio.h>
21
22 #include <wx/string.h>
23 #include <wx/file.h>
24 #include <wx/app.h>
25
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
29
30 // what to test?
31
32 //#define TEST_ARRAYS
33 //#define TEST_LOG
34 #define TEST_THREADS
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // threads
42 // ----------------------------------------------------------------------------
43
44 #ifdef TEST_THREADS
45
46 #include <wx/thread.h>
47
48 static size_t gs_counter = (size_t)-1;
49 static wxCriticalSection gs_critsect;
50 static wxCondition gs_cond;
51
52 class MyJoinableThread : public wxThread
53 {
54 public:
55 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
56 { m_n = n; Create(); }
57
58 // thread execution starts here
59 virtual ExitCode Entry();
60
61 private:
62 size_t m_n;
63 };
64
65 wxThread::ExitCode MyJoinableThread::Entry()
66 {
67 unsigned long res = 1;
68 for ( size_t n = 1; n < m_n; n++ )
69 {
70 res *= n;
71
72 // it's a loooong calculation :-)
73 Sleep(100);
74 }
75
76 return (ExitCode)res;
77 }
78
79 class MyDetachedThread : public wxThread
80 {
81 public:
82 MyDetachedThread(char ch) { m_ch = ch; Create(); }
83
84 // thread execution starts here
85 virtual ExitCode Entry();
86
87 // and stops here
88 virtual void OnExit();
89
90 private:
91 char m_ch;
92 };
93
94 wxThread::ExitCode MyDetachedThread::Entry()
95 {
96 {
97 wxCriticalSectionLocker lock(gs_critsect);
98 if ( gs_counter == (size_t)-1 )
99 gs_counter = 1;
100 else
101 gs_counter++;
102 }
103
104 static const size_t nIter = 10;
105 for ( size_t n = 0; n < nIter; n++ )
106 {
107 if ( TestDestroy() )
108 break;
109
110 putchar(m_ch);
111 fflush(stdout);
112
113 wxThread::Sleep(100);
114 }
115
116 return 0;
117 }
118
119 void MyDetachedThread::OnExit()
120 {
121 wxCriticalSectionLocker lock(gs_critsect);
122 if ( !--gs_counter )
123 gs_cond.Signal();
124 }
125
126 #endif // TEST_THREADS
127
128 // ----------------------------------------------------------------------------
129 // arrays
130 // ----------------------------------------------------------------------------
131
132 #ifdef TEST_ARRAYS
133
134 void PrintArray(const char* name, const wxArrayString& array)
135 {
136 printf("Dump of the array '%s'\n", name);
137
138 size_t nCount = array.GetCount();
139 for ( size_t n = 0; n < nCount; n++ )
140 {
141 printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
142 }
143 }
144
145 #endif // TEST_ARRAYS
146
147 // ----------------------------------------------------------------------------
148 // entry point
149 // ----------------------------------------------------------------------------
150
151 int main(int argc, char **argv)
152 {
153 if ( !wxInitialize() )
154 {
155 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
156 }
157
158 #ifdef TEST_ARRAYS
159 wxArrayString a1;
160 a1.Add("tiger");
161 a1.Add("cat");
162 a1.Add("lion");
163 a1.Add("dog");
164 a1.Add("human");
165 a1.Add("ape");
166
167 puts("*** Initially:");
168
169 PrintArray("a1", a1);
170
171 wxArrayString a2(a1);
172 PrintArray("a2", a2);
173
174 wxSortedArrayString a3(a1);
175 PrintArray("a3", a3);
176
177 puts("*** After deleting a string from a1");
178 a1.Remove(2);
179
180 PrintArray("a1", a1);
181 PrintArray("a2", a2);
182 PrintArray("a3", a3);
183
184 puts("*** After reassigning a1 to a2 and a3");
185 a3 = a2 = a1;
186 PrintArray("a2", a2);
187 PrintArray("a3", a3);
188 #endif // TEST_ARRAYS
189
190 #ifdef TEST_LOG
191 wxString s;
192 for ( size_t n = 0; n < 8000; n++ )
193 {
194 s << (char)('A' + (n % 26));
195 }
196
197 wxString msg;
198 msg.Printf("A very very long message: '%s', the end!\n", s.c_str());
199
200 // this one shouldn't be truncated
201 printf(msg);
202
203 // but this one will because log functions use fixed size buffer
204 // (note that it doesn't need '\n' at the end neither - will be added
205 // by wxLog anyhow)
206 wxLogMessage("A very very long message 2: '%s', the end!", s.c_str());
207 #endif // TEST_LOG
208
209 #ifdef TEST_THREADS
210 puts("Testing detached threads...");
211
212 static const size_t nThreads = 3;
213 MyDetachedThread *threads[nThreads];
214 size_t n;
215 for ( n = 0; n < nThreads; n++ )
216 {
217 threads[n] = new MyDetachedThread('A' + n);
218 }
219
220 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
221 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
222
223 for ( n = 0; n < nThreads; n++ )
224 {
225 threads[n]->Run();
226 }
227
228 // wait until all threads terminate
229 wxMutex mutex;
230 mutex.Lock();
231 gs_cond.Wait(mutex);
232 mutex.Unlock();
233
234 puts("\n\nTesting a joinable thread used for a loooong calculation...");
235
236 // calc 10! in the background
237 MyJoinableThread thread(10);
238 thread.Run();
239
240 printf("\nThread terminated with exit code %lu.\n",
241 (unsigned long)thread.Wait());
242 #endif // TEST_THREADS
243
244 wxUninitialize();
245
246 return 0;
247 }