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