2 * Licensed under the Apache License, Version 2.0 (the "License");
\r
3 * you may not use this file except in compliance with the License.
\r
4 * You may obtain a copy of the License at
\r
6 * http://www.apache.org/licenses/LICENSE-2.0
\r
8 * Unless required by applicable law or agreed to in writing, software
\r
9 * distributed under the License is distributed on an "AS IS" BASIS,
\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
11 * See the License for the specific language governing permissions and
\r
12 * limitations under the License.
\r
18 /// This interface should be implemented by any class whose instances are intended
\r
19 /// to be executed by a thread.
\r
21 public interface IThreadRunnable
\r
24 /// This method has to be implemented in order that starting of the thread causes the object's
\r
25 /// run method to be called in that separately executing thread.
\r
31 /// Contains conversion support elements such as classes, interfaces and static methods.
\r
33 public class SupportClass
\r
39 /// Performs an unsigned bitwise right shift with the specified number
\r
41 /// <param name="number">Number to operate on</param>
\r
42 /// <param name="bits">Ammount of bits to shift</param>
\r
43 /// <returns>The resulting number from the shift operation</returns>
\r
44 public static int URShift(int number, int bits)
\r
47 return number >> bits;
\r
49 return (number >> bits) + (2 << ~bits);
\r
53 /// Performs an unsigned bitwise right shift with the specified number
\r
55 /// <param name="number">Number to operate on</param>
\r
56 /// <param name="bits">Ammount of bits to shift</param>
\r
57 /// <returns>The resulting number from the shift operation</returns>
\r
58 public static int URShift(int number, long bits)
\r
60 return URShift(number, (int)bits);
\r
64 /// Performs an unsigned bitwise right shift with the specified number
\r
66 /// <param name="number">Number to operate on</param>
\r
67 /// <param name="bits">Ammount of bits to shift</param>
\r
68 /// <returns>The resulting number from the shift operation</returns>
\r
69 public static long URShift(long number, int bits)
\r
72 return number >> bits;
\r
74 return (number >> bits) + (2L << ~bits);
\r
78 /// Performs an unsigned bitwise right shift with the specified number
\r
80 /// <param name="number">Number to operate on</param>
\r
81 /// <param name="bits">Ammount of bits to shift</param>
\r
82 /// <returns>The resulting number from the shift operation</returns>
\r
83 public static long URShift(long number, long bits)
\r
85 return URShift(number, (int)bits);
\r
88 /*******************************/
\r
90 /// Copies an array of chars obtained from a String into a specified array of chars
\r
92 /// <param name="sourceString">The String to get the chars from</param>
\r
93 /// <param name="sourceStart">Position of the String to start getting the chars</param>
\r
94 /// <param name="sourceEnd">Position of the String to end getting the chars</param>
\r
95 /// <param name="destinationArray">Array to return the chars</param>
\r
96 /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>
\r
97 /// <returns>An array of chars</returns>
\r
98 public static void GetCharsFromString(System.String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart)
\r
101 int destinationCounter;
\r
102 sourceCounter = sourceStart;
\r
103 destinationCounter = destinationStart;
\r
104 while (sourceCounter < sourceEnd)
\r
106 destinationArray[destinationCounter] = (char) sourceString[sourceCounter];
\r
108 destinationCounter++;
\r
112 /*******************************/
\r
114 /// Converts an array of sbytes to an array of bytes
\r
116 /// <param name="sbyteArray">The array of sbytes to be converted</param>
\r
117 /// <returns>The new array of bytes</returns>
\r
118 public static byte[] ToByteArray(sbyte[] sbyteArray)
\r
120 byte[] byteArray = null;
\r
122 if (sbyteArray != null)
\r
124 byteArray = new byte[sbyteArray.Length];
\r
125 for(int index=0; index < sbyteArray.Length; index++)
\r
126 byteArray[index] = (byte) sbyteArray[index];
\r
132 /// Converts a string to an array of bytes
\r
134 /// <param name="sourceString">The string to be converted</param>
\r
135 /// <returns>The new array of bytes</returns>
\r
136 public static byte[] ToByteArray(System.String sourceString)
\r
138 return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
\r
142 /// Converts a array of object-type instances to a byte-type array.
\r
144 /// <param name="tempObjectArray">Array to convert.</param>
\r
145 /// <returns>An array of byte type elements.</returns>
\r
146 public static byte[] ToByteArray(System.Object[] tempObjectArray)
\r
148 byte[] byteArray = null;
\r
149 if (tempObjectArray != null)
\r
151 byteArray = new byte[tempObjectArray.Length];
\r
152 for (int index = 0; index < tempObjectArray.Length; index++)
\r
153 byteArray[index] = (byte)tempObjectArray[index];
\r
158 /*******************************/
\r
160 /// Sets the capacity for the specified ArrayList
\r
162 /// <param name="vector">The ArrayList which capacity will be set</param>
\r
163 /// <param name="newCapacity">The new capacity value</param>
\r
164 public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity)
\r
166 if (newCapacity > vector.Count)
\r
167 vector.AddRange(new Array[newCapacity-vector.Count]);
\r
168 else if (newCapacity < vector.Count)
\r
169 vector.RemoveRange(newCapacity, vector.Count - newCapacity);
\r
170 vector.Capacity = newCapacity;
\r
175 /*******************************/
\r
177 /// This method returns the literal value received
\r
179 /// <param name="literal">The literal to return</param>
\r
180 /// <returns>The received value</returns>
\r
181 public static long Identity(long literal)
\r
187 /// This method returns the literal value received
\r
189 /// <param name="literal">The literal to return</param>
\r
190 /// <returns>The received value</returns>
\r
191 public static ulong Identity(ulong literal)
\r
197 /// This method returns the literal value received
\r
199 /// <param name="literal">The literal to return</param>
\r
200 /// <returns>The received value</returns>
\r
201 public static float Identity(float literal)
\r
207 /// This method returns the literal value received
\r
209 /// <param name="literal">The literal to return</param>
\r
210 /// <returns>The received value</returns>
\r
211 public static double Identity(double literal)
\r
216 /*******************************/
\r
218 /// Support class used to handle threads
\r
220 public class ThreadClass : IThreadRunnable
\r
223 /// The instance of System.Threading.Thread
\r
225 private System.Threading.Thread threadField;
\r
228 /// Initializes a new instance of the ThreadClass class
\r
230 public ThreadClass()
\r
232 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
\r
236 /// Initializes a new instance of the Thread class.
\r
238 /// <param name="Name">The name of the thread</param>
\r
239 public ThreadClass(System.String Name)
\r
241 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
\r
246 /// Initializes a new instance of the Thread class.
\r
248 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
\r
249 public ThreadClass(System.Threading.ThreadStart Start)
\r
251 threadField = new System.Threading.Thread(Start);
\r
255 /// Initializes a new instance of the Thread class.
\r
257 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
\r
258 /// <param name="Name">The name of the thread</param>
\r
259 public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
\r
261 threadField = new System.Threading.Thread(Start);
\r
266 /// This method has no functionality unless the method is overridden
\r
268 public virtual void Run()
\r
273 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
\r
275 public virtual void Start()
\r
277 threadField.Start();
\r
281 ///// Interrupts a thread that is in the WaitSleepJoin thread state
\r
283 //public virtual void Interrupt()
\r
285 // threadField.Interrupt();
\r
289 /// Gets the current thread instance
\r
291 public System.Threading.Thread Instance
\r
295 return threadField;
\r
299 threadField = value;
\r
304 /// Gets or sets the name of the thread
\r
306 public System.String Name
\r
310 return threadField.Name;
\r
314 if (threadField.Name == null)
\r
315 threadField.Name = value;
\r
320 /// Gets or sets a value indicating the scheduling priority of a thread
\r
322 public System.Threading.ThreadPriority Priority
\r
326 return threadField.Priority;
\r
330 threadField.Priority = value;
\r
335 ///// Gets a value indicating the execution status of the current thread
\r
337 //public bool IsAlive
\r
341 // return threadField.IsAlive;
\r
346 /// Gets or sets a value indicating whether or not a thread is a background thread.
\r
348 public bool IsBackground
\r
352 return threadField.IsBackground;
\r
356 threadField.IsBackground = value;
\r
361 /// Blocks the calling thread until a thread terminates
\r
365 threadField.Join();
\r
369 /// Blocks the calling thread until a thread terminates or the specified time elapses
\r
371 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
\r
372 public void Join(int MiliSeconds)
\r
376 threadField.Join(MiliSeconds);
\r
381 ///// Blocks the calling thread until a thread terminates or the specified time elapses
\r
383 ///// <param name="MiliSeconds">Time of wait in milliseconds</param>
\r
384 ///// <param name="NanoSeconds">Time of wait in nanoseconds</param>
\r
385 //public void Join(long MiliSeconds, int NanoSeconds)
\r
389 // threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
\r
394 ///// Resumes a thread that has been suspended
\r
396 //public void Resume()
\r
398 // threadField.Resume();
\r
402 /// Raises a ThreadAbortException in the thread on which it is invoked,
\r
403 /// to begin the process of terminating the thread. Calling this method
\r
404 /// usually terminates the thread
\r
406 public void Abort()
\r
408 threadField.Abort();
\r
412 /// Raises a ThreadAbortException in the thread on which it is invoked,
\r
413 /// to begin the process of terminating the thread while also providing
\r
414 /// exception information about the thread termination.
\r
415 /// Calling this method usually terminates the thread.
\r
417 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
\r
418 public void Abort(System.Object stateInfo)
\r
422 threadField.Abort(stateInfo);
\r
427 ///// Suspends the thread, if the thread is already suspended it has no effect
\r
429 //public void Suspend()
\r
431 // threadField.Suspend();
\r
435 /// Obtain a String that represents the current Object
\r
437 /// <returns>A String that represents the current Object</returns>
\r
438 public override System.String ToString()
\r
440 return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
\r
444 /// Gets the currently running thread
\r
446 /// <returns>The currently running thread</returns>
\r
447 public static ThreadClass Current()
\r
449 ThreadClass CurrentThread = new ThreadClass();
\r
450 CurrentThread.Instance = System.Threading.Thread.CurrentThread;
\r
451 return CurrentThread;
\r