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.

281 lines
8.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/util/DECIMAL_functions.h
  3. *
  4. * Purpose: DECIMAL helper functions.
  5. *
  6. * Created: 23rd August 2008
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2008-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 comstl/util/DECIMAL_functions.h
  40. *
  41. * \brief [C++ only; requires COM] DECIMAL helper functions
  42. * (\ref group__library__utility__com "COM Utility" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_UTIL_H_DECIMAL_FUNCTIONS
  45. #define COMSTL_INCL_COMSTL_UTIL_H_DECIMAL_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_UTIL_H_DECIMAL_FUNCTIONS_MAJOR 1
  48. # define COMSTL_VER_COMSTL_UTIL_H_DECIMAL_FUNCTIONS_MINOR 0
  49. # define COMSTL_VER_COMSTL_UTIL_H_DECIMAL_FUNCTIONS_REVISION 2
  50. # define COMSTL_VER_COMSTL_UTIL_H_DECIMAL_FUNCTIONS_EDIT 3
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  56. # include <comstl/comstl.h>
  57. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  58. #ifndef STLSOFT_INCL_H_OAIDL
  59. # define STLSOFT_INCL_H_OAIDL
  60. # include <oaidl.h>
  61. #endif /* !STLSOFT_INCL_H_OAIDL */
  62. #ifndef STLSOFT_INCL_H_WTYPES
  63. # define STLSOFT_INCL_H_WTYPES
  64. # include <wtypes.h>
  65. #endif /* !STLSOFT_INCL_H_WTYPES */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #if !defined(_COMSTL_NO_NAMESPACE) && \
  70. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  71. # if defined(_STLSOFT_NO_NAMESPACE)
  72. /* There is no stlsoft namespace, so must define ::comstl */
  73. namespace comstl
  74. {
  75. # else
  76. /* Define stlsoft::comstl_project */
  77. namespace stlsoft
  78. {
  79. namespace comstl_project
  80. {
  81. # endif /* _STLSOFT_NO_NAMESPACE */
  82. #endif /* !_COMSTL_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * C functions
  85. */
  86. /** \brief [C only] Compares two DECIMAL structures
  87. *
  88. * \ingroup group__library__utility__com
  89. *
  90. * \param lhs Pointer to the left-hand instances to compare
  91. * \param lhs Pointer to the right-hand instances to compare
  92. *
  93. * \pre \c lhs must not be NULL.
  94. * \pre \c rhs must not be NULL.
  95. */
  96. STLSOFT_INLINE int comstl__DECIMAL_compare(DECIMAL const* lhs, DECIMAL const* rhs)
  97. {
  98. COMSTL_MESSAGE_ASSERT("Cannot pass NULL pointer(s) to DECIMAL_compare()", (NULL != lhs && NULL != rhs));
  99. COMSTL_MESSAGE_ASSERT("invalid sign value in lhs", (0 == lhs->sign || DECIMAL_NEG == lhs->sign));
  100. COMSTL_MESSAGE_ASSERT("invalid sign value in rhs", (0 == rhs->sign || DECIMAL_NEG == rhs->sign));
  101. if(lhs->sign != rhs->sign)
  102. {
  103. /* signs are different, so we need only check for both being 0,
  104. * otherwise just return indication of which is -ve
  105. */
  106. if( 0 == lhs->Hi32 &&
  107. 0 == rhs->Hi32 &&
  108. 0 == lhs->Mid32 &&
  109. 0 == rhs->Mid32 &&
  110. 0 == lhs->Lo32 &&
  111. 0 == rhs->Lo32)
  112. {
  113. return 0;
  114. }
  115. else
  116. {
  117. if(0 != lhs->sign)
  118. {
  119. /* lhs is negative, and rhs is not, so lhs is less */
  120. return -1;
  121. }
  122. else
  123. {
  124. return +1;
  125. }
  126. }
  127. }
  128. else
  129. {
  130. /* next see if scale is the same */
  131. if(lhs->scale != rhs->scale)
  132. {
  133. /* This is too-hard, so we convert to VARIANTS and do the check
  134. * that way
  135. */
  136. VARIANT vdecL;
  137. VARIANT vdecR;
  138. VARIANT vdblL;
  139. VARIANT vdblR;
  140. VariantClear(&vdecL);
  141. VariantClear(&vdecR);
  142. VariantClear(&vdblL);
  143. VariantClear(&vdblR);
  144. COMSTL_ACCESS_VARIANT_vt_BYREF(vdecL) = VT_DECIMAL;
  145. COMSTL_ACCESS_VARIANT_decVal_BYREF(vdecL) = *lhs;
  146. COMSTL_ACCESS_VARIANT_vt_BYREF(vdecR) = VT_DECIMAL;
  147. COMSTL_ACCESS_VARIANT_decVal_BYREF(vdecR) = *rhs;
  148. VariantChangeType(&vdblL, &vdecL, 0, VT_R8);
  149. VariantChangeType(&vdblR, &vdecR, 0, VT_R8);
  150. if(COMSTL_ACCESS_VARIANT_MEM_BYREF(vdblL, dblVal) == COMSTL_ACCESS_VARIANT_MEM_BYREF(vdblR, dblVal))
  151. {
  152. return 0;
  153. }
  154. else if(COMSTL_ACCESS_VARIANT_MEM_BYREF(vdblL, dblVal) < COMSTL_ACCESS_VARIANT_MEM_BYREF(vdblR, dblVal))
  155. {
  156. return -1;
  157. }
  158. else
  159. {
  160. return +1;
  161. }
  162. }
  163. else
  164. {
  165. /* scale is the same, so compare Hi32 first */
  166. if(lhs->Hi32 != rhs->Hi32)
  167. {
  168. return (lhs->Hi32 < rhs->Hi32) ? -1 : +1;
  169. }
  170. else
  171. {
  172. if(lhs->Mid32 != rhs->Mid32)
  173. {
  174. return (lhs->Mid32 < rhs->Mid32) ? -1 : +1;
  175. }
  176. else
  177. {
  178. if(lhs->Lo32 != rhs->Lo32)
  179. {
  180. return (lhs->Lo32 < rhs->Lo32) ? -1 : +1;
  181. }
  182. else
  183. {
  184. return 0;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /* /////////////////////////////////////////////////////////////////////////
  192. * Namespace
  193. */
  194. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  195. namespace comstl
  196. {
  197. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  198. /* /////////////////////////////////////////////////////////////////////////
  199. * C++ functions
  200. */
  201. #ifdef __cplusplus
  202. /** \brief [C++ only] Compares two DECIMAL structures
  203. *
  204. * \ingroup group__library__utility__com
  205. *
  206. * \param lhs Pointer to the left-hand instances to compare
  207. * \param lhs Pointer to the right-hand instances to compare
  208. *
  209. * \pre \c lhs must not be NULL.
  210. * \pre \c rhs must not be NULL.
  211. */
  212. inline int DECIMAL_compare(DECIMAL const* lhs, DECIMAL const* rhs)
  213. {
  214. return comstl__DECIMAL_compare(lhs, rhs);
  215. }
  216. /** \brief [C++ only] Compares two DECIMAL structures
  217. *
  218. * \ingroup group__library__utility__com
  219. *
  220. * \param lhs Reference to the left-hand instances to compare
  221. * \param lhs Reference to the right-hand instances to compare
  222. */
  223. inline int DECIMAL_compare(DECIMAL const& lhs, DECIMAL const& rhs)
  224. {
  225. return comstl__DECIMAL_compare(&lhs, &rhs);
  226. }
  227. #endif /* __cplusplus */
  228. /* /////////////////////////////////////////////////////////////////////////
  229. * Unit-testing
  230. */
  231. #ifdef STLSOFT_UNITTEST
  232. # include "./unittest/DECIMAL_functions_unittest_.h"
  233. #endif /* STLSOFT_UNITTEST */
  234. /* ////////////////////////////////////////////////////////////////////// */
  235. #ifndef _COMSTL_NO_NAMESPACE
  236. # if defined(_STLSOFT_NO_NAMESPACE) || \
  237. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  238. } /* namespace comstl */
  239. # else
  240. } /* namespace comstl_project */
  241. } /* namespace stlsoft */
  242. # endif /* _STLSOFT_NO_NAMESPACE */
  243. #endif /* !_COMSTL_NO_NAMESPACE */
  244. /* ////////////////////////////////////////////////////////////////////// */
  245. #endif /* !COMSTL_INCL_COMSTL_UTIL_H_DECIMAL_FUNCTIONS */
  246. /* ///////////////////////////// end of file //////////////////////////// */