MsrThreadStatus.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "stdafx.h"
  2. //#include "OTSData.h"
  3. #include "MsrThreadStatus.h"
  4. namespace OTSMODEL {
  5. using namespace OTSDATA;
  6. // constructor
  7. CMsrThreadStatus::CMsrThreadStatus()
  8. {
  9. // initialization
  10. Init();
  11. }
  12. // copy constructor
  13. CMsrThreadStatus::CMsrThreadStatus(const CMsrThreadStatus& a_oSource)
  14. {
  15. // can't copy itself
  16. if (&a_oSource == this)
  17. {
  18. return;
  19. }
  20. // copy data over
  21. Duplicate(a_oSource);
  22. }
  23. // copy constructor
  24. CMsrThreadStatus::CMsrThreadStatus(CMsrThreadStatus* a_poSource)
  25. {
  26. // input check
  27. ASSERT(a_poSource);
  28. if (!a_poSource)
  29. {
  30. return;
  31. }
  32. // can't copy itself
  33. if (a_poSource == this)
  34. {
  35. return;
  36. }
  37. // copy data over
  38. Duplicate(*a_poSource);
  39. }
  40. // =operator
  41. CMsrThreadStatus& CMsrThreadStatus::operator=(const CMsrThreadStatus& a_oSource)
  42. {
  43. // cleanup
  44. Cleanup();
  45. // copy the class data over
  46. Duplicate(a_oSource);
  47. // return class
  48. return *this;
  49. }
  50. // destructor
  51. CMsrThreadStatus::~CMsrThreadStatus()
  52. {
  53. // cleanup
  54. Cleanup();
  55. }
  56. // ==operator
  57. BOOL CMsrThreadStatus::operator==(const CMsrThreadStatus& a_oSource)
  58. {
  59. int nSize = (int)m_listCpldSamples.size();
  60. if (nSize != (int)a_oSource.m_listCpldSamples.size())
  61. {
  62. return FALSE;
  63. }
  64. // return FALSE if any of the samples are different
  65. for (int i = 0; i < nSize; ++i)
  66. {
  67. if (!(*(m_listCpldSamples[i].get()) == *(a_oSource.m_listCpldSamples[i].get())))
  68. {
  69. return FALSE;
  70. }
  71. }
  72. return m_nStatus == a_oSource.m_nStatus &&
  73. m_timeStart == a_oSource.m_timeStart &&
  74. m_timeUsed == a_oSource.m_timeUsed &&
  75. m_timeStart == a_oSource.m_timeStart;
  76. }
  77. // CMsrThreadStatus member functions
  78. // public
  79. // serialization
  80. void CMsrThreadStatus::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  81. {
  82. xmls::xInt xnStatus;
  83. xmls::xOleDateTime xtimeStart;
  84. xmls::xOleDateTimeSpan xtimeUsed;
  85. xmls::xOleDateTime xtimeEnd;
  86. xmls::Collection<COTSSample> xSamples;
  87. xmls::Slo slo;
  88. slo.Register("CompletedFields", &xnStatus);
  89. slo.Register("timeStart", &xtimeStart);
  90. slo.Register("timeUsed", &xtimeUsed);
  91. slo.Register("timeEnd", &xtimeEnd);
  92. if (isStoring)
  93. {
  94. xnStatus = (int)m_nStatus;
  95. xtimeStart = m_timeStart;
  96. xtimeUsed = m_timeUsed;
  97. xtimeEnd = m_timeEnd;
  98. xSamples.Clear();
  99. for (auto pSample : m_listCpldSamples)
  100. {
  101. xSamples.addItem(pSample.get());
  102. }
  103. slo.Serialize(true, classDoc, rootNode);
  104. }
  105. else
  106. {
  107. slo.Serialize(false, classDoc, rootNode);
  108. m_nStatus = (OTS_MSR_THREAD_STATUS)xnStatus.value();
  109. m_timeStart = xtimeStart.value ();
  110. m_timeUsed = xtimeUsed.value ();
  111. m_timeEnd = xtimeEnd.value ();
  112. m_listCpldSamples.clear();
  113. for (unsigned int i=0;i<xSamples.size ();i++)
  114. {
  115. m_listCpldSamples.push_back (COTSSamplePtr(xSamples.getItem (i)));
  116. }
  117. }
  118. }
  119. BOOL CMsrThreadStatus::ComputeTime(OTS_THREAD_TIME_TYPE a_nType)
  120. {
  121. if (a_nType == OTS_THREAD_TIME_TYPE::START)
  122. {
  123. if (m_timeStart == COleDateTime())
  124. {
  125. m_timeStart = COleDateTime::GetCurrentTime();
  126. m_timeStartCur = m_timeStart;
  127. }
  128. else
  129. {
  130. m_timeStartCur = COleDateTime::GetCurrentTime();
  131. }
  132. m_timeEnd = COleDateTime::GetCurrentTime();
  133. }
  134. else if (a_nType == OTS_THREAD_TIME_TYPE::STOPPED)
  135. {
  136. // set current time as end time
  137. m_timeEnd = COleDateTime::GetCurrentTime();
  138. // compute used time
  139. COleDateTimeSpan timeUsed = COleDateTimeSpan();
  140. if (m_timeStartCur == m_timeStart)
  141. {
  142. // first compute time
  143. timeUsed = m_timeEnd - m_timeStart;
  144. }
  145. else
  146. {
  147. // not the first compute time
  148. timeUsed = m_timeUsed + m_timeEnd - m_timeStartCur;
  149. }
  150. m_timeUsed = timeUsed;
  151. }
  152. else
  153. {
  154. return FALSE;
  155. }
  156. return TRUE;
  157. }
  158. // protected
  159. // cleanup
  160. void CMsrThreadStatus::Cleanup()
  161. {
  162. m_listCpldSamples.clear();
  163. }
  164. void CMsrThreadStatus::SetCompletedSamples(COTSSamplesList& a_listCpldSamples)
  165. {
  166. m_listCpldSamples.clear();
  167. for (auto pSample : a_listCpldSamples)
  168. {
  169. m_listCpldSamples.push_back(pSample);
  170. }
  171. }
  172. // initialization
  173. void CMsrThreadStatus::Init()
  174. {
  175. m_nStatus = DEFAULT_MSR_THREAD_STATUS;
  176. m_timeStart = COleDateTime();
  177. m_timeUsed = COleDateTimeSpan();
  178. m_timeEnd = COleDateTime();
  179. m_timeStartCur = COleDateTime();
  180. m_listCpldSamples.clear();
  181. }
  182. // duplication
  183. void CMsrThreadStatus::Duplicate(const CMsrThreadStatus& a_oSource)
  184. {
  185. // initialization
  186. Init();
  187. // copy data over
  188. m_nStatus = a_oSource.m_nStatus;
  189. m_timeStart = a_oSource.m_timeStart;
  190. m_timeUsed = a_oSource.m_timeUsed;
  191. m_timeEnd = a_oSource.m_timeEnd;
  192. for (auto pSample : a_oSource.m_listCpldSamples)
  193. {
  194. COTSSamplePtr pSampleNew = COTSSamplePtr(new COTSSample(pSample.get()));
  195. m_listCpldSamples.push_back(pSampleNew);
  196. }
  197. }
  198. }