| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IACommService4CSharp
- {
- public static class DType
- {
- public const UInt32 Undefined = 0u; /* undefined type */
- public const UInt32 Boolean = 1u; /* boolean */
- public const UInt32 SByte = 2u; /* signed byte */
- public const UInt32 Byte = 3u; /* unsigned byte */
- public const UInt32 Int16 = 4u; /* signed 16 bit integer */
- public const UInt32 UInt16 = 5u; /* unsigned 16 bit integer */
- public const UInt32 Int32 = 6u; /* signed 32 bit integer */
- public const UInt32 UInt32 = 7u; /* unsigned 32 bit integer */
- public const UInt32 Int64 = 8u; /* signed 64 bit integer */
- public const UInt32 UInt64 = 9u; /* unsigned 64 bit integer */
- public const UInt32 Float = 10u; /* 32 bit single precision floating point */
- public const UInt32 Double = 11u; /* 64 bit double precision floating point */
- public const UInt32 String = 12u; /* UTF-8 string */
- public const UInt32 Complex = 100u; /* complex type, such as Structure */
- public const UInt32 Custom = 200u; /* custom type */
- };
- public class DataValue_t : IDisposable
- {
- private static Dictionary<uint, Type> m_dataTypeDic = new Dictionary<uint, Type>()
- {
- { DType.Boolean, typeof(bool) },
- { DType.SByte, typeof(sbyte) },
- { DType.Byte, typeof(byte) },
- { DType.Int16, typeof(short) },
- { DType.UInt16, typeof(ushort) },
- { DType.Int32, typeof(int) },
- { DType.UInt32, typeof(uint) },
- { DType.Int64, typeof(long) },
- { DType.UInt64, typeof(ulong) },
- { DType.Float, typeof(float) },
- { DType.Double, typeof(double) },
- { DType.String, typeof(string) },
- { DType.Custom, typeof(sbyte[]) },
- { DType.Undefined, null }
- };
- private IDataValue4CSharp m_value;
- private bool m_disposed = false;
- private bool m_isReference = false;
- public DataValue_t(uint dataType = DType.Undefined, uint[] dimensions = null)
- {
- m_value = DataValueFactory4CSharp.Create(dataType, dimensions);
- if (m_value is null)
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- }
- public DataValue_t(object value)
- {
- if (value is null)
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- if (value is DataValue_t)
- {
- m_value = DataValueFactory4CSharp.Create(DType.Undefined, null);
- if (m_value is null)
- throw new IACommServiceException(ErrorCode.InternalError);
- CopyFrom((DataValue_t)value);
- return;
- }
- Type valueType = value.GetType();
- uint[] dimensions = null;
- if (value is Array)
- {
- var arrayValue = value as Array;
- dimensions = new uint[arrayValue.Rank];
- for (int index = 0; index < arrayValue.Rank; index++)
- {
- dimensions[index] = (uint)arrayValue.GetLength(index);
- }
- valueType = valueType.GetElementType();
- }
- uint dataType = DType.Undefined;
- if (m_dataTypeDic.ContainsValue(valueType))
- {
- dataType = m_dataTypeDic.First(q => q.Value == valueType).Key;
- }
- else if (valueType.IsValueType && !valueType.IsPrimitive && !valueType.IsEnum)
- {
- dataType = DType.Complex;
- }
- else if (valueType.IsEnum)
- {
- dataType = DType.Int16;
- }
- else
- {
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- }
- m_value = DataValueFactory4CSharp.Create(dataType, dimensions);
- Value = value;
- }
- //Internal use
- public DataValue_t(IDataValue4CSharp value)
- {
- m_value = value;
- if (null == m_value)
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- m_isReference = true;
- }
- ~DataValue_t()
- {
- Dispose(false);
- }
- public uint CopyFrom(DataValue_t value)
- {
- if(value is null)
- return ErrorCode.InvalidArgument;
- return m_value.CopyFrom(value.m_value);
- }
- public uint CopyTo(ref DataValue_t value)
- {
- if (value is null)
- value = new DataValue_t();
- return m_value.CopyTo(value.m_value);
- }
- public uint GetArrayValue(ref uint[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref long[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref ulong[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref bool[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref sbyte[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref byte[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref short[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref ushort[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref int[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref float[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref string[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetArrayValue(ref double[] value)
- {
- return m_value.GetArrayValue(ref value);
- }
- public uint GetElement(uint[] indexes, ref DataValue_t value)
- {
- if (value is null)
- value = new DataValue_t();
- return m_value.GetElement(indexes, value.m_value);
- }
- public uint GetElement(uint index, ref DataValue_t value)
- {
- if (value is null)
- value = new DataValue_t();
- return m_value.GetElement(index, value.m_value);
- }
- public uint GetValue(ref bool value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref sbyte value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref byte value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref short value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref uint value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref int value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref long value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref ulong value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref float value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref double value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref string value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(ref sbyte[] value)
- {
- return m_value.GetValue(ref value);
- }
- public uint GetValue(string field, ref DataValue_t value)
- {
- if (value is null)
- value = new DataValue_t();
- return m_value.GetValue(field, value.m_value);
- }
- public uint GetValue(ref ushort value)
- {
- return m_value.GetValue(ref value);
- }
- public uint Reset(uint dataType = DType.Undefined, uint[] dimensions = null)
- {
- return m_value.Reset(dataType, dimensions);
- }
- public uint SetArrayValue(byte[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(bool[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(sbyte[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(short[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(float[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(int[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(uint[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(long[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(ulong[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(double[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(string[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetArrayValue(ushort[] value)
- {
- return m_value.SetArrayValue(value);
- }
- public uint SetElement(uint index, DataValue_t value)
- {
- if (value is null)
- return ErrorCode.InvalidArgument;
- return m_value.SetElement(index, value.m_value);
- }
- public uint SetElement(uint[] indexes, DataValue_t value)
- {
- if (value is null)
- return ErrorCode.InvalidArgument;
- return m_value.SetElement(indexes, value.m_value);
- }
- public uint SetValue(string field, DataValue_t value)
- {
- if (value is null)
- return ErrorCode.InvalidArgument;
- return m_value.SetValue(field, value.m_value);
- }
- public uint SetValue(sbyte[] value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(ulong value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(long value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(uint value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(short value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(ushort value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(byte value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(sbyte value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(bool value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(string value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(double value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(int value)
- {
- return m_value.SetValue(value);
- }
- public uint SetValue(float value)
- {
- return m_value.SetValue(value);
- }
- public object Get(Type valueType = null)
- {
- if (valueType is null && DataType == DType.Complex)
- {
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- }
- if (valueType != null)
- {
- if (valueType == typeof(DataValue_t) || valueType.IsSubclassOf(typeof(DataValue_t)))
- {
- object value = null;
- if (valueType.Name == typeof(ArrayBase_t<>).Name)
- {
- if (!IsArray)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- value = Activator.CreateInstance(valueType, new object[] { new uint[] { 0 } });
- }
- else if (valueType.IsSubclassOf(typeof(ComplexBase_t)))
- {
- if (DataType != DType.Complex)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- value = Activator.CreateInstance(valueType);
- }
- else
- {
- value = Activator.CreateInstance(valueType, new object[] { DType.Undefined, null });
- }
- ((DataValue_t)value).CopyFrom(this);
- return value;
- }
- if (IsArray)
- {
- if (!valueType.IsArray || valueType.GetArrayRank() != Dimensions.Length)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- valueType = valueType.GetElementType();
- }
- if (valueType.IsEnum)
- {
- if (DataType != DType.Int16)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- }
- else if (valueType.IsValueType && !valueType.IsPrimitive)
- {
- if (DataType != DType.Complex)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- }
- else if (DataType == DType.Complex || m_dataTypeDic[DataType] != valueType)
- {
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- }
- }
- if (IsArray)
- {
- int[] tempDimensions = Array.ConvertAll<uint, int>(Dimensions, z => (int)z);
- Array arrayValue = null;
- switch (DataType)
- {
- case DType.Boolean:
- {
- bool[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.SByte:
- {
- sbyte[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Byte:
- {
- byte[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Int16:
- {
- short[] tempArray = null;
- GetArrayValue(ref tempArray);
- if (valueType != null && valueType.IsEnum)
- {
- arrayValue = Array.CreateInstance(valueType, tempDimensions);
- for (int index = 0; index < tempArray.Length; index++)
- {
- int[] indexes = null;
- ConvertIndex(index, tempDimensions, ref indexes);
- arrayValue.SetValue(Enum.ToObject(valueType, tempArray[index]), indexes);
- }
- }
- else
- {
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- }
- break;
- }
- case DType.UInt16:
- {
- ushort[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Int32:
- {
- int[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.UInt32:
- {
- uint[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Int64:
- {
- long[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.UInt64:
- {
- ulong[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Float:
- {
- float[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Double:
- {
- double[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.String:
- {
- string[] tempArray = null;
- GetArrayValue(ref tempArray);
- Convert2MultiDimensionsArray(tempArray, tempDimensions, ref arrayValue);
- break;
- }
- case DType.Complex:
- {
- arrayValue = Array.CreateInstance(valueType, tempDimensions);
- DataValue_t elementValue = new DataValue_t();
- for (uint index = 0; index < ArraySize; index++)
- {
- if (GetElement(index, ref elementValue) != ErrorCode.Good)
- {
- elementValue.Dispose();
- throw new IACommServiceException(ErrorCode.InternalError);
- }
- var element = Convert2Struct(elementValue, valueType);
- if (element is null)
- {
- elementValue.Dispose();
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- }
- int[] indexes = null;
- ConvertIndex((int)index, tempDimensions, ref indexes);
- arrayValue.SetValue(element, indexes);
- }
- elementValue.Dispose();
- break;
- }
- default:
- {
- return null;
- }
- }
- return arrayValue;
- }
- else
- {
- switch (DataType)
- {
- case DType.Boolean:
- {
- bool value = false;
- GetValue(ref value);
- return value;
- }
- case DType.SByte:
- {
- sbyte value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Byte:
- {
- byte value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Int16:
- {
- short value = 0;
- GetValue(ref value);
- if (valueType != null && valueType.IsEnum)
- {
- return Enum.ToObject(valueType, value);
- }
- else
- {
- return value;
- }
- }
- case DType.UInt16:
- {
- ushort value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Int32:
- {
- int value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.UInt32:
- {
- uint value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Int64:
- {
- long value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.UInt64:
- {
- ulong value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Float:
- {
- float value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.Double:
- {
- double value = 0;
- GetValue(ref value);
- return value;
- }
- case DType.String:
- {
- string value = null;
- GetValue(ref value);
- return value;
- }
- case DType.Custom:
- {
- sbyte[] value = null;
- GetValue(ref value);
- return value;
- }
- case DType.Complex:
- {
- var value = Convert2Struct(this, valueType);
- if (value is null)
- throw new IACommServiceException(ErrorCode.DataTypeMismatch);
- return value;
- }
- default:
- {
- return null;
- }
- }
- }
- }
- public uint Set(object value)
- {
- if (value is null)
- return ErrorCode.InvalidArgument;
- if (value is DataValue_t)
- {
- if (DataType != ((DataValue_t)value).DataType ||
- Dimensions.ToString() != ((DataValue_t)value).Dimensions.ToString())
- return ErrorCode.DataTypeMismatch;
- CopyFrom((DataValue_t)value);
- return ErrorCode.Good;
- }
- Type valueType = value.GetType();
- if (IsArray)
- {
- if (!valueType.IsArray)
- return ErrorCode.DataTypeMismatch;
- valueType = valueType.GetElementType();
- }
- if (valueType.IsEnum)
- {
- if (DataType != DType.Int16)
- return ErrorCode.DataTypeMismatch;
- }
- else if (valueType.IsValueType && !valueType.IsPrimitive)
- {
- if (DataType != DType.Complex)
- return ErrorCode.DataTypeMismatch;
- }
- else if (DataType == DType.Complex || m_dataTypeDic[DataType] != valueType)
- {
- return ErrorCode.DataTypeMismatch;
- }
- if (IsArray)
- {
- var tempValue = value as Array;
- int[] tempDimensions = Array.ConvertAll<uint, int>(Dimensions, z => (int)z);
- if (tempValue.Rank != tempDimensions.Length)
- return ErrorCode.DataTypeMismatch;
- for (int index = 0; index < tempValue.Rank; index++)
- {
- if (tempValue.GetLength(index) != tempDimensions[index])
- return ErrorCode.DataTypeMismatch;
- }
- if (valueType.IsEnum)
- {
- short[] arrayValue = new short[tempValue.Length];
- for (int index = 0; index < arrayValue.Length; index++)
- {
- int[] indexes = null;
- ConvertIndex(index, tempDimensions, ref indexes);
- arrayValue[index] = Convert.ToInt16(tempValue.GetValue(indexes));
- }
- return SetArrayValue(arrayValue);
- }
- else if (DataType == DType.Complex)
- {
- for (uint index = 0; index < ArraySize; index++)
- {
- int[] indexes = null;
- ConvertIndex((int)index, tempDimensions, ref indexes);
- DataValue_t element = new DataValue_t(DType.Complex);
- var structElement = tempValue.GetValue(indexes);
- var status = Convert2DataValue(structElement, element);
- if (ErrorCode.Good == status)
- status = SetElement(index, element);
- element.Dispose();
- if (status != ErrorCode.Good)
- return status;
- }
- return ErrorCode.Good;
- }
- else
- {
- switch (DataType)
- {
- case DType.Boolean:
- {
- bool[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.SByte:
- {
- sbyte[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Byte:
- {
- byte[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Int16:
- {
- short[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.UInt16:
- {
- ushort[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Int32:
- {
- int[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.UInt32:
- {
- uint[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Int64:
- {
- long[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.UInt64:
- {
- ulong[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Float:
- {
- float[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.Double:
- {
- double[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- case DType.String:
- {
- string[] arrayValue = null;
- Convert2SingleDimensionArray(value as Array, tempDimensions, ref arrayValue);
- return SetArrayValue(arrayValue);
- }
- default:
- {
- return ErrorCode.DataTypeMismatch;
- }
- }
- }
- }
- else
- {
- if (valueType.IsEnum)
- {
- return SetValue(Convert.ToInt16(value));
- }
- else if (DataType == DType.Complex)
- {
- Reset(DType.Complex);
- return Convert2DataValue(value, this);
- }
- else
- {
- switch (DataType)
- {
- case DType.Boolean:
- return SetValue((bool)value);
- case DType.SByte:
- return SetValue((sbyte)value);
- case DType.Byte:
- return SetValue((byte)value);
- case DType.Int16:
- return SetValue((short)value);
- case DType.UInt16:
- return SetValue((ushort)value);
- case DType.Int32:
- return SetValue((int)value);
- case DType.UInt32:
- return SetValue((uint)value);
- case DType.Int64:
- return SetValue((long)value);
- case DType.UInt64:
- return SetValue((ulong)value);
- case DType.Float:
- return SetValue((float)value);
- case DType.Double:
- return SetValue((double)value);
- case DType.String:
- return SetValue((string)value);
- case DType.Custom:
- return SetValue((sbyte[])value);
- default:
- return ErrorCode.DataTypeMismatch;
- }
- }
- }
- }
- public uint DataType
- {
- get
- {
- return m_value.DataType();
- }
- }
- public bool IsArray
- {
- get
- {
- return m_value.IsArray();
- }
- }
- public uint ArraySize
- {
- get
- {
- return m_value.ArraySize();
- }
- }
- public uint[] Dimensions
- {
- get
- {
- uint[] dimensions = null;
- m_value.GetDimensions(ref dimensions);
- return dimensions;
- }
- }
- public object Value
- {
- get
- {
- if (DataType == DType.Complex)
- throw new IACommServiceException(ErrorCode.NotSupported);
- return Get();
- }
- set
- {
- Set(value);
- }
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposeManaged)
- {
- if (!m_disposed)
- {
- if (disposeManaged)
- {
- //do nothing
- }
- if (!m_isReference)
- DataValueFactory4CSharp.Destroy(m_value);
- m_value = null;
- m_disposed = true;
- }
- }
- //Internal use
- public IDataValue4CSharp Internal
- {
- get
- {
- return m_value;
- }
- }
- private uint Convert2SingleDimensionArray<T>(Array sourceArray, int[] dimensions, ref T[] targetArray)
- {
- if (null == sourceArray)
- return ErrorCode.InvalidArgument;
- targetArray = new T[sourceArray.Length];
- for (int index = 0; index < targetArray.Length; index++)
- {
- int[] indexes = null;
- ConvertIndex(index, dimensions, ref indexes);
- targetArray[index] = (T)sourceArray.GetValue(indexes);
- }
- return ErrorCode.Good;
- }
- private uint Convert2MultiDimensionsArray<T>(T[] sourceArray, int[] dimensions, ref Array targetArray)
- {
- if (null == sourceArray)
- return ErrorCode.InvalidArgument;
- targetArray = Array.CreateInstance(typeof(T), dimensions);
- for (int index = 0; index < sourceArray.Length; index++)
- {
- int[] indexes = null;
- ConvertIndex(index, dimensions, ref indexes);
- targetArray.SetValue(sourceArray[index], indexes);
- }
- return ErrorCode.Good;
- }
- private uint ConvertIndex(int index, int[] dimensions, ref int[] indexes)
- {
- if (null == dimensions)
- return ErrorCode.InvalidArgument;
- indexes = new int[dimensions.Length];
- for (int n = 0; n < indexes.Length; n++)
- {
- int curLength = (int)dimensions[n];
- int lengths = 1;
- for (int m = n + 1; m < indexes.Length; m++)
- {
- lengths *= (int)dimensions[m];
- }
- indexes[n] = (index / lengths) % curLength;
- }
- return ErrorCode.Good;
- }
- private object Convert2Struct(DataValue_t value, Type structType)
- {
- object retValue = null;
- if (structType.IsValueType && !structType.IsPrimitive && !structType.IsEnum)
- {
- retValue = Activator.CreateInstance(structType);
- DataValue_t fieldValue = new DataValue_t();
- var fieldInfos = structType.GetFields();
- foreach (var fieldInfo in fieldInfos)
- {
- var status = value.GetValue(fieldInfo.Name, ref fieldValue);
- if (status != ErrorCode.Good)
- {
- fieldValue.Dispose();
- throw new IACommServiceException(ErrorCode.InvalidArgument);
- }
- object field = null;
- try
- {
- field = fieldValue.Get(fieldInfo.FieldType);
- }
- catch
- {
- field = null;
- }
- fieldInfo.SetValue(retValue, field);
- }
- fieldValue.Dispose();
- }
- return retValue;
- }
- private uint Convert2DataValue(object structValue, DataValue_t value)
- {
- var structType = structValue.GetType();
- if (structType.IsValueType && !structType.IsPrimitive && !structType.IsEnum)
- {
- var fieldInfos = structType.GetFields();
- foreach (var fieldInfo in fieldInfos)
- {
- object field = fieldInfo.GetValue(structValue);
- DataValue_t fieldValue = new DataValue_t(field);
- var status = value.SetValue(fieldInfo.Name, fieldValue);
- fieldValue.Dispose();
- if (status != ErrorCode.Good)
- return ErrorCode.InvalidArgument;
- }
- return ErrorCode.Good;
- }
- else
- {
- return ErrorCode.DataTypeMismatch;
- }
- }
- }
- }
|