From c9d7b3e6f0ae68b9a3fc6f935172fca28ce80fb9 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 26 Apr 2015 17:23:07 +0200 Subject: [PATCH] Finish sync with carla classes, remove d_ prefixes --- distrho/DistrhoUtils.hpp | 18 ++ distrho/extra/d_leakdetector.hpp | 2 +- distrho/extra/d_mutex.hpp | 2 +- distrho/extra/d_scopedpointer.hpp | 25 +-- distrho/extra/d_sleep.hpp | 8 +- distrho/extra/d_string.hpp | 282 ++++++++++++++++++++---------- distrho/extra/d_thread.hpp | 17 +- 7 files changed, 225 insertions(+), 129 deletions(-) diff --git a/distrho/DistrhoUtils.hpp b/distrho/DistrhoUtils.hpp index b07282cb..c9222a1d 100644 --- a/distrho/DistrhoUtils.hpp +++ b/distrho/DistrhoUtils.hpp @@ -201,6 +201,24 @@ bool d_isNotZero(const T& value) return std::abs(value) >= std::numeric_limits::epsilon(); } +/* + * Get next power of 2. + */ +static inline +uint32_t d_nextPowerOf2(uint32_t size) noexcept +{ + DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0); + + // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + --size; + size |= size >> 1; + size |= size >> 2; + size |= size >> 4; + size |= size >> 8; + size |= size >> 16; + return ++size; +} + // ----------------------------------------------------------------------- #endif // DISTRHO_UTILS_HPP_INCLUDED diff --git a/distrho/extra/d_leakdetector.hpp b/distrho/extra/d_leakdetector.hpp index 2840c2db..08974e6a 100644 --- a/distrho/extra/d_leakdetector.hpp +++ b/distrho/extra/d_leakdetector.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2014 Filipe Coelho + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this diff --git a/distrho/extra/d_mutex.hpp b/distrho/extra/d_mutex.hpp index ccb48fa9..8badcc33 100644 --- a/distrho/extra/d_mutex.hpp +++ b/distrho/extra/d_mutex.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2014 Filipe Coelho + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this diff --git a/distrho/extra/d_scopedpointer.hpp b/distrho/extra/d_scopedpointer.hpp index bde4e7be..12e6ba0f 100644 --- a/distrho/extra/d_scopedpointer.hpp +++ b/distrho/extra/d_scopedpointer.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2014 Filipe Coelho + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -27,23 +27,6 @@ START_NAMESPACE_DISTRHO // The following code was based from juce-core ScopedPointer class // Copyright (C) 2013 Raw Material Software Ltd. -/** - Used by container classes as an indirect way to delete an object of a - particular type. - - The generic implementation of this class simply calls 'delete', but you can - create a specialised version of it for a particular class if you need to - delete that type of object in a more appropriate way. -*/ -template -struct ContainerDeletePolicy -{ - static void destroy(ObjectType* const object) - { - delete object; - } -}; - //============================================================================== /** This class holds a pointer which is automatically deleted when this object goes @@ -107,7 +90,7 @@ public: */ ~ScopedPointer() { - ContainerDeletePolicy::destroy(object); + delete object; } /** Changes this ScopedPointer to point to a new object. @@ -130,7 +113,7 @@ public: ObjectType* const oldObject = object; object = objectToTransferFrom.object; objectToTransferFrom.object = nullptr; - ContainerDeletePolicy::destroy(oldObject); + delete oldObject; } return *this; @@ -149,7 +132,7 @@ public: { ObjectType* const oldObject = object; object = newObjectToTakePossessionOf; - ContainerDeletePolicy::destroy(oldObject); + delete oldObject; } return *this; diff --git a/distrho/extra/d_sleep.hpp b/distrho/extra/d_sleep.hpp index c9c2c593..4953a12b 100644 --- a/distrho/extra/d_sleep.hpp +++ b/distrho/extra/d_sleep.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2014 Filipe Coelho + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -29,6 +29,9 @@ // ----------------------------------------------------------------------- // d_*sleep +/* + * Sleep for 'secs' seconds. + */ static inline void d_sleep(const uint secs) noexcept { @@ -43,6 +46,9 @@ void d_sleep(const uint secs) noexcept } DISTRHO_SAFE_EXCEPTION("d_sleep"); } +/* + * Sleep for 'msecs' milliseconds. + */ static inline void d_msleep(const uint msecs) noexcept { diff --git a/distrho/extra/d_string.hpp b/distrho/extra/d_string.hpp index 0420c420..b2a35561 100644 --- a/distrho/extra/d_string.hpp +++ b/distrho/extra/d_string.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2014 Filipe Coelho + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -22,9 +22,9 @@ START_NAMESPACE_DISTRHO // ----------------------------------------------------------------------- -// d_string class +// String class -class d_string +class String { public: // ------------------------------------------------------------------- @@ -33,143 +33,153 @@ public: /* * Empty string. */ - explicit d_string() noexcept - { - _init(); - } + explicit String() noexcept + : fBuffer(_null()), + fBufferLen(0) {} /* * Simple character. */ - explicit d_string(const char c) noexcept + explicit String(const char c) noexcept + : fBuffer(_null()), + fBufferLen(0) { char ch[2]; ch[0] = c; ch[1] = '\0'; - _init(); _dup(ch); } /* * Simple char string. */ - explicit d_string(char* const strBuf) noexcept + explicit String(char* const strBuf) noexcept + : fBuffer(_null()), + fBufferLen(0) { - _init(); _dup(strBuf); } /* * Simple const char string. */ - explicit d_string(const char* const strBuf) noexcept + explicit String(const char* const strBuf) noexcept + : fBuffer(_null()), + fBufferLen(0) { - _init(); _dup(strBuf); } /* * Integer. */ - explicit d_string(const int value) noexcept + explicit String(const int value) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%d", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Unsigned integer, possibly in hexadecimal. */ - explicit d_string(const unsigned int value, const bool hexadecimal = false) noexcept + explicit String(const unsigned int value, const bool hexadecimal = false) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, hexadecimal ? "0x%x" : "%u", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Long integer. */ - explicit d_string(const long value) noexcept + explicit String(const long value) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%ld", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Long unsigned integer, possibly hexadecimal. */ - explicit d_string(const unsigned long value, const bool hexadecimal = false) noexcept + explicit String(const unsigned long value, const bool hexadecimal = false) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, hexadecimal ? "0x%lx" : "%lu", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Long long integer. */ - explicit d_string(const long long value) noexcept + explicit String(const long long value) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%lld", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Long long unsigned integer, possibly hexadecimal. */ - explicit d_string(const unsigned long long value, const bool hexadecimal = false) noexcept + explicit String(const unsigned long long value, const bool hexadecimal = false) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, hexadecimal ? "0x%llx" : "%llu", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Single-precision floating point number. */ - explicit d_string(const float value) noexcept + explicit String(const float value) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%f", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } /* * Double-precision floating point number. */ - explicit d_string(const double value) noexcept + explicit String(const double value) noexcept + : fBuffer(_null()), + fBufferLen(0) { char strBuf[0xff+1]; std::snprintf(strBuf, 0xff, "%g", value); strBuf[0xff] = '\0'; - _init(); _dup(strBuf); } @@ -179,9 +189,10 @@ public: /* * Create string from another string. */ - d_string(const d_string& str) noexcept + String(const String& str) noexcept + : fBuffer(_null()), + fBufferLen(0) { - _init(); _dup(str.fBuffer); } @@ -191,7 +202,7 @@ public: /* * Destructor. */ - ~d_string() noexcept + ~String() noexcept { DISTRHO_SAFE_ASSERT_RETURN(fBuffer != nullptr,); @@ -210,7 +221,7 @@ public: /* * Get length of the string. */ - size_t length() const noexcept + std::size_t length() const noexcept { return fBufferLen; } @@ -243,7 +254,7 @@ public: #ifdef __USE_GNU return (strcasestr(fBuffer, strBuf) != nullptr); #else - d_string tmp1(fBuffer), tmp2(strBuf); + String tmp1(fBuffer), tmp2(strBuf); // memory allocation failed or empty string(s) if (tmp1.fBuffer == _null() || tmp2.fBuffer == _null()) @@ -261,7 +272,7 @@ public: /* * Check if character at 'pos' is a digit. */ - bool isDigit(const size_t pos) const noexcept + bool isDigit(const std::size_t pos) const noexcept { DISTRHO_SAFE_ASSERT_RETURN(pos < fBufferLen, false); @@ -285,7 +296,7 @@ public: { DISTRHO_SAFE_ASSERT_RETURN(prefix != nullptr, false); - const size_t prefixLen(std::strlen(prefix)); + const std::size_t prefixLen(std::strlen(prefix)); if (fBufferLen < prefixLen) return false; @@ -310,7 +321,7 @@ public: { DISTRHO_SAFE_ASSERT_RETURN(suffix != nullptr, false); - const size_t suffixLen(std::strlen(suffix)); + const std::size_t suffixLen(std::strlen(suffix)); if (fBufferLen < suffixLen) return false; @@ -322,7 +333,7 @@ public: * Find the first occurrence of character 'c' in the string. * Returns "length()" if the character is not found. */ - size_t find(const char c, bool* const found = nullptr) const noexcept + std::size_t find(const char c, bool* const found = nullptr) const noexcept { if (fBufferLen == 0 || c == '\0') { @@ -331,7 +342,7 @@ public: return fBufferLen; } - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (fBuffer[i] == c) { @@ -350,7 +361,7 @@ public: * Find the first occurrence of string 'strBuf' in the string. * Returns "length()" if the string is not found. */ - size_t find(const char* const strBuf, bool* const found = nullptr) const noexcept + std::size_t find(const char* const strBuf, bool* const found = nullptr) const noexcept { if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0') { @@ -375,7 +386,7 @@ public: if (found != nullptr) *found = true; - return static_cast(ret); + return static_cast(ret); } if (found != nullptr) @@ -387,7 +398,7 @@ public: * Find the last occurrence of character 'c' in the string. * Returns "length()" if the character is not found. */ - size_t rfind(const char c, bool* const found = nullptr) const noexcept + std::size_t rfind(const char c, bool* const found = nullptr) const noexcept { if (fBufferLen == 0 || c == '\0') { @@ -396,7 +407,7 @@ public: return fBufferLen; } - for (size_t i=fBufferLen; i > 0; --i) + for (std::size_t i=fBufferLen; i > 0; --i) { if (fBuffer[i-1] == c) { @@ -415,7 +426,7 @@ public: * Find the last occurrence of string 'strBuf' in the string. * Returns "length()" if the string is not found. */ - size_t rfind(const char* const strBuf, bool* const found = nullptr) const noexcept + std::size_t rfind(const char* const strBuf, bool* const found = nullptr) const noexcept { if (found != nullptr) *found = false; @@ -423,12 +434,12 @@ public: if (fBufferLen == 0 || strBuf == nullptr || strBuf[0] == '\0') return fBufferLen; - const size_t strBufLen(std::strlen(strBuf)); + const std::size_t strBufLen(std::strlen(strBuf)); - size_t ret = fBufferLen; + std::size_t ret = fBufferLen; const char* tmpBuf = fBuffer; - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (std::strstr(tmpBuf+1, strBuf) == nullptr && std::strncmp(tmpBuf, strBuf, strBufLen) == 0) { @@ -455,37 +466,41 @@ public: /* * Replace all occurrences of character 'before' with character 'after'. */ - void replace(const char before, const char after) noexcept + String& replace(const char before, const char after) noexcept { - DISTRHO_SAFE_ASSERT_RETURN(before != '\0' && after != '\0',); + DISTRHO_SAFE_ASSERT_RETURN(before != '\0' && after != '\0', *this); - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (fBuffer[i] == before) fBuffer[i] = after; } + + return *this; } /* * Truncate the string to size 'n'. */ - void truncate(const size_t n) noexcept + String& truncate(const std::size_t n) noexcept { if (n >= fBufferLen) - return; + return *this; - for (size_t i=n; i < fBufferLen; ++i) + for (std::size_t i=n; i < fBufferLen; ++i) fBuffer[i] = '\0'; fBufferLen = n; + + return *this; } /* * Convert all non-basic characters to '_'. */ - void toBasic() noexcept + String& toBasic() noexcept { - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (fBuffer[i] >= '0' && fBuffer[i] <= '9') continue; @@ -498,34 +513,40 @@ public: fBuffer[i] = '_'; } + + return *this; } /* * Convert to all ascii characters to lowercase. */ - void toLower() noexcept + String& toLower() noexcept { static const char kCharDiff('a' - 'A'); - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (fBuffer[i] >= 'A' && fBuffer[i] <= 'Z') fBuffer[i] = static_cast(fBuffer[i] + kCharDiff); } + + return *this; } /* * Convert to all ascii characters to uppercase. */ - void toUpper() noexcept + String& toUpper() noexcept { static const char kCharDiff('a' - 'A'); - for (size_t i=0; i < fBufferLen; ++i) + for (std::size_t i=0; i < fBufferLen; ++i) { if (fBuffer[i] >= 'a' && fBuffer[i] <= 'z') fBuffer[i] = static_cast(fBuffer[i] - kCharDiff); } + + return *this; } /* @@ -536,6 +557,81 @@ public: return fBuffer; } + // ------------------------------------------------------------------- + // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html + // Copyright (C) 2004-2008 René Nyffenegger + + static String asBase64(const void* const data, const std::size_t dataSize) + { + static const char* const kBase64Chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + const std::size_t kTmpBufSize = d_nextPowerOf2(dataSize/3); + + const uchar* bytesToEncode((const uchar*)data); + + uint i=0, j=0; + uint charArray3[3], charArray4[4]; + + char strBuf[kTmpBufSize+1]; + strBuf[kTmpBufSize] = '\0'; + std::size_t strBufIndex = 0; + + String ret; + + for (std::size_t s=0; s> 2; + charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4); + charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6); + charArray4[3] = charArray3[2] & 0x3f; + + for (i=0; i<4; ++i) + strBuf[strBufIndex++] = kBase64Chars[charArray4[i]]; + + if (strBufIndex >= kTmpBufSize-7) + { + strBuf[strBufIndex] = '\0'; + strBufIndex = 0; + ret += strBuf; + } + + i = 0; + } + } + + if (i != 0) + { + for (j=i; j<3; ++j) + charArray3[j] = '\0'; + + charArray4[0] = (charArray3[0] & 0xfc) >> 2; + charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4); + charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6); + charArray4[3] = charArray3[2] & 0x3f; + + for (j=0; j<4 && i<3 && j + * Copyright (C) 2012-2015 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -21,9 +21,7 @@ #include "d_sleep.hpp" #include "d_string.hpp" -#if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 -// has pthread_setname_np -#elif defined(DISTRHO_OS_LINUX) +#ifdef DISTRHO_OS_LINUX # include #endif @@ -156,7 +154,7 @@ public: if (isThreadRunning()) { // should never happen! - d_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__); + d_stderr2("assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__); // copy thread id so we can clear our one pthread_t threadId; @@ -188,7 +186,7 @@ public: * Returns the name of the thread. * This is the name that gets set in the constructor. */ - const d_string& getThreadName() const noexcept + const String& getThreadName() const noexcept { return fName; } @@ -200,10 +198,11 @@ public: { DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',); +#ifdef DISTRHO_OS_LINUX + prctl(PR_SET_NAME, name, 0, 0, 0); +#endif #if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012 pthread_setname_np(pthread_self(), name); -#elif defined(DISTRHO_OS_LINUX) - prctl(PR_SET_NAME, name, 0, 0, 0); #endif } @@ -211,7 +210,7 @@ public: private: Mutex fLock; // Thread lock - const d_string fName; // Thread name + const String fName; // Thread name volatile pthread_t fHandle; // Handle for this thread volatile bool fShouldExit; // true if thread should exit