ITSLibF.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //---------------------------------------------------------------------------
  2. #ifndef ITSLibFH
  3. #define ITSLibFH
  4. //---------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #ifndef __ITSLIb_EXPORT__
  7. #pragma comment(lib, "ITSLib.lib")
  8. #endif // #ifndef __ITSLIb_EXPORT__
  9. //---------------------------------------------------------------------------
  10. #include <vcl.h>
  11. #include <map>
  12. #include <list>
  13. #include <vector>
  14. #include <algorithm>
  15. #include <Classes.hpp>
  16. /*
  17. ***************************************************************************************************
  18. * STL Define
  19. ***************************************************************************************************/
  20. #ifndef FOR_STL
  21. #define FOR_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetHead(); !(_list).IsEnd(); _pointer = (_list).GetNext())
  22. #endif
  23. #ifndef REV_STL
  24. #define REV_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetTail(); !(_list).IsBegin(); _pointer = (_list).GetPrev())
  25. #endif
  26. /*
  27. ***************************************************************************************************
  28. * DB JOB Define
  29. ***************************************************************************************************/
  30. #define DB_INIT 0
  31. #define DB_NEW 1
  32. #define DB_DELETE 2
  33. #define DB_UPDATE 3
  34. /*
  35. ***************************************************************************************************
  36. * STL OBJECT
  37. ***************************************************************************************************/
  38. namespace ITSLib
  39. {
  40. class TITSAutoLock
  41. {
  42. public:
  43. TITSAutoLock(CRITICAL_SECTION *ACS);
  44. ~TITSAutoLock();
  45. private:
  46. protected:
  47. CRITICAL_SECTION *CS;
  48. };
  49. //---------------------------------------------------------------------------
  50. template <typename Key, class T>
  51. class TITSStlMap
  52. {
  53. protected:
  54. typedef std::map<Key, T> MapObject;
  55. typedef typename std::map<Key, T>::iterator Iter;
  56. typedef typename std::map<Key, T>::reverse_iterator RIter;
  57. Iter FCurrPos;
  58. RIter FRevsPos;
  59. CRITICAL_SECTION CS;
  60. public:
  61. MapObject FObjects;
  62. TITSStlMap()
  63. {
  64. FObjects.clear();
  65. InitializeCriticalSection(&CS);
  66. };
  67. virtual ~TITSStlMap()
  68. {
  69. RemoveAll();
  70. DeleteCriticalSection(&CS);
  71. };
  72. public:
  73. inline CRITICAL_SECTION* __fastcall GetLock()
  74. {
  75. return &CS;
  76. };
  77. inline bool __fastcall Lock()
  78. {
  79. EnterCriticalSection(&CS);
  80. return true;
  81. };
  82. inline void __fastcall UnLock()
  83. {
  84. LeaveCriticalSection(&CS);
  85. };
  86. inline bool __fastcall Push(Key AKey, T AData)
  87. {
  88. std::pair<Iter, bool> pairTwo;
  89. pairTwo = FObjects.insert(std::make_pair<Key, T>(AKey, AData));
  90. return pairTwo.second;
  91. };
  92. inline T __fastcall Find(Key AKey)
  93. {
  94. Iter it;
  95. it = FObjects.find(AKey);
  96. if (it != FObjects.end())
  97. return (it->second);
  98. return (T)NULL;
  99. }
  100. inline void __fastcall RemoveAll()
  101. {
  102. Iter it;
  103. for (it = FObjects.begin(); it != FObjects.end(); ++it)
  104. {
  105. T data = (it->second);
  106. if (data) delete data;
  107. }
  108. FObjects.clear();
  109. }
  110. inline void __fastcall Clear()
  111. {
  112. FObjects.clear();
  113. }
  114. inline bool __fastcall Remove(Key AKey)
  115. {
  116. Iter it;
  117. it = FObjects.find(AKey);
  118. if (it != FObjects.end())
  119. {
  120. T data = (it->second);
  121. if (data) delete data;
  122. FObjects.erase(AKey);
  123. return true;
  124. }
  125. return false;
  126. }
  127. inline bool __fastcall Erase(Key AKey)
  128. {
  129. Iter it;
  130. it = FObjects.find(AKey);
  131. if (it != FObjects.end())
  132. {
  133. FObjects.erase(AKey);
  134. return true;
  135. }
  136. return false;
  137. }
  138. inline int __fastcall Size()
  139. {
  140. return (int)FObjects.size();
  141. }
  142. inline bool __fastcall IsExist(Key AKey)
  143. {
  144. Iter it;
  145. it = FObjects.find(AKey);
  146. return (iter != FObjects.end());
  147. }
  148. inline bool __fastcall IsEmpty()
  149. {
  150. return FObjects.empty();
  151. };
  152. inline T __fastcall GetHead()
  153. {
  154. FCurrPos = FObjects.begin();
  155. if (FCurrPos != FObjects.end())
  156. return (FCurrPos->second);
  157. return (T)NULL;
  158. }
  159. inline T __fastcall GetNext()
  160. {
  161. if (FCurrPos != FObjects.end())
  162. {
  163. FCurrPos++;
  164. if (FCurrPos != FObjects.end())
  165. return (FCurrPos->second);
  166. }
  167. return (T)NULL;
  168. }
  169. inline bool __fastcall IsEnd()
  170. {
  171. return (FCurrPos == FObjects.end());
  172. }
  173. inline T __fastcall GetTail()
  174. {
  175. FRevsPos = FObjects.rbegin();
  176. if (FRevsPos != FObjects.rend())
  177. return (*FRevsPos);
  178. return (T)NULL;
  179. }
  180. inline T __fastcall GetRNext()
  181. {
  182. if (FRevsPos != FObjects.rend())
  183. {
  184. FRevsPos++;
  185. if (FRevsPos != FObjects.rend())
  186. return (*FRevsPos);
  187. }
  188. return (T)NULL;
  189. }
  190. inline bool __fastcall IsREnd()
  191. {
  192. return (FRevsPos == FObjects.rend());
  193. }
  194. inline T __fastcall GetValue(int AIndex)
  195. {
  196. if ((FObjects.size() <= AIndex) || (AIndex < 0))
  197. return (T)NULL;
  198. #if 1
  199. Iter it = FObjects.begin();
  200. if (it == FObjects.end())
  201. return (T)NULL;
  202. for (int ii = 0; ii < AIndex; ii++)
  203. {
  204. it++;
  205. }
  206. #else
  207. Iter it = (FObjects.begin() + AIndex);
  208. #endif
  209. if (it != FObjects.end())
  210. return (it->second);
  211. return (T)NULL;
  212. };
  213. inline T operator[](size_t AIndex)
  214. {
  215. return GetValue(AIndex));
  216. };
  217. };
  218. //-----------------------------------------------------------------------
  219. template <class T>
  220. class TITSStlList
  221. {
  222. protected:
  223. typedef std::list<T> ListObject;
  224. typedef typename std::list<T>::iterator Iter;
  225. typedef typename std::list<T>::reverse_iterator RIter;
  226. Iter FCurrPos;
  227. RIter FRevsPos;
  228. CRITICAL_SECTION CS;
  229. public:
  230. ListObject FObjects;
  231. TITSStlList()
  232. {
  233. FObjects.clear();
  234. InitializeCriticalSection(&CS);
  235. };
  236. virtual ~TITSStlList()
  237. {
  238. Clear(); /* RemoveAll(호출하지 않음) : 포인터객체를 사용하지 않을 수 있음 */
  239. DeleteCriticalSection(&CS);
  240. };
  241. public:
  242. inline CRITICAL_SECTION* __fastcall GetLock()
  243. {
  244. return &CS;
  245. };
  246. inline bool __fastcall Lock()
  247. {
  248. EnterCriticalSection(&CS);
  249. return true;
  250. };
  251. inline void __fastcall UnLock()
  252. {
  253. LeaveCriticalSection(&CS);
  254. };
  255. inline void __fastcall Push(T AData)
  256. {
  257. FObjects.push_back(AData);
  258. };
  259. inline T __fastcall Find()
  260. {
  261. return NULL; /* std::list 에서는 사용하지 않음 */
  262. }
  263. inline void __fastcall RemoveAll()
  264. {
  265. ListObject::const_iterator eIter(FObjects.end());
  266. for (ListObject::const_iterator iter = FObjects.begin(); iter != eIter; ++iter)
  267. {
  268. T data = (*iter);
  269. delete data;
  270. }
  271. FObjects.clear();
  272. };
  273. inline void __fastcall Clear()
  274. {
  275. FObjects.clear();
  276. };
  277. inline bool __fastcall Remove(T AData)
  278. {
  279. Iter iter = std::find(FObjects.begin(), FObjects.end(), AData);
  280. if (iter != FObjects.end())
  281. {
  282. T data = (*iter);
  283. FObjects.erase(iter);
  284. FCurrPos = iter;
  285. if (data) delete data;
  286. return true;
  287. }
  288. return false;
  289. }
  290. inline bool __fastcall Erase(T AData)
  291. {
  292. ITer iter = std::find(FObjects.begin(), FObjects.end(), AData);
  293. if (iter != FObjects.end())
  294. {
  295. FObjects.erase(iter);
  296. FCurrPos = iter;
  297. return true;
  298. }
  299. return false;
  300. }
  301. inline int __fastcall Size()
  302. {
  303. return ((int)FObjects.size());
  304. };
  305. inline bool __fastcall IsExist(T AData)
  306. {
  307. Iter iter = std::find(FObjects.begin(), FObjects.end(), AData);
  308. return (iter != FObjects.end());
  309. };
  310. inline bool __fastcall IsEmpty()
  311. {
  312. return FObjects.empty();
  313. };
  314. inline T __fastcall GetHead()
  315. {
  316. FCurrPos = FObjects.begin();
  317. if (FCurrPos == FObjects.end())
  318. return (T)NULL;
  319. return (*FCurrPos);
  320. };
  321. inline T __fastcall GetNext()
  322. {
  323. if (FCurrPos != FObjects.end())
  324. {
  325. FCurrPos++;
  326. if (FCurrPos != FObjects.end())
  327. return (*FCurrPos);
  328. }
  329. return (T)NULL;
  330. };
  331. inline bool __fastcall IsEnd()
  332. {
  333. return (FCurrPos == FObjects.end());
  334. }
  335. inline T __fastcall GetTail()
  336. {
  337. FRevsPos = FObjects.rbegin();
  338. if (FRevsPos != FObjects.rend())
  339. return (*FRevsPos);
  340. return (T)NULL;
  341. }
  342. inline T __fastcall GetRNext()
  343. {
  344. if (FRevsPos != FObjects.rend())
  345. {
  346. FRevsPos++;
  347. if (FRevsPos != FObjects.rend())
  348. return (*FRevsPos);
  349. }
  350. return (T)NULL;
  351. }
  352. inline bool __fastcall IsREnd()
  353. {
  354. return (FRevsPos == FObjects.rend());
  355. }
  356. inline T __fastcall GetValue(size_t AIndex)
  357. {
  358. if ((FObjects.size() <= index) || (index < 0))
  359. return (T)NULL;
  360. ITer iter = (FObjects.begin() + AIndex);
  361. if (iter != FObjects.end())
  362. return (*iter);
  363. return (T)NULL;
  364. };
  365. inline T operator[](size_t AIndex)
  366. {
  367. return GetValue(AIndex));
  368. };
  369. };
  370. //-----------------------------------------------------------------------
  371. } // namespace
  372. //---------------------------------------------------------------------------
  373. #define TItsAutoLock ITSLib::TITSAutoLock
  374. #define TItsMap ITSLib::TITSStlMap
  375. #define TItsList ITSLib::TITSStlList
  376. //---------------------------------------------------------------------------
  377. /*
  378. *****************************************************************************
  379. * Extern Function Prototypes
  380. *****************************************************************************
  381. */
  382. extern "C"
  383. {
  384. } /* extern "C" */
  385. #endif