The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

705 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /*
  21. This file sets up some handy mathematical typdefs and functions.
  22. */
  23. //==============================================================================
  24. // Definitions for the int8, int16, int32, int64 and pointer_sized_int types.
  25. /** A platform-independent 8-bit signed integer type. */
  26. using int8 = signed char;
  27. /** A platform-independent 8-bit unsigned integer type. */
  28. using uint8 = unsigned char;
  29. /** A platform-independent 16-bit signed integer type. */
  30. using int16 = signed short;
  31. /** A platform-independent 16-bit unsigned integer type. */
  32. using uint16 = unsigned short;
  33. /** A platform-independent 32-bit signed integer type. */
  34. using int32 = signed int;
  35. /** A platform-independent 32-bit unsigned integer type. */
  36. using uint32 = unsigned int;
  37. #if JUCE_MSVC
  38. /** A platform-independent 64-bit integer type. */
  39. using int64 = __int64;
  40. /** A platform-independent 64-bit unsigned integer type. */
  41. using uint64 = unsigned __int64;
  42. #else
  43. /** A platform-independent 64-bit integer type. */
  44. using int64 = long long;
  45. /** A platform-independent 64-bit unsigned integer type. */
  46. using uint64 = unsigned long long;
  47. #endif
  48. #ifndef DOXYGEN
  49. /** A macro for creating 64-bit literals.
  50. Historically, this was needed to support portability with MSVC6, and is kept here
  51. so that old code will still compile, but nowadays every compiler will support the
  52. LL and ULL suffixes, so you should use those in preference to this macro.
  53. */
  54. #define literal64bit(longLiteral) (longLiteral##LL)
  55. #endif
  56. #if JUCE_64BIT
  57. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  58. using pointer_sized_int = int64;
  59. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  60. using pointer_sized_uint = uint64;
  61. #elif JUCE_MSVC
  62. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  63. using pointer_sized_int = _W64 int;
  64. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  65. using pointer_sized_uint = _W64 unsigned int;
  66. #else
  67. /** A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  68. using pointer_sized_int = int;
  69. /** An unsigned integer type that's guaranteed to be large enough to hold a pointer without truncating it. */
  70. using pointer_sized_uint = unsigned int;
  71. #endif
  72. #if JUCE_WINDOWS && ! JUCE_MINGW
  73. using ssize_t = pointer_sized_int;
  74. #endif
  75. //==============================================================================
  76. // Some indispensable min/max functions
  77. /** Returns the larger of two values. */
  78. template <typename Type>
  79. JUCE_CONSTEXPR Type jmax (Type a, Type b) { return a < b ? b : a; }
  80. /** Returns the larger of three values. */
  81. template <typename Type>
  82. JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c) { return a < b ? (b < c ? c : b) : (a < c ? c : a); }
  83. /** Returns the larger of four values. */
  84. template <typename Type>
  85. JUCE_CONSTEXPR Type jmax (Type a, Type b, Type c, Type d) { return jmax (a, jmax (b, c, d)); }
  86. /** Returns the smaller of two values. */
  87. template <typename Type>
  88. JUCE_CONSTEXPR Type jmin (Type a, Type b) { return b < a ? b : a; }
  89. /** Returns the smaller of three values. */
  90. template <typename Type>
  91. JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c) { return b < a ? (c < b ? c : b) : (c < a ? c : a); }
  92. /** Returns the smaller of four values. */
  93. template <typename Type>
  94. JUCE_CONSTEXPR Type jmin (Type a, Type b, Type c, Type d) { return jmin (a, jmin (b, c, d)); }
  95. /** Remaps a normalised value (between 0 and 1) to a target range.
  96. This effectively returns (targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin)).
  97. */
  98. template <typename Type>
  99. JUCE_CONSTEXPR Type jmap (Type value0To1, Type targetRangeMin, Type targetRangeMax)
  100. {
  101. return targetRangeMin + value0To1 * (targetRangeMax - targetRangeMin);
  102. }
  103. /** Remaps a value from a source range to a target range. */
  104. template <typename Type>
  105. Type jmap (Type sourceValue, Type sourceRangeMin, Type sourceRangeMax, Type targetRangeMin, Type targetRangeMax)
  106. {
  107. jassert (sourceRangeMax != sourceRangeMin); // mapping from a range of zero will produce NaN!
  108. return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin);
  109. }
  110. /** Scans an array of values, returning the minimum value that it contains. */
  111. template <typename Type>
  112. Type findMinimum (const Type* data, int numValues)
  113. {
  114. if (numValues <= 0)
  115. return Type (0);
  116. auto result = *data++;
  117. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  118. {
  119. auto v = *data++;
  120. if (v < result)
  121. result = v;
  122. }
  123. return result;
  124. }
  125. /** Scans an array of values, returning the maximum value that it contains. */
  126. template <typename Type>
  127. Type findMaximum (const Type* values, int numValues)
  128. {
  129. if (numValues <= 0)
  130. return Type (0);
  131. auto result = *values++;
  132. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  133. {
  134. auto v = *values++;
  135. if (result < v)
  136. result = v;
  137. }
  138. return result;
  139. }
  140. /** Scans an array of values, returning the minimum and maximum values that it contains. */
  141. template <typename Type>
  142. void findMinAndMax (const Type* values, int numValues, Type& lowest, Type& highest)
  143. {
  144. if (numValues <= 0)
  145. {
  146. lowest = Type (0);
  147. highest = Type (0);
  148. }
  149. else
  150. {
  151. auto mn = *values++;
  152. auto mx = mn;
  153. while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
  154. {
  155. auto v = *values++;
  156. if (mx < v) mx = v;
  157. if (v < mn) mn = v;
  158. }
  159. lowest = mn;
  160. highest = mx;
  161. }
  162. }
  163. //==============================================================================
  164. /** Constrains a value to keep it within a given range.
  165. This will check that the specified value lies between the lower and upper bounds
  166. specified, and if not, will return the nearest value that would be in-range. Effectively,
  167. it's like calling jmax (lowerLimit, jmin (upperLimit, value)).
  168. Note that it expects that lowerLimit <= upperLimit. If this isn't true,
  169. the results will be unpredictable.
  170. @param lowerLimit the minimum value to return
  171. @param upperLimit the maximum value to return
  172. @param valueToConstrain the value to try to return
  173. @returns the closest value to valueToConstrain which lies between lowerLimit
  174. and upperLimit (inclusive)
  175. @see jmin, jmax, jmap
  176. */
  177. template <typename Type>
  178. Type jlimit (Type lowerLimit,
  179. Type upperLimit,
  180. Type valueToConstrain) noexcept
  181. {
  182. jassert (lowerLimit <= upperLimit); // if these are in the wrong order, results are unpredictable..
  183. return valueToConstrain < lowerLimit ? lowerLimit
  184. : (upperLimit < valueToConstrain ? upperLimit
  185. : valueToConstrain);
  186. }
  187. /** Returns true if a value is at least zero, and also below a specified upper limit.
  188. This is basically a quicker way to write:
  189. @code valueToTest >= 0 && valueToTest < upperLimit
  190. @endcode
  191. */
  192. template <typename Type1, typename Type2>
  193. bool isPositiveAndBelow (Type1 valueToTest, Type2 upperLimit) noexcept
  194. {
  195. jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
  196. return Type1() <= valueToTest && valueToTest < static_cast<Type1> (upperLimit);
  197. }
  198. template <typename Type>
  199. bool isPositiveAndBelow (int valueToTest, Type upperLimit) noexcept
  200. {
  201. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  202. return static_cast<unsigned int> (valueToTest) < static_cast<unsigned int> (upperLimit);
  203. }
  204. /** Returns true if a value is at least zero, and also less than or equal to a specified upper limit.
  205. This is basically a quicker way to write:
  206. @code valueToTest >= 0 && valueToTest <= upperLimit
  207. @endcode
  208. */
  209. template <typename Type1, typename Type2>
  210. bool isPositiveAndNotGreaterThan (Type1 valueToTest, Type2 upperLimit) noexcept
  211. {
  212. jassert (Type1() <= static_cast<Type1> (upperLimit)); // makes no sense to call this if the upper limit is itself below zero..
  213. return Type1() <= valueToTest && valueToTest <= static_cast<Type1> (upperLimit);
  214. }
  215. template <typename Type>
  216. bool isPositiveAndNotGreaterThan (int valueToTest, Type upperLimit) noexcept
  217. {
  218. jassert (upperLimit >= 0); // makes no sense to call this if the upper limit is itself below zero..
  219. return static_cast<unsigned int> (valueToTest) <= static_cast<unsigned int> (upperLimit);
  220. }
  221. /** Computes the absolute difference between two values and returns true if it is less than or equal
  222. to a given tolerance, otherwise it returns false.
  223. */
  224. template <typename Type>
  225. bool isWithin (Type a, Type b, Type tolerance) noexcept
  226. {
  227. return std::abs (a - b) <= tolerance;
  228. }
  229. /** Returns true if the two numbers are approximately equal. This is useful for floating-point
  230. and double comparisons.
  231. */
  232. template <typename Type>
  233. bool approximatelyEqual (Type a, Type b) noexcept
  234. {
  235. return std::abs (a - b) <= (std::numeric_limits<Type>::epsilon() * std::max (a, b))
  236. || std::abs (a - b) < std::numeric_limits<Type>::min();
  237. }
  238. //==============================================================================
  239. /** Handy function for avoiding unused variables warning. */
  240. template <typename... Types>
  241. void ignoreUnused (Types&&...) noexcept {}
  242. /** Handy function for getting the number of elements in a simple const C array.
  243. E.g.
  244. @code
  245. static int myArray[] = { 1, 2, 3 };
  246. int numElements = numElementsInArray (myArray) // returns 3
  247. @endcode
  248. */
  249. template <typename Type, int N>
  250. int numElementsInArray (Type (&array)[N])
  251. {
  252. (void) array;
  253. (void) sizeof (0[array]); // This line should cause an error if you pass an object with a user-defined subscript operator
  254. return N;
  255. }
  256. //==============================================================================
  257. // Some useful maths functions that aren't always present with all compilers and build settings.
  258. /** Using juce_hypot is easier than dealing with the different types of hypot function
  259. that are provided by the various platforms and compilers. */
  260. template <typename Type>
  261. Type juce_hypot (Type a, Type b) noexcept
  262. {
  263. #if JUCE_MSVC
  264. return static_cast<Type> (_hypot (a, b));
  265. #else
  266. return static_cast<Type> (hypot (a, b));
  267. #endif
  268. }
  269. #ifndef DOXYGEN
  270. template <>
  271. inline float juce_hypot (float a, float b) noexcept
  272. {
  273. #if JUCE_MSVC
  274. return _hypotf (a, b);
  275. #else
  276. return hypotf (a, b);
  277. #endif
  278. }
  279. #endif
  280. #if JUCE_MSVC && ! defined (DOXYGEN) // The MSVC libraries omit these functions for some reason...
  281. template<typename Type> Type asinh (Type x) { return std::log (x + std::sqrt (x * x + (Type) 1)); }
  282. template<typename Type> Type acosh (Type x) { return std::log (x + std::sqrt (x * x - (Type) 1)); }
  283. template<typename Type> Type atanh (Type x) { return (std::log (x + (Type) 1) - std::log (((Type) 1) - x)) / (Type) 2; }
  284. #endif
  285. //==============================================================================
  286. #if JUCE_HAS_CONSTEXPR
  287. /** Commonly used mathematical constants
  288. @tags{Core}
  289. */
  290. template <typename FloatType>
  291. struct MathConstants
  292. {
  293. /** A predefined value for Pi */
  294. static constexpr FloatType pi = static_cast<FloatType> (3.141592653589793238L);
  295. /** A predefined value for 2 * Pi */
  296. static constexpr FloatType twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
  297. /** A predefined value for Pi / 2 */
  298. static constexpr FloatType halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
  299. /** A predefined value for Euler's number */
  300. static constexpr FloatType euler = static_cast<FloatType> (2.71828182845904523536L);
  301. /** A predefined value for sqrt(2) */
  302. static constexpr FloatType sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
  303. };
  304. #else
  305. /** Commonly used mathematical constants
  306. @tags{Core}
  307. */
  308. template <typename FloatType>
  309. struct MathConstants
  310. {
  311. /** A predefined value for Pi */
  312. static const FloatType pi;
  313. /** A predefined value for 2 * Pi */
  314. static const FloatType twoPi;
  315. /** A predefined value for Pi / 2 */
  316. static const FloatType halfPi;
  317. /** A predefined value for Euler's number */
  318. static const FloatType euler;
  319. /** A predefined value for sqrt(2) */
  320. static const FloatType sqrt2;
  321. };
  322. template <typename FloatType>
  323. const FloatType MathConstants<FloatType>::pi = static_cast<FloatType> (3.141592653589793238L);
  324. template <typename FloatType>
  325. const FloatType MathConstants<FloatType>::twoPi = static_cast<FloatType> (2 * 3.141592653589793238L);
  326. template <typename FloatType>
  327. const FloatType MathConstants<FloatType>::halfPi = static_cast<FloatType> (3.141592653589793238L / 2);
  328. template <typename FloatType>
  329. const FloatType MathConstants<FloatType>::euler = static_cast<FloatType> (2.71828182845904523536L);
  330. template <typename FloatType>
  331. const FloatType MathConstants<FloatType>::sqrt2 = static_cast<FloatType> (1.4142135623730950488L);
  332. #endif
  333. #ifndef DOXYGEN
  334. /** A double-precision constant for pi.
  335. @deprecated This is deprecated in favour of MathConstants<double>::pi.
  336. The reason is that "double_Pi" was a confusing name, and many people misused it,
  337. wrongly thinking it meant 2 * pi !
  338. */
  339. const JUCE_CONSTEXPR double double_Pi = MathConstants<double>::pi;
  340. /** A single-precision constant for pi.
  341. @deprecated This is deprecated in favour of MathConstants<float>::pi.
  342. The reason is that "double_Pi" was a confusing name, and many people misused it,
  343. wrongly thinking it meant 2 * pi !
  344. */
  345. const JUCE_CONSTEXPR float float_Pi = MathConstants<float>::pi;
  346. #endif
  347. /** Converts an angle in degrees to radians. */
  348. template <typename FloatType>
  349. JUCE_CONSTEXPR FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * (MathConstants<FloatType>::pi / FloatType (180)); }
  350. /** Converts an angle in radians to degrees. */
  351. template <typename FloatType>
  352. JUCE_CONSTEXPR FloatType radiansToDegrees (FloatType radians) noexcept { return radians * (FloatType (180) / MathConstants<FloatType>::pi); }
  353. //==============================================================================
  354. /** The isfinite() method seems to vary between platforms, so this is a
  355. platform-independent function for it.
  356. */
  357. template <typename NumericType>
  358. bool juce_isfinite (NumericType) noexcept
  359. {
  360. return true; // Integer types are always finite
  361. }
  362. template <>
  363. inline bool juce_isfinite (float value) noexcept
  364. {
  365. #if JUCE_WINDOWS && ! JUCE_MINGW
  366. return _finite (value) != 0;
  367. #else
  368. return std::isfinite (value);
  369. #endif
  370. }
  371. template <>
  372. inline bool juce_isfinite (double value) noexcept
  373. {
  374. #if JUCE_WINDOWS && ! JUCE_MINGW
  375. return _finite (value) != 0;
  376. #else
  377. return std::isfinite (value);
  378. #endif
  379. }
  380. //==============================================================================
  381. #if JUCE_MSVC
  382. #pragma optimize ("t", off)
  383. #ifndef __INTEL_COMPILER
  384. #pragma float_control (precise, on, push)
  385. #endif
  386. #endif
  387. /** Fast floating-point-to-integer conversion.
  388. This is faster than using the normal c++ cast to convert a float to an int, and
  389. it will round the value to the nearest integer, rather than rounding it down
  390. like the normal cast does.
  391. Note that this routine gets its speed at the expense of some accuracy, and when
  392. rounding values whose floating point component is exactly 0.5, odd numbers and
  393. even numbers will be rounded up or down differently.
  394. */
  395. template <typename FloatType>
  396. int roundToInt (const FloatType value) noexcept
  397. {
  398. #ifdef __INTEL_COMPILER
  399. #pragma float_control (precise, on, push)
  400. #endif
  401. union { int asInt[2]; double asDouble; } n;
  402. n.asDouble = ((double) value) + 6755399441055744.0;
  403. #if JUCE_BIG_ENDIAN
  404. return n.asInt [1];
  405. #else
  406. return n.asInt [0];
  407. #endif
  408. }
  409. inline int roundToInt (int value) noexcept
  410. {
  411. return value;
  412. }
  413. #if JUCE_MSVC
  414. #ifndef __INTEL_COMPILER
  415. #pragma float_control (pop)
  416. #endif
  417. #pragma optimize ("", on) // resets optimisations to the project defaults
  418. #endif
  419. /** Fast floating-point-to-integer conversion.
  420. This is a slightly slower and slightly more accurate version of roundToInt(). It works
  421. fine for values above zero, but negative numbers are rounded the wrong way.
  422. */
  423. inline int roundToIntAccurate (double value) noexcept
  424. {
  425. #ifdef __INTEL_COMPILER
  426. #pragma float_control (pop)
  427. #endif
  428. return roundToInt (value + 1.5e-8);
  429. }
  430. //==============================================================================
  431. /** Truncates a positive floating-point number to an unsigned int.
  432. This is generally faster than static_cast<unsigned int> (std::floor (x))
  433. but it only works for positive numbers small enough to be represented as an
  434. unsigned int.
  435. */
  436. template <typename FloatType>
  437. unsigned int truncatePositiveToUnsignedInt (FloatType value) noexcept
  438. {
  439. jassert (value >= static_cast<FloatType> (0));
  440. jassert (static_cast<FloatType> (value) <= std::numeric_limits<unsigned int>::max());
  441. return static_cast<unsigned int> (value);
  442. }
  443. //==============================================================================
  444. /** Returns true if the specified integer is a power-of-two. */
  445. template <typename IntegerType>
  446. JUCE_CONSTEXPR bool isPowerOfTwo (IntegerType value)
  447. {
  448. return (value & (value - 1)) == 0;
  449. }
  450. /** Returns the smallest power-of-two which is equal to or greater than the given integer. */
  451. inline int nextPowerOfTwo (int n) noexcept
  452. {
  453. --n;
  454. n |= (n >> 1);
  455. n |= (n >> 2);
  456. n |= (n >> 4);
  457. n |= (n >> 8);
  458. n |= (n >> 16);
  459. return n + 1;
  460. }
  461. /** Returns the index of the highest set bit in a (non-zero) number.
  462. So for n=3 this would return 1, for n=7 it returns 2, etc.
  463. An input value of 0 is illegal!
  464. */
  465. int findHighestSetBit (uint32 n) noexcept;
  466. /** Returns the number of bits in a 32-bit integer. */
  467. inline int countNumberOfBits (uint32 n) noexcept
  468. {
  469. n -= ((n >> 1) & 0x55555555);
  470. n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
  471. n = (((n >> 4) + n) & 0x0f0f0f0f);
  472. n += (n >> 8);
  473. n += (n >> 16);
  474. return (int) (n & 0x3f);
  475. }
  476. /** Returns the number of bits in a 64-bit integer. */
  477. inline int countNumberOfBits (uint64 n) noexcept
  478. {
  479. return countNumberOfBits ((uint32) n) + countNumberOfBits ((uint32) (n >> 32));
  480. }
  481. /** Performs a modulo operation, but can cope with the dividend being negative.
  482. The divisor must be greater than zero.
  483. */
  484. template <typename IntegerType>
  485. IntegerType negativeAwareModulo (IntegerType dividend, const IntegerType divisor) noexcept
  486. {
  487. jassert (divisor > 0);
  488. dividend %= divisor;
  489. return (dividend < 0) ? (dividend + divisor) : dividend;
  490. }
  491. /** Returns the square of its argument. */
  492. template <typename NumericType>
  493. inline JUCE_CONSTEXPR NumericType square (NumericType n) noexcept
  494. {
  495. return n * n;
  496. }
  497. //==============================================================================
  498. /** Writes a number of bits into a memory buffer at a given bit index.
  499. The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
  500. so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the value would be written
  501. into bits 2-8 of targetBuffer[1], and the upper 5 bits of value into bits 0-5 of targetBuffer[2].
  502. @see readLittleEndianBitsInBuffer
  503. */
  504. void writeLittleEndianBitsInBuffer (void* targetBuffer, uint32 startBit, uint32 numBits, uint32 value) noexcept;
  505. /** Reads a number of bits from a buffer at a given bit index.
  506. The buffer is treated as a sequence of 8-bit bytes, and the value is encoded in little-endian order,
  507. so for example if startBit = 10, and numBits = 11 then the lower 6 bits of the result would be read
  508. from bits 2-8 of sourceBuffer[1], and the upper 5 bits of the result from bits 0-5 of sourceBuffer[2].
  509. @see writeLittleEndianBitsInBuffer
  510. */
  511. uint32 readLittleEndianBitsInBuffer (const void* sourceBuffer, uint32 startBit, uint32 numBits) noexcept;
  512. //==============================================================================
  513. #if JUCE_INTEL || defined (DOXYGEN)
  514. /** This macro can be applied to a float variable to check whether it contains a denormalised
  515. value, and to normalise it if necessary.
  516. On CPUs that aren't vulnerable to denormalisation problems, this will have no effect.
  517. */
  518. #define JUCE_UNDENORMALISE(x) { (x) += 0.1f; (x) -= 0.1f; }
  519. #else
  520. #define JUCE_UNDENORMALISE(x)
  521. #endif
  522. //==============================================================================
  523. /** This namespace contains a few template classes for helping work out class type variations.
  524. */
  525. namespace TypeHelpers
  526. {
  527. /** The ParameterType struct is used to find the best type to use when passing some kind
  528. of object as a parameter.
  529. Of course, this is only likely to be useful in certain esoteric template situations.
  530. E.g. "myFunction (typename TypeHelpers::ParameterType<int>::type, typename TypeHelpers::ParameterType<MyObject>::type)"
  531. would evaluate to "myfunction (int, const MyObject&)", keeping any primitive types as
  532. pass-by-value, but passing objects as a const reference, to avoid copying.
  533. @tags{Core}
  534. */
  535. template <typename Type> struct ParameterType { using type = const Type&; };
  536. #if ! DOXYGEN
  537. template <typename Type> struct ParameterType <Type&> { using type = Type&; };
  538. template <typename Type> struct ParameterType <Type*> { using type = Type*; };
  539. template <> struct ParameterType <char> { using type = char; };
  540. template <> struct ParameterType <unsigned char> { using type = unsigned char; };
  541. template <> struct ParameterType <short> { using type = short; };
  542. template <> struct ParameterType <unsigned short> { using type = unsigned short; };
  543. template <> struct ParameterType <int> { using type = int; };
  544. template <> struct ParameterType <unsigned int> { using type = unsigned int; };
  545. template <> struct ParameterType <long> { using type = long; };
  546. template <> struct ParameterType <unsigned long> { using type = unsigned long; };
  547. template <> struct ParameterType <int64> { using type = int64; };
  548. template <> struct ParameterType <uint64> { using type = uint64; };
  549. template <> struct ParameterType <bool> { using type = bool; };
  550. template <> struct ParameterType <float> { using type = float; };
  551. template <> struct ParameterType <double> { using type = double; };
  552. #endif
  553. /** These templates are designed to take a type, and if it's a double, they return a double
  554. type; for anything else, they return a float type.
  555. @tags{Core}
  556. */
  557. template <typename Type> struct SmallestFloatType { using type = float; };
  558. #if ! DOXYGEN
  559. template <> struct SmallestFloatType <double> { using type = double; };
  560. #endif
  561. /** These templates are designed to take an integer type, and return an unsigned int
  562. version with the same size.
  563. @tags{Core}
  564. */
  565. template <int bytes> struct UnsignedTypeWithSize {};
  566. #if ! DOXYGEN
  567. template <> struct UnsignedTypeWithSize<1> { using type = uint8; };
  568. template <> struct UnsignedTypeWithSize<2> { using type = uint16; };
  569. template <> struct UnsignedTypeWithSize<4> { using type = uint32; };
  570. template <> struct UnsignedTypeWithSize<8> { using type = uint64; };
  571. #endif
  572. }
  573. //==============================================================================
  574. #if ! DOXYGEN
  575. // These old functions are deprecated: Just use roundToInt instead.
  576. JUCE_DEPRECATED_ATTRIBUTE inline int roundDoubleToInt (double value) noexcept { return roundToInt (value); }
  577. JUCE_DEPRECATED_ATTRIBUTE inline int roundFloatToInt (float value) noexcept { return roundToInt (value); }
  578. // This old function isn't needed - just use std::abs() instead
  579. JUCE_DEPRECATED_ATTRIBUTE inline int64 abs64 (int64 n) noexcept { return std::abs (n); }
  580. #endif
  581. } // namespace juce