cvstd.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CVSTD_HPP
  44. #define OPENCV_CORE_CVSTD_HPP
  45. #ifndef __cplusplus
  46. # error cvstd.hpp header must be compiled as C++
  47. #endif
  48. #include "opencv2/core/cvdef.h"
  49. #include <cstddef>
  50. #include <cstring>
  51. #include <cctype>
  52. #include <string>
  53. // import useful primitives from stl
  54. # include <algorithm>
  55. # include <utility>
  56. # include <cstdlib> //for abs(int)
  57. # include <cmath>
  58. namespace cv
  59. {
  60. static inline uchar abs(uchar a) { return a; }
  61. static inline ushort abs(ushort a) { return a; }
  62. static inline unsigned abs(unsigned a) { return a; }
  63. static inline uint64 abs(uint64 a) { return a; }
  64. using std::min;
  65. using std::max;
  66. using std::abs;
  67. using std::swap;
  68. using std::sqrt;
  69. using std::exp;
  70. using std::pow;
  71. using std::log;
  72. }
  73. #include "cvstd_wrapper.hpp"
  74. namespace cv {
  75. //! @addtogroup core_utils
  76. //! @{
  77. //////////////////////////// memory management functions ////////////////////////////
  78. /** @brief Allocates an aligned memory buffer.
  79. The function allocates the buffer of the specified size and returns it. When the buffer size is 16
  80. bytes or more, the returned buffer is aligned to 16 bytes.
  81. @param bufSize Allocated buffer size.
  82. */
  83. CV_EXPORTS void* fastMalloc(size_t bufSize);
  84. /** @brief Deallocates a memory buffer.
  85. The function deallocates the buffer allocated with fastMalloc . If NULL pointer is passed, the
  86. function does nothing. C version of the function clears the pointer *pptr* to avoid problems with
  87. double memory deallocation.
  88. @param ptr Pointer to the allocated buffer.
  89. */
  90. CV_EXPORTS void fastFree(void* ptr);
  91. /*!
  92. The STL-compliant memory Allocator based on cv::fastMalloc() and cv::fastFree()
  93. */
  94. template<typename _Tp> class Allocator
  95. {
  96. public:
  97. typedef _Tp value_type;
  98. typedef value_type* pointer;
  99. typedef const value_type* const_pointer;
  100. typedef value_type& reference;
  101. typedef const value_type& const_reference;
  102. typedef size_t size_type;
  103. typedef ptrdiff_t difference_type;
  104. template<typename U> class rebind { typedef Allocator<U> other; };
  105. explicit Allocator() {}
  106. ~Allocator() {}
  107. explicit Allocator(Allocator const&) {}
  108. template<typename U>
  109. explicit Allocator(Allocator<U> const&) {}
  110. // address
  111. pointer address(reference r) { return &r; }
  112. const_pointer address(const_reference r) { return &r; }
  113. pointer allocate(size_type count, const void* =0) { return reinterpret_cast<pointer>(fastMalloc(count * sizeof (_Tp))); }
  114. void deallocate(pointer p, size_type) { fastFree(p); }
  115. void construct(pointer p, const _Tp& v) { new(static_cast<void*>(p)) _Tp(v); }
  116. void destroy(pointer p) { p->~_Tp(); }
  117. size_type max_size() const { return cv::max(static_cast<_Tp>(-1)/sizeof(_Tp), 1); }
  118. };
  119. //! @} core_utils
  120. //! @endcond
  121. //! @addtogroup core_basic
  122. //! @{
  123. //////////////////////////////// string class ////////////////////////////////
  124. class CV_EXPORTS FileNode; //for string constructor from FileNode
  125. typedef std::string String;
  126. #ifndef OPENCV_DISABLE_STRING_LOWER_UPPER_CONVERSIONS
  127. //! @cond IGNORED
  128. namespace details {
  129. // std::tolower is int->int
  130. static inline char char_tolower(char ch)
  131. {
  132. return (char)std::tolower((int)ch);
  133. }
  134. // std::toupper is int->int
  135. static inline char char_toupper(char ch)
  136. {
  137. return (char)std::toupper((int)ch);
  138. }
  139. } // namespace details
  140. //! @endcond
  141. static inline std::string toLowerCase(const std::string& str)
  142. {
  143. std::string result(str);
  144. std::transform(result.begin(), result.end(), result.begin(), details::char_tolower);
  145. return result;
  146. }
  147. static inline std::string toUpperCase(const std::string& str)
  148. {
  149. std::string result(str);
  150. std::transform(result.begin(), result.end(), result.begin(), details::char_toupper);
  151. return result;
  152. }
  153. #endif // OPENCV_DISABLE_STRING_LOWER_UPPER_CONVERSIONS
  154. //! @} core_basic
  155. } // cv
  156. #endif //OPENCV_CORE_CVSTD_HPP