Add initial bits for Qt6 support
[carla.git] / source / modules / water / common.hpp
blob64f6e8f9fd5e806f93d8605e89e7e41a2b1c7caa
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>
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.
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.
16 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
19 #pragma once
21 #include "maths/MathsFunctions.h"
22 #include "misc/Result.h"
24 #ifdef CARLA_OS_MAC
25 # include "text/String.h"
26 # import <Foundation/NSAutoreleasePool.h>
27 # import <Foundation/NSString.h>
28 #endif
30 #include <cerrno>
32 #ifndef CARLA_PROPER_CPP11_SUPPORT
33 namespace std {
34 using strerror;
36 #endif
38 //==============================================================================
39 namespace water
42 #ifdef CARLA_OS_WIN
43 static inline
44 Result getResultForLastError()
46 CHAR messageBuffer [256] = { 0 };
48 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
49 nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
50 messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
52 return Result::fail (messageBuffer);
55 static inline
56 int64 water_fileSetPosition (void* handle, int64 pos)
58 LARGE_INTEGER li;
59 li.QuadPart = pos;
60 li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
61 return li.QuadPart;
64 HINSTANCE getCurrentModuleInstanceHandle() noexcept;
65 #else
66 static inline
67 Result getResultForErrno()
69 return Result::fail (std::strerror (errno));
72 static inline
73 int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
75 static inline
76 void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
78 static inline
79 int64 water_fileSetPosition (void* handle, int64 pos)
81 if (handle != nullptr && lseek (getFD (handle), pos, SEEK_SET) == pos)
82 return pos;
84 return -1;
86 #endif
88 #ifdef CARLA_OS_MAC
89 static inline
90 String nsStringToWater (NSString* s)
92 return CharPointer_UTF8 ([s UTF8String]);
95 static inline
96 NSString* waterStringToNS (const String& s)
98 return [NSString stringWithUTF8String: s.toUTF8()];
101 class AutoNSAutoreleasePool {
102 public:
103 AutoNSAutoreleasePool() : pool([NSAutoreleasePool new]) {}
104 ~AutoNSAutoreleasePool() { [pool drain]; }
106 private:
107 NSAutoreleasePool* const pool;
109 #endif