CResultManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "StdAfx.h"
  2. #include "CResultManager.h"
  3. void CIdentifyTask::SetFeeder(IFeeder * feeder)
  4. {
  5. this->feeder = feeder;
  6. }
  7. IFeeder * CIdentifyTask::GetFeeder()
  8. {
  9. return feeder;
  10. }
  11. UnhandledPageInfo * CIdentifyTask::getUnhandledPageManager()
  12. {
  13. return &unhandleds;
  14. }
  15. CResultManager * CIdentifyTask::getResultBufferManager()
  16. {
  17. return &result;
  18. }
  19. CIdentifyTask::~CIdentifyTask()
  20. {
  21. }
  22. CIdentifyTask::CIdentifyTask()
  23. {
  24. feeder = NULL;
  25. unhandleds.unhandled_page_count = 0;
  26. unhandleds.first_unhandled_page_index = 0;
  27. unhandleds.m_stop = false;
  28. unhandleds.task = this;
  29. unhandleds.result = &result;
  30. unhandleds.bOnlineCard = false;
  31. result.reset();
  32. result.open();
  33. }
  34. CIdentifyTaskManager::~CIdentifyTaskManager()
  35. {
  36. for (int i = 0; i < tasks0.size(); i++)
  37. {
  38. delete tasks0[i];
  39. }
  40. }
  41. void CIdentifyTaskManager::doneTask(CIdentifyTask* task)
  42. {
  43. if (handle_task_index >= 0)handle_task_index--;
  44. if (task == main_task){ main_task = NULL; }
  45. for (int i = 0; i < tasks.size(); i++)
  46. {
  47. if (task == tasks[i]){ tasks.erase(tasks.begin() + i); }
  48. }
  49. for (int i = 0; i < tasks0.size(); i++)
  50. {
  51. if (task == tasks0[i]){ tasks0.erase(tasks0.begin() + i); }
  52. }
  53. delete task;
  54. }
  55. CIdentifyTask* CIdentifyTaskManager::getCurrentHandleTask()
  56. {
  57. if (handle_task_index >= 0)return tasks[handle_task_index];
  58. return main_task;
  59. }
  60. CIdentifyTask* CIdentifyTaskManager::getNextIdentifyTask()
  61. {
  62. if (idenfity_task_index >= 0)idenfity_task_index--;
  63. return getCurrentIdentifyTask();
  64. }
  65. CIdentifyTask* CIdentifyTaskManager::getCurrentIdentifyTask()
  66. {
  67. if (idenfity_task_index >= 0)return tasks[idenfity_task_index];
  68. return main_task;
  69. }
  70. CIdentifyTask* CIdentifyTaskManager::MainTask()
  71. {
  72. return main_task;
  73. }
  74. void CIdentifyTaskManager::setMainTask(CIdentifyTask* task)
  75. {
  76. main_task = task;
  77. }
  78. void CIdentifyTaskManager::ExcuTask(CIdentifyTask* task)
  79. {
  80. tasks.push_back(task);
  81. idenfity_task_index++;
  82. handle_task_index++;
  83. }
  84. CIdentifyTask* CIdentifyTaskManager::createTask()
  85. {
  86. CIdentifyTask* task = new CIdentifyTask();
  87. tasks0.push_back(task);
  88. return task;
  89. }
  90. CIdentifyTaskManager::CIdentifyTaskManager()
  91. {
  92. main_task = NULL;
  93. idenfity_task_index = -1;
  94. handle_task_index = -1;
  95. }
  96. void CResultManager::setIdentifyComplete(bool iscomplete)
  97. {
  98. identify_complete = iscomplete;
  99. }
  100. result_buffer_state * CResultManager::getBufferstate(void * result)
  101. {
  102. int buffer_index = -1;
  103. for (int i = 0; i < max_result_buffer_count; i++)
  104. {
  105. if (result == this->result[i]){ buffer_index = i; break; }
  106. }
  107. return buffer_index < 0 ? NULL : &m_buffer_state[buffer_index];
  108. }
  109. void CResultManager::FreeResult(identify::result::OMR_RESULT * &result)
  110. {
  111. for (int i = 0; i < this->result.size(); i++)
  112. {
  113. if (this->result[i] == result){
  114. this->result[i]->reset();
  115. m_buffer_state[i] = unused;
  116. result = NULL;
  117. break;
  118. }
  119. }
  120. }
  121. identify::result::OMR_RESULT * CResultManager::GetResult(BOOL wait)
  122. {
  123. do
  124. {
  125. if (m_buffer_state[m_next_result_index] == identified){
  126. m_buffer_state[m_next_result_index] = handling;
  127. identify::result::OMR_RESULT *ret = (identify::result::OMR_RESULT *)result[m_next_result_index];
  128. m_next_result_index = (m_next_result_index + 1) % max_result_buffer_count;
  129. return ret;
  130. }
  131. if (identify_complete&&m_buffer_state[m_next_result_index] == unused)return NULL;
  132. if (wait)Sleep(10);
  133. } while (wait);
  134. return NULL;
  135. }
  136. identify::result::OMR_RESULT * CResultManager::getNextResult()
  137. {
  138. identify::result::OMR_RESULT * out_result = NULL;
  139. int buffer_index = -1;
  140. do{
  141. for (int i = 0; i < max_result_buffer_count; i++)
  142. {
  143. int _index = (m_next_result_index + i) % max_result_buffer_count;
  144. if (m_buffer_state[_index] != unused) continue;
  145. out_result = result[_index];
  146. buffer_index = _index;
  147. m_buffer_state[buffer_index] = identifing;
  148. break;
  149. }
  150. if (out_result == NULL)Sleep(10);
  151. } while (out_result == NULL);
  152. return out_result;
  153. }
  154. void CResultManager::open()
  155. {
  156. m_result_buffer.resize(m_buffer_len);
  157. for (int i = 0; i < m_buffer_len; i++)
  158. {
  159. result[i] = &m_result_buffer[i];
  160. m_buffer_state[i] = unused;
  161. }
  162. }
  163. void CResultManager::reset()
  164. {
  165. m_next_result_index = 0;
  166. }
  167. CResultManager::~CResultManager()
  168. {
  169. }
  170. CResultManager::CResultManager(int buffer_len /*=max_result_buffer_count*/)
  171. {
  172. m_buffer_len = buffer_len;
  173. m_next_result_index = 0;
  174. identify_complete = false;
  175. result.resize(buffer_len, NULL);
  176. m_buffer_state.resize(buffer_len, unused);
  177. }