/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; /// /// This interface should be implemented by any class whose instances are intended /// to be executed by a thread. /// public interface IThreadRunnable { /// /// This method has to be implemented in order that starting of the thread causes the object's /// run method to be called in that separately executing thread. /// void Run(); } /// /// Contains conversion support elements such as classes, interfaces and static methods. /// public class SupportClass { /// /// Performs an unsigned bitwise right shift with the specified number /// /// Number to operate on /// Ammount of bits to shift /// The resulting number from the shift operation public static int URShift(int number, int bits) { if ( number >= 0) return number >> bits; else return (number >> bits) + (2 << ~bits); } /// /// Performs an unsigned bitwise right shift with the specified number /// /// Number to operate on /// Ammount of bits to shift /// The resulting number from the shift operation public static int URShift(int number, long bits) { return URShift(number, (int)bits); } /// /// Performs an unsigned bitwise right shift with the specified number /// /// Number to operate on /// Ammount of bits to shift /// The resulting number from the shift operation public static long URShift(long number, int bits) { if ( number >= 0) return number >> bits; else return (number >> bits) + (2L << ~bits); } /// /// Performs an unsigned bitwise right shift with the specified number /// /// Number to operate on /// Ammount of bits to shift /// The resulting number from the shift operation public static long URShift(long number, long bits) { return URShift(number, (int)bits); } /*******************************/ /// /// Copies an array of chars obtained from a String into a specified array of chars /// /// The String to get the chars from /// Position of the String to start getting the chars /// Position of the String to end getting the chars /// Array to return the chars /// Position of the destination array of chars to start storing the chars /// An array of chars public static void GetCharsFromString(System.String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart) { int sourceCounter; int destinationCounter; sourceCounter = sourceStart; destinationCounter = destinationStart; while (sourceCounter < sourceEnd) { destinationArray[destinationCounter] = (char) sourceString[sourceCounter]; sourceCounter++; destinationCounter++; } } /*******************************/ /// /// Converts an array of sbytes to an array of bytes /// /// The array of sbytes to be converted /// The new array of bytes public static byte[] ToByteArray(sbyte[] sbyteArray) { byte[] byteArray = null; if (sbyteArray != null) { byteArray = new byte[sbyteArray.Length]; for(int index=0; index < sbyteArray.Length; index++) byteArray[index] = (byte) sbyteArray[index]; } return byteArray; } /// /// Converts a string to an array of bytes /// /// The string to be converted /// The new array of bytes public static byte[] ToByteArray(System.String sourceString) { return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString); } /// /// Converts a array of object-type instances to a byte-type array. /// /// Array to convert. /// An array of byte type elements. public static byte[] ToByteArray(System.Object[] tempObjectArray) { byte[] byteArray = null; if (tempObjectArray != null) { byteArray = new byte[tempObjectArray.Length]; for (int index = 0; index < tempObjectArray.Length; index++) byteArray[index] = (byte)tempObjectArray[index]; } return byteArray; } /*******************************/ /// /// Sets the capacity for the specified ArrayList /// /// The ArrayList which capacity will be set /// The new capacity value public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity) { if (newCapacity > vector.Count) vector.AddRange(new Array[newCapacity-vector.Count]); else if (newCapacity < vector.Count) vector.RemoveRange(newCapacity, vector.Count - newCapacity); vector.Capacity = newCapacity; } /*******************************/ /// /// This method returns the literal value received /// /// The literal to return /// The received value public static long Identity(long literal) { return literal; } /// /// This method returns the literal value received /// /// The literal to return /// The received value public static ulong Identity(ulong literal) { return literal; } /// /// This method returns the literal value received /// /// The literal to return /// The received value public static float Identity(float literal) { return literal; } /// /// This method returns the literal value received /// /// The literal to return /// The received value public static double Identity(double literal) { return literal; } /*******************************/ /// /// Support class used to handle threads /// public class ThreadClass : IThreadRunnable { /// /// The instance of System.Threading.Thread /// private System.Threading.Thread threadField; /// /// Initializes a new instance of the ThreadClass class /// public ThreadClass() { threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run)); } /// /// Initializes a new instance of the Thread class. /// /// The name of the thread public ThreadClass(System.String Name) { threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run)); this.Name = Name; } /// /// Initializes a new instance of the Thread class. /// /// A ThreadStart delegate that references the methods to be invoked when this thread begins executing public ThreadClass(System.Threading.ThreadStart Start) { threadField = new System.Threading.Thread(Start); } /// /// Initializes a new instance of the Thread class. /// /// A ThreadStart delegate that references the methods to be invoked when this thread begins executing /// The name of the thread public ThreadClass(System.Threading.ThreadStart Start, System.String Name) { threadField = new System.Threading.Thread(Start); this.Name = Name; } /// /// This method has no functionality unless the method is overridden /// public virtual void Run() { } /// /// Causes the operating system to change the state of the current thread instance to ThreadState.Running /// public virtual void Start() { threadField.Start(); } ///// ///// Interrupts a thread that is in the WaitSleepJoin thread state ///// //public virtual void Interrupt() //{ // threadField.Interrupt(); //} /// /// Gets the current thread instance /// public System.Threading.Thread Instance { get { return threadField; } set { threadField = value; } } /// /// Gets or sets the name of the thread /// public System.String Name { get { return threadField.Name; } set { if (threadField.Name == null) threadField.Name = value; } } /// /// Gets or sets a value indicating the scheduling priority of a thread /// public System.Threading.ThreadPriority Priority { get { return threadField.Priority; } set { threadField.Priority = value; } } ///// ///// Gets a value indicating the execution status of the current thread ///// //public bool IsAlive //{ // get // { // return threadField.IsAlive; // } //} /// /// Gets or sets a value indicating whether or not a thread is a background thread. /// public bool IsBackground { get { return threadField.IsBackground; } set { threadField.IsBackground = value; } } /// /// Blocks the calling thread until a thread terminates /// public void Join() { threadField.Join(); } /// /// Blocks the calling thread until a thread terminates or the specified time elapses /// /// Time of wait in milliseconds public void Join(int MiliSeconds) { lock(this) { threadField.Join(MiliSeconds); } } ///// ///// Blocks the calling thread until a thread terminates or the specified time elapses ///// ///// Time of wait in milliseconds ///// Time of wait in nanoseconds //public void Join(long MiliSeconds, int NanoSeconds) //{ // lock(this) // { // threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100)); // } //} ///// ///// Resumes a thread that has been suspended ///// //public void Resume() //{ // threadField.Resume(); //} /// /// Raises a ThreadAbortException in the thread on which it is invoked, /// to begin the process of terminating the thread. Calling this method /// usually terminates the thread /// public void Abort() { threadField.Abort(); } /// /// Raises a ThreadAbortException in the thread on which it is invoked, /// to begin the process of terminating the thread while also providing /// exception information about the thread termination. /// Calling this method usually terminates the thread. /// /// An object that contains application-specific information, such as state, which can be used by the thread being aborted public void Abort(System.Object stateInfo) { lock(this) { threadField.Abort(stateInfo); } } ///// ///// Suspends the thread, if the thread is already suspended it has no effect ///// //public void Suspend() //{ // threadField.Suspend(); //} /// /// Obtain a String that represents the current Object /// /// A String that represents the current Object public override System.String ToString() { return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]"; } /// /// Gets the currently running thread /// /// The currently running thread public static ThreadClass Current() { ThreadClass CurrentThread = new ThreadClass(); CurrentThread.Instance = System.Threading.Thread.CurrentThread; return CurrentThread; } } }