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.

513 lines
10 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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Stream.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of Stream.h
  13. //----------------------------------------------------------------------------
  14. #ifndef __STREAM__H__INCLUDED__
  15. #define __STREAM__H__INCLUDED__
  16. #include "..\lgs\stdafx.h"
  17. /*
  18. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  19. class CStream
  20. {
  21. public:
  22. CStream();
  23. ~CStream();
  24. public:
  25. BOOL Create();
  26. void Destroy();
  27. public:
  28. BOOL Put( TYPE byte );
  29. BOOL Get( TYPE *pByte );
  30. INT Get( TYPE *pByte, INT nCount );
  31. INT Put( TYPE *pByte, INT nCount );
  32. INT GetCount() { return m_nCount; }
  33. INT GetOverflow();
  34. void Clear();
  35. private:
  36. #ifndef INCLUDE_WINDOWS_DEFINITIONS
  37. TYPE m_pBuffer[ MAX_CAPACITY ];
  38. INT m_nCount;
  39. INT m_nHead;
  40. INT m_nTail;
  41. INT m_nOverflow;
  42. #else
  43. volatile TYPE m_pBuffer[ MAX_CAPACITY ];
  44. volatile INT m_nCount;
  45. volatile INT m_nHead;
  46. volatile INT m_nTail;
  47. volatile INT m_nOverflow;
  48. #endif
  49. };
  50. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  51. CStream<TYPE,MAX_CAPACITY,OVERFLOW>::CStream()
  52. {
  53. m_nCount = 0;
  54. m_nHead = 0;
  55. m_nTail = 0;
  56. m_nOverflow = 0;
  57. }
  58. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  59. CStream<TYPE,MAX_CAPACITY,OVERFLOW>::~CStream()
  60. {
  61. Destroy();
  62. }
  63. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  64. BOOL CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Create()
  65. {
  66. Destroy();
  67. m_nCount = 0;
  68. m_nHead = 0;
  69. m_nTail = 0;
  70. m_nOverflow = 0;
  71. return TRUE;
  72. }
  73. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  74. void CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Destroy()
  75. {
  76. m_nCount = 0;
  77. m_nHead = 0;
  78. m_nTail = 0;
  79. m_nOverflow = 0;
  80. }
  81. #ifdef __BCPLUSPLUS__
  82. #pragma warn -rch
  83. #pragma warn -ccc
  84. #endif
  85. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  86. BOOL CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Put( TYPE byte )
  87. {
  88. if( OVERFLOW )
  89. {
  90. m_pBuffer[ m_nHead ] = byte;
  91. m_nHead = (m_nHead + 1) % MAX_CAPACITY;
  92. if( m_nCount >= MAX_CAPACITY )
  93. {
  94. m_nTail = ( m_nTail + 1 ) % MAX_CAPACITY;
  95. m_nOverflow++;
  96. }
  97. else
  98. m_nCount++;
  99. }
  100. else
  101. {
  102. if( m_nCount >= MAX_CAPACITY )
  103. return FALSE;
  104. m_pBuffer[ m_nHead ] = byte;
  105. m_nHead = (m_nHead + 1) % MAX_CAPACITY;
  106. m_nCount ++;
  107. }
  108. return TRUE;
  109. }
  110. #ifdef __BCPLUSPLUS__
  111. #pragma warn .ccc
  112. #pragma warn .rch
  113. #endif
  114. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  115. BOOL CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Get( TYPE *pByte )
  116. {
  117. if( m_nCount <= 0 )
  118. return FALSE;
  119. *pByte = m_pBuffer[ m_nTail ];
  120. m_nTail = (m_nTail + 1) % MAX_CAPACITY;
  121. m_nCount --;
  122. return TRUE;
  123. }
  124. #ifdef __BCPLUSPLUS__
  125. #pragma warn -rch
  126. #pragma warn -ccc
  127. #endif
  128. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  129. INT CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Put( TYPE *pByte, INT nCount )
  130. {
  131. if( OVERFLOW )
  132. {
  133. for( INT i = 0; i < nCount; i++ )
  134. {
  135. m_pBuffer[ m_nHead ] = *pByte++;
  136. m_nHead = (m_nHead + 1) % MAX_CAPACITY;
  137. if( m_nCount >= MAX_CAPACITY )
  138. {
  139. m_nTail = ( m_nTail + 1 ) % MAX_CAPACITY;
  140. m_nOverflow++;
  141. }
  142. else
  143. m_nCount++;
  144. }
  145. return nCount;
  146. }
  147. else
  148. {
  149. for( INT i = 0; (i < nCount) && (m_nCount < MAX_CAPACITY); i++ )
  150. {
  151. m_pBuffer[ m_nHead ] = *pByte++;
  152. m_nHead = (m_nHead + 1) % MAX_CAPACITY;
  153. m_nCount ++;
  154. }
  155. return i;
  156. }
  157. return 0;
  158. }
  159. #ifdef __BCPLUSPLUS__
  160. #pragma warn .ccc
  161. #pragma warn .rch
  162. #endif
  163. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  164. INT CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Get( TYPE *pByte, INT nCount )
  165. {
  166. for( INT i = 0; (i < nCount) && m_nCount; i++ )
  167. {
  168. *pByte++ = m_pBuffer[ m_nTail ];
  169. m_nTail = (m_nTail + 1) % MAX_CAPACITY;
  170. m_nCount --;
  171. }
  172. return i;
  173. }
  174. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  175. void CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Clear()
  176. {
  177. m_nCount = 0;
  178. m_nTail = 0;
  179. m_nHead = 0;
  180. m_nOverflow = 0;
  181. }
  182. template< class TYPE, INT MAX_CAPACITY, BOOL OVERFLOW >
  183. INT CStream<TYPE,MAX_CAPACITY,OVERFLOW>::GetOverflow()
  184. {
  185. INT nOverflow = m_nOverflow;
  186. m_nOverflow = 0;
  187. return nOverflow;
  188. }
  189. */
  190. ////////////////////////////////////////////////////////////////////////////////
  191. // fast stream without overflow
  192. ////////////////////////////////////////////////////////////////////////////////
  193. template< class TYPE, const INT MAX_CAPACITY >
  194. class CFastStream
  195. {
  196. public:
  197. CFastStream();
  198. virtual ~CFastStream();
  199. public:
  200. virtual BOOL Create();
  201. virtual void Destroy();
  202. public:
  203. virtual BOOL Put( TYPE byte );
  204. BOOL Get( TYPE *pByte );
  205. virtual INT Put( TYPE *pByte, INT nCount );
  206. INT Get( TYPE *pByte, INT nCount );
  207. INT GetCount() { return m_nCount; }
  208. virtual void Clear();
  209. protected:
  210. #ifndef INCLUDE_WINDOWS_DEFINITIONS
  211. TYPE m_pBuffer[ 1 << MAX_CAPACITY ];
  212. INT m_nCount;
  213. INT m_nHead;
  214. INT m_nTail;
  215. #else
  216. volatile TYPE m_pBuffer[ 1 << MAX_CAPACITY ];
  217. volatile INT m_nCount;
  218. volatile INT m_nHead;
  219. volatile INT m_nTail;
  220. #endif
  221. };
  222. template< class TYPE, const INT MAX_CAPACITY >
  223. CFastStream<TYPE,MAX_CAPACITY>::CFastStream()
  224. {
  225. m_nCount = m_nHead = m_nTail = 0;
  226. }
  227. template< class TYPE, const INT MAX_CAPACITY >
  228. CFastStream<TYPE,MAX_CAPACITY>::~CFastStream()
  229. {
  230. Destroy();
  231. }
  232. template< class TYPE, const INT MAX_CAPACITY >
  233. BOOL CFastStream<TYPE,MAX_CAPACITY>::Create()
  234. {
  235. Destroy();
  236. m_nCount = m_nHead = m_nTail = 0;
  237. return TRUE;
  238. }
  239. template< class TYPE, const INT MAX_CAPACITY >
  240. void CFastStream<TYPE,MAX_CAPACITY>::Destroy()
  241. {
  242. m_nCount = m_nHead = m_nTail = 0;
  243. }
  244. template< class TYPE, const INT MAX_CAPACITY >
  245. BOOL CFastStream<TYPE,MAX_CAPACITY>::Put( TYPE byte )
  246. {
  247. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  248. const INT cCapacityMask = cMaxCapacity-1;
  249. if( m_nCount >= cMaxCapacity )
  250. return FALSE;
  251. m_pBuffer[ m_nHead ] = byte;
  252. m_nHead = (m_nHead + 1) & cCapacityMask;
  253. m_nCount ++;
  254. return TRUE;
  255. }
  256. template< class TYPE, const INT MAX_CAPACITY >
  257. BOOL CFastStream<TYPE,MAX_CAPACITY>::Get( TYPE *pByte )
  258. {
  259. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  260. const INT cCapacityMask = cMaxCapacity-1;
  261. if( m_nCount <= 0 )
  262. return FALSE;
  263. *pByte = m_pBuffer[ m_nTail ];
  264. m_nTail = (m_nTail + 1) & cCapacityMask;
  265. m_nCount --;
  266. return TRUE;
  267. }
  268. template< class TYPE, const INT MAX_CAPACITY >
  269. INT CFastStream<TYPE,MAX_CAPACITY>::Put( TYPE *pByte, INT nCount )
  270. {
  271. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  272. const INT cCapacityMask = cMaxCapacity-1;
  273. for( INT i = 0; (i < nCount) && (m_nCount < cMaxCapacity); i++ )
  274. {
  275. m_pBuffer[ m_nHead ] = *pByte++;
  276. m_nHead = (m_nHead + 1) & cCapacityMask;
  277. m_nCount ++;
  278. }
  279. return i;
  280. }
  281. template< class TYPE, const INT MAX_CAPACITY >
  282. INT CFastStream<TYPE,MAX_CAPACITY>::Get( TYPE *pByte, INT nCount )
  283. {
  284. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  285. const INT cCapacityMask = cMaxCapacity-1;
  286. for( INT i = 0; (i < nCount) && m_nCount; i++ )
  287. {
  288. *pByte++ = m_pBuffer[ m_nTail ];
  289. m_nTail = (m_nTail + 1) & cCapacityMask;
  290. m_nCount --;
  291. }
  292. return i;
  293. }
  294. template< class TYPE, const INT MAX_CAPACITY >
  295. void CFastStream<TYPE,MAX_CAPACITY>::Clear()
  296. {
  297. m_nCount = m_nTail = m_nHead = 0;
  298. }
  299. ////////////////////////////////////////////////////////////////////////////////
  300. // fast stream with overflow
  301. ////////////////////////////////////////////////////////////////////////////////
  302. template< class TYPE, const INT MAX_CAPACITY >
  303. class CFastStreamOverflow :
  304. public CFastStream<TYPE,MAX_CAPACITY>
  305. {
  306. public:
  307. CFastStreamOverflow() { m_nOverflow = 0; }
  308. virtual ~CFastStreamOverflow() { Destroy(); }
  309. public:
  310. virtual BOOL Create()
  311. {
  312. if( !CFastStream<TYPE,MAX_CAPACITY>::Create() )
  313. return FALSE;
  314. m_nOverflow = 0;
  315. return TRUE;
  316. }
  317. virtual void Destroy()
  318. {
  319. CFastStream<TYPE,MAX_CAPACITY>::Destroy();
  320. m_nOverflow = 0;
  321. }
  322. public:
  323. virtual BOOL Put( TYPE byte )
  324. {
  325. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  326. const INT cCapacityMask = cMaxCapacity-1;
  327. m_pBuffer[ m_nHead ] = byte;
  328. m_nHead = (m_nHead + 1) & cCapacityMask;
  329. if( m_nCount >= cMaxCapacity )
  330. {
  331. m_nTail = ( m_nTail + 1 ) & cCapacityMask;
  332. m_nOverflow++;
  333. }
  334. else
  335. m_nCount++;
  336. return TRUE;
  337. }
  338. virtual INT Put( TYPE *pByte, INT nCount );
  339. virtual void Clear()
  340. {
  341. CFastStream<TYPE,MAX_CAPACITY>::Clear();
  342. m_nOverflow = 0;
  343. }
  344. INT GetOverflow()
  345. {
  346. INT nOverflow = m_nOverflow;
  347. m_nOverflow = 0;
  348. return nOverflow;
  349. }
  350. protected:
  351. volatile INT m_nOverflow;
  352. };
  353. // in Borland C++ this function can't be inline
  354. template< class TYPE, const INT MAX_CAPACITY >
  355. INT CFastStreamOverflow<TYPE,MAX_CAPACITY>::Put( TYPE *pByte, INT nCount )
  356. {
  357. const INT cMaxCapacity = (1<<MAX_CAPACITY);
  358. const INT cCapacityMask = cMaxCapacity-1;
  359. for( INT i = 0; i < nCount; i++ )
  360. {
  361. m_pBuffer[ m_nHead ] = *pByte++;
  362. m_nHead = (m_nHead + 1) & cCapacityMask;
  363. if( m_nCount >= cMaxCapacity )
  364. {
  365. m_nTail = ( m_nTail + 1 ) & cCapacityMask;
  366. m_nOverflow++;
  367. }
  368. else
  369. m_nCount++;
  370. }
  371. return nCount;
  372. }
  373. // NOTE: remember about interger overflow !!!
  374. template <class VALTYPE, class SUMTYPE, const INT LENGTH>
  375. class CFastHistorySum
  376. {
  377. public:
  378. CFastHistorySum()
  379. {
  380. memset( m_pHistory, 0, sizeof(m_pHistory) );
  381. Clear();
  382. }
  383. public:
  384. void Clear()
  385. {
  386. m_nSum = 0;
  387. m_nCount = 0;
  388. m_nHead = 0;
  389. m_nTail = 0;
  390. }
  391. void Put( VALTYPE value )
  392. {
  393. const INT nMaxCount = 1 << LENGTH;
  394. const INT nMask = nMaxCount - 1;
  395. if( m_nCount < nMaxCount )
  396. {
  397. // accumulate sum
  398. m_nSum += value;
  399. // store in history
  400. m_pHistory[ m_nHead ] = value;
  401. // update head pointer
  402. m_nHead = (m_nHead + 1) & nMask;
  403. // increase count
  404. m_nCount++;
  405. }
  406. else
  407. {
  408. // update "floating" medium
  409. m_nSum = m_nSum - m_pHistory[ m_nTail ] + value;
  410. // store new value in history
  411. m_pHistory[ m_nHead ] = value;
  412. // move pointers
  413. m_nHead = (m_nTail + 1) & nMask;
  414. m_nTail = (m_nTail + 1) & nMask;
  415. }
  416. }
  417. const INT GetTotalLength() const { return 1 << LENGTH; }
  418. BOOL Get( SUMTYPE *pSum )
  419. {
  420. const INT nMaxCount = 1 << LENGTH;
  421. const INT nMask = nMaxCount - 1;
  422. // if history not comlette yet
  423. if( m_nCount < nMaxCount )
  424. return FALSE;
  425. *pSum = m_nSum;
  426. return TRUE;
  427. }
  428. public:
  429. VALTYPE m_pHistory[ 1 << LENGTH ];
  430. SUMTYPE m_nSum;
  431. INT m_nCount;
  432. INT m_nHead;
  433. INT m_nTail;
  434. };
  435. #endif