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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Cross-platform C++ library for Carla, based on Juce v4
  3. * Copyright (C) 2015 ROLI Ltd.
  4. * Copyright (C) 2017 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. #include "maths/MathsFunctions.h"
  19. #include "misc/Result.h"
  20. #ifdef CARLA_OS_MAC
  21. # include "text/String.h"
  22. # import <Foundation/NSAutoreleasePool.h>
  23. # import <Foundation/NSString.h>
  24. #endif
  25. #include <cerrno>
  26. //==============================================================================
  27. namespace water
  28. {
  29. #ifdef CARLA_OS_WIN
  30. static inline
  31. Result getResultForLastError()
  32. {
  33. TCHAR messageBuffer [256] = { 0 };
  34. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  35. nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  36. messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
  37. return Result::fail (String (messageBuffer));
  38. }
  39. static inline
  40. int64 water_fileSetPosition (void* handle, int64 pos)
  41. {
  42. LARGE_INTEGER li;
  43. li.QuadPart = pos;
  44. li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
  45. return li.QuadPart;
  46. }
  47. HINSTANCE getCurrentModuleInstanceHandle() noexcept;
  48. #else
  49. static inline
  50. Result getResultForErrno()
  51. {
  52. #ifdef CARLA_PROPER_CPP11_SUPPORT
  53. return Result::fail (String (std::strerror (errno)));
  54. #else
  55. return Result::fail (String (strerror (errno)));
  56. #endif
  57. }
  58. static inline
  59. int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
  60. static inline
  61. void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
  62. static inline
  63. int64 water_fileSetPosition (void* handle, int64 pos)
  64. {
  65. if (handle != nullptr && lseek (getFD (handle), pos, SEEK_SET) == pos)
  66. return pos;
  67. return -1;
  68. }
  69. #endif
  70. #ifdef CARLA_OS_MAC
  71. static inline
  72. String nsStringToWater (NSString* s)
  73. {
  74. return CharPointer_UTF8 ([s UTF8String]);
  75. }
  76. static inline
  77. NSString* waterStringToNS (const String& s)
  78. {
  79. return [NSString stringWithUTF8String: s.toUTF8()];
  80. }
  81. class AutoNSAutoreleasePool {
  82. public:
  83. AutoNSAutoreleasePool() : pool([NSAutoreleasePool new]) {}
  84. ~AutoNSAutoreleasePool() { [pool drain]; }
  85. private:
  86. NSAutoreleasePool* const pool;
  87. };
  88. #endif
  89. }