37c68ffc833650695ad94298ad226265c621a6b3
1 #ifndef __DOM_THREAD_H__\r
2 #define __DOM_THREAD_H__\r
3 /**\r
4 * Phoebe DOM Implementation.\r
5 *\r
6 * This is a C++ approximation of the W3C DOM model, which follows\r
7 * fairly closely the specifications in the various .idl files, copies of\r
8 * which are provided for reference. Most important is this one:\r
9 *\r
10 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html\r
11 *\r
12 * Authors:\r
13 * Bob Jamison\r
14 *\r
15 * Copyright (C) 2006 Bob Jamison\r
16 *\r
17 * This library is free software; you can redistribute it and/or\r
18 * modify it under the terms of the GNU Lesser General Public\r
19 * License as published by the Free Software Foundation; either\r
20 * version 2.1 of the License, or (at your option) any later version.\r
21 *\r
22 * This library is distributed in the hope that it will be useful,\r
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
25 * Lesser General Public License for more details.\r
26 *\r
27 * You should have received a copy of the GNU Lesser General Public\r
28 * License along with this library; if not, write to the Free Software\r
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
30 */\r
31 \r
32 /**\r
33 * Thread wrapper. This provides a platform-independent thread\r
34 * class for IO and testing.\r
35 *\r
36 */\r
37 \r
38 \r
39 namespace org\r
40 {\r
41 namespace w3c\r
42 {\r
43 namespace dom\r
44 {\r
45 namespace util\r
46 {\r
47 \r
48 \r
49 /**\r
50 * This is the interface for a delegate class which can\r
51 * be run by a Thread.\r
52 * Thread thread(runnable);\r
53 * thread.start();\r
54 */\r
55 class Runnable\r
56 {\r
57 public:\r
58 \r
59 Runnable()\r
60 {}\r
61 virtual ~Runnable()\r
62 {}\r
63 \r
64 /**\r
65 * The method of a delegate class which can\r
66 * be run by a Thread. Thread is completed when this\r
67 * method is done.\r
68 */\r
69 virtual void run() = 0;\r
70 \r
71 };\r
72 \r
73 \r
74 \r
75 /**\r
76 * A simple wrapper of native threads in a portable class.\r
77 * It can be used either to execute its own run() method, or\r
78 * delegate to a Runnable class's run() method.\r
79 */\r
80 class Thread\r
81 {\r
82 public:\r
83 \r
84 /**\r
85 * Create a thread which will execute its own run() method.\r
86 */\r
87 Thread()\r
88 { runnable = (Runnable *)0 ; started = false; }\r
89 \r
90 /**\r
91 * Create a thread which will run a Runnable class's run() method.\r
92 */\r
93 Thread(const Runnable &runner)\r
94 { runnable = (Runnable *)&runner; started = false; }\r
95 \r
96 /**\r
97 * This does not kill a spawned thread.\r
98 */\r
99 virtual ~Thread()\r
100 {}\r
101 \r
102 /**\r
103 * Static method to pause the current thread for a given\r
104 * number of milliseconds.\r
105 */\r
106 static void sleep(unsigned long millis);\r
107 \r
108 /**\r
109 * This method will be executed if the Thread was created with\r
110 * no delegated Runnable class. The thread is completed when\r
111 * the method is done.\r
112 */\r
113 virtual void run()\r
114 {}\r
115 \r
116 /**\r
117 * Starts the thread.\r
118 */\r
119 virtual void start();\r
120 \r
121 /**\r
122 * Calls either this class's run() method, or that of a Runnable.\r
123 * A user would normally not call this directly.\r
124 */\r
125 virtual void execute()\r
126 {\r
127 started = true;\r
128 if (runnable)\r
129 runnable->run();\r
130 else\r
131 run();\r
132 }\r
133 \r
134 private:\r
135 \r
136 Runnable *runnable;\r
137 \r
138 bool started;\r
139 \r
140 };\r
141 \r
142 \r
143 } //namespace util\r
144 } //namespace dom\r
145 } //namespace w3c\r
146 } //namespace org\r
147 \r
148 \r
149 \r
150 #endif /* __DOM_THREAD_H__ */\r
151 //#########################################################################\r
152 //# E N D O F F I L E\r
153 //#########################################################################\r
154 \r
155 \r