DataItem_t.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace IACommService4CSharp
  7. {
  8. public class DataItem_t : IDisposable
  9. {
  10. private IDataItem4CSharp m_item;
  11. private bool m_disposed = false;
  12. private bool m_isReference = false;
  13. public DataItem_t()
  14. {
  15. m_item = DataItemFactory4CSharp.Create();
  16. if (null == m_item)
  17. throw new IACommServiceException(ErrorCode.InternalError);
  18. }
  19. public DataItem_t(DataValue_t value, uint status = ErrorCode.Good, long time = 0)
  20. {
  21. m_item = DataItemFactory4CSharp.Create();
  22. if (null == m_item)
  23. throw new IACommServiceException(ErrorCode.InternalError);
  24. Value = value;
  25. Status = status;
  26. Time = time;
  27. }
  28. public DataItem_t(object value, uint status = ErrorCode.Good, long time = 0)
  29. {
  30. m_item = DataItemFactory4CSharp.Create();
  31. if (null == m_item)
  32. throw new IACommServiceException(ErrorCode.InternalError);
  33. var temp = new DataValue_t(value);
  34. Value = temp;
  35. Status = status;
  36. Time = time;
  37. temp.Dispose();
  38. }
  39. //Internal use
  40. public DataItem_t(IDataItem4CSharp item)
  41. {
  42. m_item = item;
  43. if (null == m_item)
  44. throw new IACommServiceException(ErrorCode.InternalError);
  45. m_isReference = true;
  46. }
  47. ~DataItem_t()
  48. {
  49. Dispose(false);
  50. }
  51. public uint Status
  52. {
  53. get
  54. {
  55. return m_item.Status();
  56. }
  57. set
  58. {
  59. m_item.Status(value);
  60. }
  61. }
  62. public long Time
  63. {
  64. get
  65. {
  66. return m_item.Time();
  67. }
  68. set
  69. {
  70. m_item.Time(value);
  71. }
  72. }
  73. public DataValue_t Value
  74. {
  75. get
  76. {
  77. return new DataValue_t(m_item.Value());
  78. }
  79. set
  80. {
  81. if (value is null)
  82. throw new IACommServiceException(ErrorCode.InvalidArgument);
  83. uint status = m_item.Value(value.Internal);
  84. if (status != ErrorCode.Good)
  85. throw new IACommServiceException(status);
  86. }
  87. }
  88. public void Dispose()
  89. {
  90. Dispose(true);
  91. GC.SuppressFinalize(this);
  92. }
  93. protected virtual void Dispose(bool disposeManaged)
  94. {
  95. if (!m_disposed)
  96. {
  97. if (disposeManaged)
  98. {
  99. //do nothing
  100. }
  101. if (!m_isReference)
  102. DataItemFactory4CSharp.Destroy(m_item);
  103. m_item = null;
  104. m_disposed = true;
  105. }
  106. }
  107. public IDataItem4CSharp Internal
  108. {
  109. get
  110. {
  111. return m_item;
  112. }
  113. }
  114. }
  115. }