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.

496 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/time/comparison_functions.h
  3. *
  4. * Purpose: Comparison functions for Windows time structures.
  5. *
  6. * Created: 21st November 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/time/comparison_functions.h
  40. *
  41. * \brief [C, C++] Comparison functions for Windows time types
  42. * (\ref group__library__time "Time" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_TIME_H_COMPARISON_FUNCTIONS
  45. #define WINSTL_INCL_WINSTL_TIME_H_COMPARISON_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_TIME_H_COMPARISON_FUNCTIONS_MAJOR 4
  48. # define WINSTL_VER_WINSTL_TIME_H_COMPARISON_FUNCTIONS_MINOR 0
  49. # define WINSTL_VER_WINSTL_TIME_H_COMPARISON_FUNCTIONS_REVISION 5
  50. # define WINSTL_VER_WINSTL_TIME_H_COMPARISON_FUNCTIONS_EDIT 47
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #if !defined(_WINSTL_NO_NAMESPACE) && \
  62. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  63. # if defined(_STLSOFT_NO_NAMESPACE)
  64. /* There is no stlsoft namespace, so must define ::winstl */
  65. namespace winstl
  66. {
  67. # else
  68. /* Define stlsoft::winstl_project */
  69. namespace stlsoft
  70. {
  71. namespace winstl_project
  72. {
  73. # endif /* _STLSOFT_NO_NAMESPACE */
  74. #endif /* !_WINSTL_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * C functions
  77. */
  78. /**
  79. *
  80. * \ingroup group__library__time
  81. */
  82. STLSOFT_INLINE ws_sint_t winstl__compare_FILETIMEs(FILETIME const* lhs, FILETIME const* rhs)
  83. {
  84. WINSTL_ASSERT(NULL != lhs);
  85. WINSTL_ASSERT(NULL != rhs);
  86. if(lhs->dwHighDateTime < rhs->dwHighDateTime)
  87. {
  88. return -1;
  89. }
  90. else if(rhs->dwHighDateTime < lhs->dwHighDateTime)
  91. {
  92. return +1;
  93. }
  94. else
  95. {
  96. if(lhs->dwLowDateTime < rhs->dwLowDateTime)
  97. {
  98. return -1;
  99. }
  100. else if(rhs->dwLowDateTime < lhs->dwLowDateTime)
  101. {
  102. return +1;
  103. }
  104. else
  105. {
  106. return 0;
  107. }
  108. }
  109. }
  110. /**
  111. *
  112. * \ingroup group__library__time
  113. */
  114. STLSOFT_INLINE ws_sint_t winstl__compare_FILETIME_with_SYSTEMTIME(FILETIME const* lhs, SYSTEMTIME const* rhs)
  115. {
  116. FILETIME ft2;
  117. WINSTL_ASSERT(NULL != lhs);
  118. WINSTL_ASSERT(NULL != rhs);
  119. if(!STLSOFT_NS_GLOBAL(SystemTimeToFileTime)(rhs, &ft2))
  120. {
  121. return -1;
  122. }
  123. return winstl__compare_FILETIMEs(lhs, &ft2);
  124. }
  125. /**
  126. *
  127. * \ingroup group__library__time
  128. */
  129. STLSOFT_INLINE ws_sint_t winstl__compare_SYSTEMTIME_with_FILETIME(SYSTEMTIME const* lhs, FILETIME const* rhs)
  130. {
  131. FILETIME ft1;
  132. WINSTL_ASSERT(NULL != lhs);
  133. WINSTL_ASSERT(NULL != rhs);
  134. if(!STLSOFT_NS_GLOBAL(SystemTimeToFileTime)(lhs, &ft1))
  135. {
  136. return +1;
  137. }
  138. return winstl__compare_FILETIMEs(&ft1, rhs);
  139. }
  140. /**
  141. *
  142. * \ingroup group__library__time
  143. */
  144. STLSOFT_INLINE ws_sint_t winstl__compare_SYSTEMTIMEs(SYSTEMTIME const* lhs, SYSTEMTIME const* rhs)
  145. {
  146. FILETIME ft1;
  147. FILETIME ft2;
  148. WINSTL_ASSERT(NULL != lhs);
  149. WINSTL_ASSERT(NULL != rhs);
  150. if(!STLSOFT_NS_GLOBAL(SystemTimeToFileTime)(lhs, &ft1))
  151. {
  152. return +1;
  153. }
  154. if(!STLSOFT_NS_GLOBAL(SystemTimeToFileTime)(rhs, &ft2))
  155. {
  156. return -1;
  157. }
  158. return winstl__compare_FILETIMEs(&ft1, &ft2);
  159. }
  160. /* /////////////////////////////////////////////////////////////////////////
  161. * Namespace
  162. */
  163. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  164. namespace winstl
  165. {
  166. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  167. /* /////////////////////////////////////////////////////////////////////////
  168. * C++ functions
  169. */
  170. #ifdef __cplusplus
  171. /**
  172. *
  173. * \ingroup group__library__time
  174. */
  175. inline ws_sint_t compare(FILETIME const& lhs, FILETIME const& rhs)
  176. {
  177. return winstl__compare_FILETIMEs(&lhs, &rhs);
  178. }
  179. /**
  180. *
  181. * \ingroup group__library__time
  182. */
  183. inline ws_sint_t compare(FILETIME const& lhs, SYSTEMTIME const& rhs)
  184. {
  185. return winstl__compare_FILETIME_with_SYSTEMTIME(&lhs, &rhs);
  186. }
  187. /**
  188. *
  189. * \ingroup group__library__time
  190. */
  191. inline ws_sint_t compare(SYSTEMTIME const& lhs, FILETIME const& rhs)
  192. {
  193. return winstl__compare_SYSTEMTIME_with_FILETIME(&lhs, &rhs);
  194. }
  195. /**
  196. *
  197. * \ingroup group__library__time
  198. */
  199. inline ws_sint_t compare(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  200. {
  201. return winstl__compare_SYSTEMTIMEs(&lhs, &rhs);
  202. }
  203. /* /////////////////////////////////////////////////////////////////////////
  204. * Operators
  205. */
  206. /* operator == */
  207. /**
  208. *
  209. * \ingroup group__library__time
  210. */
  211. inline ws_bool_t operator ==(FILETIME const& lhs, FILETIME const& rhs)
  212. {
  213. return 0 == compare(lhs, rhs);
  214. }
  215. /**
  216. *
  217. * \ingroup group__library__time
  218. */
  219. inline ws_bool_t operator ==(FILETIME const& lhs, SYSTEMTIME const& rhs)
  220. {
  221. return 0 == compare(lhs, rhs);
  222. }
  223. /**
  224. *
  225. * \ingroup group__library__time
  226. */
  227. inline ws_bool_t operator ==(SYSTEMTIME const& lhs, FILETIME const& rhs)
  228. {
  229. return 0 == compare(lhs, rhs);
  230. }
  231. /**
  232. *
  233. * \ingroup group__library__time
  234. */
  235. inline ws_bool_t operator ==(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  236. {
  237. return 0 == compare(lhs, rhs);
  238. }
  239. /* operator != */
  240. /**
  241. *
  242. * \ingroup group__library__time
  243. */
  244. inline ws_bool_t operator !=(FILETIME const& lhs, FILETIME const& rhs)
  245. {
  246. return 0 != compare(lhs, rhs);
  247. }
  248. /**
  249. *
  250. * \ingroup group__library__time
  251. */
  252. inline ws_bool_t operator !=(FILETIME const& lhs, SYSTEMTIME const& rhs)
  253. {
  254. return 0 != compare(lhs, rhs);
  255. }
  256. /**
  257. *
  258. * \ingroup group__library__time
  259. */
  260. inline ws_bool_t operator !=(SYSTEMTIME const& lhs, FILETIME const& rhs)
  261. {
  262. return 0 != compare(lhs, rhs);
  263. }
  264. /**
  265. *
  266. * \ingroup group__library__time
  267. */
  268. inline ws_bool_t operator !=(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  269. {
  270. return 0 != compare(lhs, rhs);
  271. }
  272. /* operator < */
  273. /**
  274. *
  275. * \ingroup group__library__time
  276. */
  277. inline ws_bool_t operator <(FILETIME const& lhs, FILETIME const& rhs)
  278. {
  279. return 0 > compare(lhs, rhs);
  280. }
  281. /**
  282. *
  283. * \ingroup group__library__time
  284. */
  285. inline ws_bool_t operator <(FILETIME const& lhs, SYSTEMTIME const& rhs)
  286. {
  287. return 0 > compare(lhs, rhs);
  288. }
  289. /**
  290. *
  291. * \ingroup group__library__time
  292. */
  293. inline ws_bool_t operator <(SYSTEMTIME const& lhs, FILETIME const& rhs)
  294. {
  295. return 0 > compare(lhs, rhs);
  296. }
  297. /**
  298. *
  299. * \ingroup group__library__time
  300. */
  301. inline ws_bool_t operator <(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  302. {
  303. return 0 > compare(lhs, rhs);
  304. }
  305. /* operator > */
  306. /**
  307. *
  308. * \ingroup group__library__time
  309. */
  310. inline ws_bool_t operator >(FILETIME const& lhs, FILETIME const& rhs)
  311. {
  312. return 0 < compare(lhs, rhs);
  313. }
  314. /**
  315. *
  316. * \ingroup group__library__time
  317. */
  318. inline ws_bool_t operator >(FILETIME const& lhs, SYSTEMTIME const& rhs)
  319. {
  320. return 0 < compare(lhs, rhs);
  321. }
  322. /**
  323. *
  324. * \ingroup group__library__time
  325. */
  326. inline ws_bool_t operator >(SYSTEMTIME const& lhs, FILETIME const& rhs)
  327. {
  328. return 0 < compare(lhs, rhs);
  329. }
  330. /**
  331. *
  332. * \ingroup group__library__time
  333. */
  334. inline ws_bool_t operator >(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  335. {
  336. return 0 < compare(lhs, rhs);
  337. }
  338. /* operator <= */
  339. /**
  340. *
  341. * \ingroup group__library__time
  342. */
  343. inline ws_bool_t operator <=(FILETIME const& lhs, FILETIME const& rhs)
  344. {
  345. return 0 >= compare(lhs, rhs);
  346. }
  347. /**
  348. *
  349. * \ingroup group__library__time
  350. */
  351. inline ws_bool_t operator <=(FILETIME const& lhs, SYSTEMTIME const& rhs)
  352. {
  353. return 0 >= compare(lhs, rhs);
  354. }
  355. /**
  356. *
  357. * \ingroup group__library__time
  358. */
  359. inline ws_bool_t operator <=(SYSTEMTIME const& lhs, FILETIME const& rhs)
  360. {
  361. return 0 >= compare(lhs, rhs);
  362. }
  363. /**
  364. *
  365. * \ingroup group__library__time
  366. */
  367. inline ws_bool_t operator <=(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  368. {
  369. return 0 >= compare(lhs, rhs);
  370. }
  371. /* operator >= */
  372. /**
  373. *
  374. * \ingroup group__library__time
  375. */
  376. inline ws_bool_t operator >=(FILETIME const& lhs, FILETIME const& rhs)
  377. {
  378. return 0 <= compare(lhs, rhs);
  379. }
  380. /**
  381. *
  382. * \ingroup group__library__time
  383. */
  384. inline ws_bool_t operator >=(FILETIME const& lhs, SYSTEMTIME const& rhs)
  385. {
  386. return 0 <= compare(lhs, rhs);
  387. }
  388. /**
  389. *
  390. * \ingroup group__library__time
  391. */
  392. inline ws_bool_t operator >=(SYSTEMTIME const& lhs, FILETIME const& rhs)
  393. {
  394. return 0 <= compare(lhs, rhs);
  395. }
  396. /**
  397. *
  398. * \ingroup group__library__time
  399. */
  400. inline ws_bool_t operator >=(SYSTEMTIME const& lhs, SYSTEMTIME const& rhs)
  401. {
  402. return 0 <= compare(lhs, rhs);
  403. }
  404. #endif /* __cplusplus */
  405. /* /////////////////////////////////////////////////////////////////////////
  406. * Unit-testing
  407. */
  408. #ifdef STLSOFT_UNITTEST
  409. # include "./unittest/comparison_functions_unittest_.h"
  410. #endif /* STLSOFT_UNITTEST */
  411. /* ////////////////////////////////////////////////////////////////////// */
  412. #ifndef _WINSTL_NO_NAMESPACE
  413. # if defined(_STLSOFT_NO_NAMESPACE) || \
  414. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  415. } /* namespace winstl */
  416. # else
  417. } /* namespace winstl_project */
  418. } /* namespace stlsoft */
  419. # endif /* _STLSOFT_NO_NAMESPACE */
  420. #endif /* !_WINSTL_NO_NAMESPACE */
  421. /* ////////////////////////////////////////////////////////////////////// */
  422. #endif /* !WINSTL_INCL_WINSTL_TIME_H_COMPARISON_FUNCTIONS */
  423. /* ///////////////////////////// end of file //////////////////////////// */