Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

761 lines
14 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 05/11/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgArray.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of LgArray.h
  13. //----------------------------------------------------------------------------
  14. #ifndef __LG__ARRAY__H__INCLUDED__
  15. #define __LG__ARRAY__H__INCLUDED__
  16. #ifdef __BCPLUSPLUS__
  17. #pragma warn -rch
  18. #pragma warn -ccc
  19. #pragma warn -winl
  20. template <class TYPE, class ARGTYPE>
  21. #else
  22. template <class TYPE, class ARGTYPE = TYPE&>
  23. #endif
  24. class CLgArray
  25. {
  26. public:
  27. CLgArray() { m_pCurrent = m_pTail = m_pHead = NULL; m_nCount = 0; }
  28. CLgArray( CLgArray<TYPE,ARGTYPE>& array )
  29. {
  30. m_pCurrent = m_pTail = m_pHead = NULL; m_nCount = 0;
  31. // copy array
  32. for( INT i = 0; i < array.GetCount(); i++ )
  33. Add( array[i] );
  34. }
  35. virtual ~CLgArray() { RemoveAll(); }
  36. public:
  37. CLgArray<TYPE,ARGTYPE>& operator =( CLgArray<TYPE,ARGTYPE>& array )
  38. {
  39. RemoveAll();
  40. // copy array
  41. for( INT i = 0; i < array.GetCount(); i++ )
  42. Add( array[i] );
  43. return *this;
  44. }
  45. public:
  46. virtual BOOL AddEmpty()
  47. {
  48. LPNODE pNode = new NODE;
  49. if( !pNode )
  50. return FALSE;
  51. // place element
  52. pNode->pPrev = m_pTail;
  53. pNode->pNext = NULL;
  54. pNode->nIndex = m_nCount;
  55. // update last & head pointer
  56. if( m_pTail ) m_pTail->pNext = pNode;
  57. else m_pHead = pNode;
  58. // set last pointer & store ponter to current element
  59. m_pCurrent = m_pTail = pNode;
  60. // increase count
  61. m_nCount++;
  62. return TRUE;
  63. }
  64. virtual ARGTYPE AddEmptyRef()
  65. {
  66. if( AddEmpty() )
  67. return GetAt( GetCount()-1 );
  68. static TYPE data;
  69. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::AddEmptyRef"), _T("Can't allocate new item, returned default item") );
  70. return data;
  71. }
  72. virtual BOOL Add( ARGTYPE data )
  73. {
  74. LPNODE pNode = new NODE;
  75. if( !pNode )
  76. return FALSE;
  77. // place element
  78. pNode->pPrev = m_pTail;
  79. pNode->pNext = NULL;
  80. pNode->tData = data;
  81. pNode->nIndex = m_nCount;
  82. // update last & head pointer
  83. if( m_pTail ) m_pTail->pNext = pNode;
  84. else m_pHead = pNode;
  85. // set last pointer & store ponter to current element
  86. m_pCurrent = m_pTail = pNode;
  87. // increase count
  88. m_nCount++;
  89. return TRUE;
  90. }
  91. virtual BOOL Remove( INT nIndex )
  92. {
  93. if( nIndex < 0 || nIndex >= m_nCount )
  94. return FALSE;
  95. // find index
  96. LPNODE pNode = FindItem( nIndex );
  97. if( !pNode )
  98. return FALSE;
  99. // update prev element
  100. if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext;
  101. else m_pHead = pNode->pNext;
  102. // update next element
  103. if( pNode->pNext )
  104. {
  105. // set current pointer
  106. m_pCurrent = pNode->pNext;
  107. m_pCurrent->pPrev = pNode->pPrev;
  108. // delete node
  109. delete pNode;
  110. // update indexes
  111. pNode = m_pCurrent;
  112. while( pNode )
  113. {
  114. pNode->nIndex--;
  115. pNode = pNode->pNext;
  116. }
  117. }
  118. else
  119. {
  120. // set tail pointer
  121. m_pCurrent = m_pTail = pNode->pPrev;
  122. // delete node
  123. delete pNode;
  124. }
  125. // update counter
  126. m_nCount--;
  127. return TRUE;
  128. }
  129. virtual INT InsertAt( INT nIndex, ARGTYPE data )
  130. {
  131. if( nIndex < 0 || nIndex > m_nCount )
  132. return FALSE;
  133. if( nIndex == m_nCount )
  134. return Add( data );
  135. // find index
  136. LPNODE pIndexNode = FindItem( nIndex );
  137. if( !pIndexNode )
  138. return FALSE;
  139. LPNODE pNode = new NODE;
  140. if( !pNode )
  141. return FALSE;
  142. // place element
  143. pNode->pPrev = pIndexNode->pPrev;
  144. pNode->pNext = pIndexNode;
  145. pNode->tData = data;
  146. pNode->nIndex = nIndex;
  147. // check head position
  148. if( pIndexNode->pPrev ) pIndexNode->pPrev->pNext = pNode;
  149. else m_pHead = pNode;
  150. // update node that was on index place pointer
  151. pIndexNode->pPrev = pNode;
  152. // update current position
  153. m_pCurrent = pNode;
  154. // update indexes
  155. pNode = pIndexNode;
  156. while( pNode )
  157. {
  158. pNode->nIndex++;
  159. pNode = pNode->pNext;
  160. }
  161. // increase count
  162. m_nCount++;
  163. return nIndex;
  164. }
  165. virtual BOOL RemoveAll()
  166. {
  167. if( !m_nCount || !m_pTail || !m_pHead )
  168. return TRUE;
  169. LPNODE pNode = m_pHead;
  170. while( pNode )
  171. {
  172. LPNODE pRemoveNode = pNode;
  173. pNode = pNode->pNext;
  174. delete pRemoveNode;
  175. }
  176. m_pCurrent = m_pTail = m_pHead = NULL;
  177. m_nCount = 0;
  178. return TRUE;
  179. }
  180. virtual BOOL RemoveHead()
  181. {
  182. return Remove( 0 );
  183. }
  184. virtual BOOL RemoveTail()
  185. {
  186. return Remove( GetCount() - 1 );
  187. }
  188. public:
  189. enum { INVALID_INDEX = -1 };
  190. ARGTYPE operator[] ( int nIndex )
  191. {
  192. return GetAt( nIndex );
  193. }
  194. ARGTYPE GetAt( int nIndex )
  195. {
  196. static TYPE data;
  197. if( nIndex < 0 || nIndex >= m_nCount )
  198. {
  199. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::GetAt"), _T("Index %d is out of bounds [0..%d)"), nIndex, m_nCount );
  200. return data;
  201. }
  202. // find index
  203. m_pCurrent = FindItem( nIndex );
  204. if( !m_pCurrent )
  205. return data;
  206. return m_pCurrent->tData;
  207. }
  208. ARGTYPE GetHead()
  209. {
  210. return GetAt( 0 );
  211. }
  212. ARGTYPE GetTail()
  213. {
  214. return GetAt( GetCount() - 1 );
  215. }
  216. INT GetCount()
  217. {
  218. return m_nCount;
  219. }
  220. INT Find( ARGTYPE data )
  221. {
  222. LPNODE pNode = m_pHead;
  223. while( pNode )
  224. {
  225. if( pNode->tData == data )
  226. return pNode->nIndex;
  227. pNode = pNode->pNext;
  228. }
  229. return INVALID_INDEX;
  230. }
  231. public:
  232. friend BOOL operator == ( CLgArray<TYPE,ARGTYPE>& a1, CLgArray<TYPE,ARGTYPE>& a2 )
  233. {
  234. if( a1.GetCount() != a2.GetCount() )
  235. return FALSE;
  236. for( INT i = 0; i < a1.GetCount(); i++ )
  237. if( !(a1[i] == a2[i]) )
  238. return FALSE;
  239. return TRUE;
  240. }
  241. private:
  242. struct NODE
  243. {
  244. struct NODE far *pNext;
  245. struct NODE far *pPrev;
  246. TYPE tData;
  247. INT nIndex;
  248. };
  249. typedef NODE* PNODE;
  250. typedef NODE far* LPNODE;
  251. private:
  252. LPNODE FindItem( INT nIndex )
  253. {
  254. // if index before then current
  255. if( m_pCurrent->nIndex > nIndex )
  256. {
  257. // if closer to current
  258. if( ( nIndex - m_pHead->nIndex ) > ( m_pCurrent->nIndex - nIndex ) )
  259. {
  260. LPNODE pNode = m_pCurrent;
  261. while( pNode->nIndex != nIndex )
  262. pNode = pNode->pPrev;
  263. return pNode;
  264. }
  265. else
  266. {
  267. LPNODE pNode = m_pHead;
  268. while( pNode->nIndex != nIndex )
  269. pNode = pNode->pNext;
  270. return pNode;
  271. }
  272. }
  273. else
  274. {
  275. // if closer to current
  276. if( ( nIndex - m_pCurrent->nIndex ) > ( m_pTail->nIndex - nIndex ) )
  277. {
  278. LPNODE pNode = m_pCurrent;
  279. while( pNode->nIndex != nIndex )
  280. pNode = pNode->pNext;
  281. return pNode;
  282. }
  283. else
  284. {
  285. LPNODE pNode = m_pTail;
  286. while( pNode->nIndex != nIndex )
  287. pNode = pNode->pPrev;
  288. return pNode;
  289. }
  290. }
  291. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::FindItem"), _T("Can't find item with index %d"), nIndex );
  292. return NULL;
  293. }
  294. private:
  295. LPNODE m_pHead;
  296. LPNODE m_pTail;
  297. LPNODE m_pCurrent;
  298. INT m_nCount;
  299. };
  300. template <class TYPE, const int MAXSIZE>
  301. class CLgQueue : public CLgArray<TYPE>
  302. {
  303. public:
  304. CLgQueue()
  305. {
  306. m_dwOverflowSkiped = 0i64;
  307. }
  308. virtual ~CLgQueue()
  309. {
  310. if( m_dwOverflowSkiped )
  311. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::~CLgQueue"), _T("Overflow, lost %ld items"), m_dwOverflowSkiped );
  312. }
  313. public:
  314. virtual BOOL Push( TYPE data )
  315. {
  316. if( GetCount() >= MAXSIZE )
  317. {
  318. if( !m_dwOverflowSkiped )
  319. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::Push"), _T("Overflow (more then %d items)"), MAXSIZE );
  320. m_dwOverflowSkiped++;
  321. return FALSE;
  322. }
  323. if( m_dwOverflowSkiped )
  324. {
  325. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::Push"), _T("Overflow, lost %ld items"), m_dwOverflowSkiped );
  326. m_dwOverflowSkiped = 0i64;
  327. }
  328. return Add( data );
  329. }
  330. virtual BOOL Pop( TYPE& data )
  331. {
  332. if( !GetCount() )
  333. return FALSE;
  334. data = GetAt(0);
  335. return Remove(0);
  336. }
  337. virtual BOOL Clear()
  338. {
  339. return RemoveAll();
  340. }
  341. private:
  342. __int64 m_dwOverflowSkiped;
  343. };
  344. template <class TYPE, class ARGTYPE = TYPE&>
  345. class CLgStack : public CLgArray<TYPE,ARGTYPE>
  346. {
  347. public:
  348. virtual ~CLgStack()
  349. {
  350. RemoveAll();
  351. }
  352. public:
  353. virtual BOOL Push( ARGTYPE data )
  354. {
  355. return Add( data );
  356. }
  357. virtual BOOL Peek( ARGTYPE data )
  358. {
  359. if( !GetCount() )
  360. return FALSE;
  361. // store data
  362. data = GetTail();
  363. return TRUE;
  364. }
  365. virtual BOOL Pop( ARGTYPE data )
  366. {
  367. // get data
  368. if( !Peek( data ) )
  369. return FALSE;
  370. // remove last
  371. return RemoveTail();
  372. }
  373. };
  374. template <class TYPE, class ARGTYPE = TYPE&>
  375. class CLgList
  376. {
  377. private:
  378. struct NODE
  379. {
  380. struct NODE far *pNext;
  381. struct NODE far *pPrev;
  382. TYPE tData;
  383. INT nIndex;
  384. };
  385. typedef NODE* PNODE;
  386. typedef NODE far* LPNODE;
  387. public:
  388. CLgList() { m_pTail = m_pHead = NULL; m_nCount = 0; }
  389. CLgList( CLgList<TYPE,ARGTYPE>& list )
  390. {
  391. m_pTail = m_pHead = NULL; m_nCount = 0;
  392. // copy list
  393. POSITION pos = list.GetHeadPosition();
  394. while( pos )
  395. Add( list.GetNext( pPos ) );
  396. }
  397. virtual ~CLgList() { RemoveAll(); }
  398. public:
  399. CLgList<TYPE,ARGTYPE>& operator =( CLgList<TYPE,ARGTYPE>& list )
  400. {
  401. RemoveAll();
  402. // copy list
  403. PVOID pos = list.GetHeadPosition();
  404. while( pos )
  405. Add( list.GetNext( pos ) );
  406. return *this;
  407. }
  408. public:
  409. virtual PVOID AddEmpty()
  410. {
  411. LPNODE pNode = new NODE;
  412. if( !pNode )
  413. return FALSE;
  414. // place element
  415. pNode->pPrev = m_pTail;
  416. pNode->pNext = NULL;
  417. pNode->nIndex = m_nCount;
  418. // update last & head pointer
  419. if( m_pTail ) m_pTail->pNext = pNode;
  420. else m_pHead = pNode;
  421. // set last pointer
  422. m_pTail = pNode;
  423. // increase count
  424. m_nCount++;
  425. return pNode;
  426. }
  427. virtual ARGTYPE AddEmptyRef()
  428. {
  429. PNODE pNode = (PNODE)AddEmpty();
  430. if( pNode )
  431. return GetAt( pNode );
  432. static TYPE data;
  433. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgList::AddEmptyRef"), _T("Can't allocate new item, returned default item") );
  434. return data;
  435. }
  436. virtual PVOID Add( ARGTYPE data )
  437. {
  438. LPNODE pNode = new NODE;
  439. if( !pNode )
  440. return NULL;
  441. // place element
  442. pNode->pPrev = m_pTail;
  443. pNode->pNext = NULL;
  444. pNode->tData = data;
  445. // update last & head pointer
  446. if( m_pTail ) m_pTail->pNext = pNode;
  447. else m_pHead = pNode;
  448. // set last pointer
  449. m_pTail = pNode;
  450. // increase count
  451. m_nCount++;
  452. return pNode;
  453. }
  454. virtual BOOL Remove( PVOID pos )
  455. {
  456. LPNODE pNode = (PNODE)pos;
  457. if( !pNode )
  458. return FALSE;
  459. // update prev element
  460. if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext;
  461. else m_pHead = pNode->pNext;
  462. // update next element
  463. if( pNode->pNext ) pNode->pNext->pPrev = pNode->pPrev;
  464. else m_pTail = pNode->pPrev;
  465. // delete node
  466. delete pNode;
  467. // update counter
  468. m_nCount--;
  469. return TRUE;
  470. }
  471. virtual PVOID InsertAt( PVOID pos, ARGTYPE data )
  472. {
  473. if( pos == m_pTail )
  474. return Add( data );
  475. // find index
  476. LPNODE pIndexNode = (PNODE)pos;
  477. if( !pIndexNode )
  478. return FALSE;
  479. LPNODE pNode = new NODE;
  480. if( !pNode )
  481. return FALSE;
  482. // place element
  483. pNode->pPrev = pIndexNode->pPrev;
  484. pNode->pNext = pIndexNode;
  485. pNode->tData = data;
  486. // check head PVOID
  487. if( pIndexNode->pPrev ) pIndexNode->pPrev->pNext = pNode;
  488. else m_pHead = pNode;
  489. // update node that was on index place pointer
  490. pIndexNode->pPrev = pNode;
  491. // increase count
  492. m_nCount++;
  493. return pNode;
  494. }
  495. virtual BOOL MoveToTail( PVOID pos )
  496. {
  497. PNODE pNode = (PNODE)pos;
  498. if( !pNode )
  499. return FALSE;
  500. if( pNode == m_pTail )
  501. return TRUE;
  502. // update prev element
  503. if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext;
  504. else m_pHead = pNode->pNext;
  505. // update next element
  506. if( pNode->pNext ) pNode->pNext->pPrev = pNode->pPrev;
  507. else m_pTail = pNode->pPrev;
  508. // move to tail
  509. m_pTail->pNext = pNode;
  510. pNode->pNext = NULL;
  511. pNode->pPrev = m_pTail;
  512. m_pTail = pNode;
  513. return TRUE;
  514. }
  515. virtual BOOL RemoveAll()
  516. {
  517. LPNODE pNode = m_pHead;
  518. while( pNode )
  519. {
  520. LPNODE pRemoveNode = pNode;
  521. pNode = pNode->pNext;
  522. delete pRemoveNode;
  523. }
  524. m_pTail = m_pHead = NULL;
  525. m_nCount = 0;
  526. return TRUE;
  527. }
  528. virtual BOOL RemoveHead()
  529. {
  530. return Remove( m_pHead );
  531. }
  532. virtual BOOL RemoveTail()
  533. {
  534. return Remove( m_pTail );
  535. }
  536. public:
  537. ARGTYPE GetAt( PVOID pos )
  538. {
  539. static TYPE data;
  540. // find index
  541. PNODE pNode = (PNODE)pos;
  542. if( !pNode )
  543. return data;
  544. return pNode->tData;
  545. }
  546. ARGTYPE GetNext( PVOID& pos )
  547. {
  548. static TYPE data;
  549. // find index
  550. PNODE pNode = (PNODE)pos;
  551. if( !pNode )
  552. return data;
  553. reinterpret_cast<PNODE&>(pos) = reinterpret_cast<PNODE&>(pos)->pNext;
  554. return pNode->tData;
  555. }
  556. ARGTYPE GetPrev( PVOID& pos )
  557. {
  558. static TYPE data;
  559. // find index
  560. PNODE pNode = (PNODE)pos;
  561. if( !pNode )
  562. return data;
  563. reinterpret_cast<PNODE&>(pos) = reinterpret_cast<PNODE&>(pos)->pPrev;
  564. return pNode->tData;
  565. }
  566. PVOID GetHeadPosition()
  567. {
  568. return m_pHead;
  569. }
  570. PVOID GetTailPosition()
  571. {
  572. return m_pTail;
  573. }
  574. ARGTYPE GetHead()
  575. {
  576. return GetAt( m_pHead );
  577. }
  578. ARGTYPE GetTail()
  579. {
  580. return GetAt( m_pTail );
  581. }
  582. INT GetCount()
  583. {
  584. return m_nCount;
  585. }
  586. PVOID Find( ARGTYPE data )
  587. {
  588. LPNODE pNode = m_pHead;
  589. while( pNode )
  590. {
  591. if( pNode->tData == data )
  592. return pNode->nIndex;
  593. pNode = pNode->pNext;
  594. }
  595. return NULL;
  596. }
  597. public:
  598. friend BOOL operator == ( CLgList<TYPE,ARGTYPE>& a1, CLgList<TYPE,ARGTYPE>& a2 )
  599. {
  600. if( a1.GetCount() != a2.GetCount() )
  601. return FALSE;
  602. for( INT i = 0; i < a1.GetCount(); i++ )
  603. if( !(a1[i] == a2[i]) )
  604. return FALSE;
  605. return TRUE;
  606. }
  607. private:
  608. LPNODE m_pHead;
  609. LPNODE m_pTail;
  610. INT m_nCount;
  611. };
  612. #endif