Audio plugin host https://kx.studio/carla
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.

common.hpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Cross-platform C++ library for Carla, based on Juce v4
  3. * Copyright (C) 2015 ROLI Ltd.
  4. * Copyright (C) 2017-2023 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #pragma once
  19. #include "maths/MathsFunctions.h"
  20. #include "misc/Result.h"
  21. #ifdef CARLA_OS_MAC
  22. # include "text/String.h"
  23. # import <Foundation/NSAutoreleasePool.h>
  24. # import <Foundation/NSString.h>
  25. #endif
  26. #include <cerrno>
  27. #ifndef CARLA_PROPER_CPP11_SUPPORT
  28. namespace std {
  29. using strerror;
  30. }
  31. #endif
  32. //==============================================================================
  33. namespace water
  34. {
  35. #ifdef CARLA_OS_WIN
  36. static inline
  37. Result getResultForLastError()
  38. {
  39. CHAR messageBuffer [256] = { 0 };
  40. FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  41. nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  42. messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
  43. return Result::fail (messageBuffer);
  44. }
  45. static inline
  46. int64 water_fileSetPosition (void* handle, int64 pos)
  47. {
  48. LARGE_INTEGER li;
  49. li.QuadPart = pos;
  50. li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
  51. return li.QuadPart;
  52. }
  53. HINSTANCE getCurrentModuleInstanceHandle() noexcept;
  54. #else
  55. static inline
  56. Result getResultForErrno()
  57. {
  58. return Result::fail (std::strerror (errno));
  59. }
  60. static inline
  61. int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
  62. static inline
  63. void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
  64. static inline
  65. int64 water_fileSetPosition (void* handle, int64 pos)
  66. {
  67. if (handle != nullptr && lseek (getFD (handle), pos, SEEK_SET) == pos)
  68. return pos;
  69. return -1;
  70. }
  71. #endif
  72. #ifdef CARLA_OS_MAC
  73. static inline
  74. String nsStringToWater (NSString* s)
  75. {
  76. return CharPointer_UTF8 ([s UTF8String]);
  77. }
  78. static inline
  79. NSString* waterStringToNS (const String& s)
  80. {
  81. return [NSString stringWithUTF8String: s.toUTF8()];
  82. }
  83. class AutoNSAutoreleasePool {
  84. public:
  85. AutoNSAutoreleasePool() : pool([NSAutoreleasePool new]) {}
  86. ~AutoNSAutoreleasePool() { [pool drain]; }
  87. private:
  88. NSAutoreleasePool* const pool;
  89. };
  90. #endif
  91. }