diff --git a/include/asset.hpp b/include/asset.hpp index 85ef3ab0..4e3be2aa 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -1,6 +1,5 @@ #pragma once - -#include +#include "common.hpp" #include "plugin.hpp" diff --git a/include/audio.hpp b/include/audio.hpp index 425fe90d..359c278e 100644 --- a/include/audio.hpp +++ b/include/audio.hpp @@ -9,6 +9,8 @@ #include #pragma GCC diagnostic pop +#include "common.hpp" + namespace rack { diff --git a/include/color.hpp b/include/color.hpp index e6a723b5..47f19b56 100644 --- a/include/color.hpp +++ b/include/color.hpp @@ -1,6 +1,6 @@ #pragma once -#include "util/common.hpp" +#include "common.hpp" #include "string.hpp" #include "nanovg.h" diff --git a/include/macros.hpp b/include/common.hpp similarity index 66% rename from include/macros.hpp rename to include/common.hpp index ddb3bda9..95e72ec1 100644 --- a/include/macros.hpp +++ b/include/common.hpp @@ -1,5 +1,20 @@ #pragma once +// Include some of the C++ standard library for convenience +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/** Deprecation notice for GCC */ +#define DEPRECATED __attribute__ ((deprecated)) + /** Concatenates two literals or two macros Example: @@ -11,6 +26,7 @@ expands to #define CONCAT_LITERAL(x, y) x ## y #define CONCAT(x, y) CONCAT_LITERAL(x, y) + /** Surrounds raw text with quotes Example: #define NAME "world" @@ -22,9 +38,11 @@ and of course the C++ lexer/parser then concatenates the string literals. #define TOSTRING_LITERAL(x) #x #define TOSTRING(x) TOSTRING_LITERAL(x) + /** Produces the length of a static array in number of elements */ #define LENGTHOF(arr) (sizeof(arr) / sizeof((arr)[0])) + /** Reserve space for `count` enums starting with `name`. Example: enum Foo { @@ -36,9 +54,6 @@ Example: */ #define ENUMS(name, count) name, name ## _LAST = name + (count) - 1 -/** Deprecation notice for GCC */ -#define DEPRECATED __attribute__ ((deprecated)) - /** References binary files compiled into the program. For example, to include a file "Test.dat" directly into your program binary, add @@ -65,3 +80,46 @@ to get its size in bytes. // The symbol "_binary_##sym##_size" doesn't seem to be valid after a plugin is dynamically loaded, so simply take the difference between the two addresses. #define BINARY_SIZE(sym) ((size_t) (&_binary_##sym##_end - &_binary_##sym##_start)) #endif + + +/** C#-style property constructor +Example: + Foo *foo = construct(&Foo::greeting, "Hello world"); +*/ +template +T *construct() { + return new T(); +} + +template +T *construct(F f, V v, Args... args) { + T *o = construct(args...); + o->*f = v; + return o; +} + +/** Defers code until the scope is destructed +From http://www.gingerbill.org/article/defer-in-cpp.html + +Example: + file = fopen(...); + DEFER({ + fclose(file); + }); +*/ +template +struct DeferWrapper { + F f; + DeferWrapper(F f) : f(f) {} + ~DeferWrapper() { f(); } +}; + +template +DeferWrapper deferWrapper(F f) { + return DeferWrapper(f); +} + +#define DEFER(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code) + +/** Deprecated lowercase macro */ +#define defer(...) DEFER(__VA_ARGS__) diff --git a/include/dsp/ringbuffer.hpp b/include/dsp/ringbuffer.hpp index 73506e91..37d0449c 100644 --- a/include/dsp/ringbuffer.hpp +++ b/include/dsp/ringbuffer.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include "util/common.hpp" +#include "common.hpp" namespace rack { diff --git a/include/engine.hpp b/include/engine.hpp index ed4d801d..0b28eba4 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include "util/common.hpp" +#include "common.hpp" #include diff --git a/include/gamepad.hpp b/include/gamepad.hpp index 3db2696f..60cb8b79 100644 --- a/include/gamepad.hpp +++ b/include/gamepad.hpp @@ -1,6 +1,5 @@ #pragma once - -#include "util/common.hpp" +#include "common.hpp" #include "midi.hpp" diff --git a/include/keyboard.hpp b/include/keyboard.hpp index 982f967c..1367e6d2 100644 --- a/include/keyboard.hpp +++ b/include/keyboard.hpp @@ -1,8 +1,7 @@ #pragma once - -#include "util/common.hpp" -#include "midi.hpp" #include +#include "common.hpp" +#include "midi.hpp" namespace rack { diff --git a/include/logger.hpp b/include/logger.hpp index bb70dec4..75ce1c2b 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -1,4 +1,5 @@ #pragma once +#include "common.hpp" /** Example usage: diff --git a/include/math.hpp b/include/math.hpp index 09901489..dfe93c05 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -1,8 +1,6 @@ #pragma once -#include -#include #include // for std::min, max -#include "macros.hpp" +#include "common.hpp" namespace rack { diff --git a/include/midi.hpp b/include/midi.hpp index f53c2bce..c08d320f 100644 --- a/include/midi.hpp +++ b/include/midi.hpp @@ -1,10 +1,9 @@ #pragma once - -#include "util/common.hpp" #include #include #include #include +#include "common.hpp" namespace rack { diff --git a/include/network.hpp b/include/network.hpp index bd52208d..49891937 100644 --- a/include/network.hpp +++ b/include/network.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include "common.hpp" namespace rack { diff --git a/include/plugin.hpp b/include/plugin.hpp index f3089d9d..a378317f 100644 --- a/include/plugin.hpp +++ b/include/plugin.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include "common.hpp" #include "tags.hpp" diff --git a/include/rack.hpp b/include/rack.hpp index d7fee4e3..f6c5ef1e 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -1,10 +1,17 @@ #pragma once // Include headers that plugins will likely use, for convenience -#include "util/common.hpp" +#include "common.hpp" +#include "math.hpp" +#include "string.hpp" +#include "logger.hpp" +#include "system.hpp" +#include "random.hpp" +#include "network.hpp" #include "asset.hpp" #include "plugin.hpp" #include "engine.hpp" +#include "window.hpp" #include "widgets.hpp" #include "app.hpp" #include "ui.hpp" @@ -16,7 +23,6 @@ namespace rack { using namespace math; using namespace string; -using namespace logger; } // namespace rack diff --git a/include/random.hpp b/include/random.hpp new file mode 100644 index 00000000..5993259a --- /dev/null +++ b/include/random.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include "common.hpp" + + +namespace rack { +namespace random { + + +/** Seeds the RNG with the current time */ +void init(); +/** Returns a uniform random uint32_t from 0 to UINT32_MAX */ +uint32_t u32(); +uint64_t u64(); +/** Returns a uniform random float in the interval [0.0, 1.0) */ +float uniform(); +/** Returns a normal random number with mean 0 and standard deviation 1 */ +float normal(); + + +} // namespace random + + +DEPRECATED inline float randomu32() {return random::u32();} +DEPRECATED inline float randomu64() {return random::u64();} +DEPRECATED inline float randomUniform() {return random::uniform();} +DEPRECATED inline float randomNormal() {return random::normal();} +DEPRECATED inline float randomf() {return random::uniform();} + + +} // namespace rack diff --git a/include/settings.hpp b/include/settings.hpp index 5f89d5a8..f54ee9a1 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -1,6 +1,5 @@ #pragma once - -#include +#include "common.hpp" namespace rack { diff --git a/include/string.hpp b/include/string.hpp index 08fd3a90..788b1ed5 100644 --- a/include/string.hpp +++ b/include/string.hpp @@ -1,7 +1,7 @@ #pragma once -#include "util/common.hpp" -#include +#include "common.hpp" +#include // for transform #include // for dirname and basename diff --git a/include/system.hpp b/include/system.hpp index a7c1bc1a..6ba1b5ab 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -1,6 +1,6 @@ #pragma once -#include #include +#include "common.hpp" namespace rack { diff --git a/include/util/common.hpp b/include/util/common.hpp deleted file mode 100644 index 8c0735db..00000000 --- a/include/util/common.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -// Include most of the C++ standard library for convenience -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "macros.hpp" -#include "math.hpp" -#include "string.hpp" -#include "logger.hpp" -#include "system.hpp" - - -namespace rack { - -//////////////////// -// Template hacks -//////////////////// - -/** C#-style property constructor -Example: - Foo *foo = construct(&Foo::greeting, "Hello world"); -*/ -template -T *construct() { - return new T(); -} - -template -T *construct(F f, V v, Args... args) { - T *o = construct(args...); - o->*f = v; - return o; -} - -/** Defers code until the scope is destructed -From http://www.gingerbill.org/article/defer-in-cpp.html - -Example: - file = fopen(...); - defer({ - fclose(file); - }); -*/ -template -struct DeferWrapper { - F f; - DeferWrapper(F f) : f(f) {} - ~DeferWrapper() { f(); } -}; - -template -DeferWrapper deferWrapper(F f) { - return DeferWrapper(f); -} - -#define defer(code) auto CONCAT(_defer_, __COUNTER__) = deferWrapper([&]() code) - -//////////////////// -// Random number generator -// random.cpp -//////////////////// - -/** Seeds the RNG with the current time */ -void randomInit(); -/** Returns a uniform random uint32_t from 0 to UINT32_MAX */ -uint32_t randomu32(); -uint64_t randomu64(); -/** Returns a uniform random float in the interval [0.0, 1.0) */ -float randomUniform(); -/** Returns a normal random number with mean 0 and standard deviation 1 */ -float randomNormal(); - -DEPRECATED inline float randomf() {return randomUniform();} - - -} // namespace rack diff --git a/include/widgets.hpp b/include/widgets.hpp index 2ab39802..0348bfe9 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -5,7 +5,7 @@ #include "nanovg.h" #include "nanosvg.h" -#include "util/common.hpp" +#include "common.hpp" #include "events.hpp" #include "color.hpp" diff --git a/include/window.hpp b/include/window.hpp index a3b92413..37735820 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,9 +1,11 @@ #pragma once -#include "widgets.hpp" #define GLEW_STATIC #include #include +#include "common.hpp" +#include "widgets.hpp" + #ifdef ARCH_MAC #define WINDOW_MOD_KEY_NAME "Cmd" diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 1d666a7d..b95a8508 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -1,9 +1,5 @@ -#include "app.hpp" -#include "engine.hpp" -#include "plugin.hpp" -#include "window.hpp" -#include "asset.hpp" #include "osdialog.h" +#include "rack.hpp" namespace rack { diff --git a/src/app/ParamWidget.cpp b/src/app/ParamWidget.cpp index e8b490c8..a2f573a1 100644 --- a/src/app/ParamWidget.cpp +++ b/src/app/ParamWidget.cpp @@ -1,5 +1,6 @@ #include "app.hpp" #include "engine.hpp" +#include "random.hpp" namespace rack { @@ -31,7 +32,7 @@ void ParamWidget::reset() { void ParamWidget::randomize() { // Infinite params should not be randomized if (randomizable && std::isfinite(minValue) && std::isfinite(maxValue)) { - setValue(math::rescale(randomUniform(), 0.f, 1.f, minValue, maxValue)); + setValue(math::rescale(random::uniform(), 0.f, 1.f, minValue, maxValue)); } } diff --git a/src/app/PluginManagerWidget.cpp b/src/app/PluginManagerWidget.cpp index 7b2a9ee0..e48db788 100644 --- a/src/app/PluginManagerWidget.cpp +++ b/src/app/PluginManagerWidget.cpp @@ -1,4 +1,5 @@ #include +#include "system.hpp" #include "app.hpp" #include "plugin.hpp" #include "window.hpp" diff --git a/src/app/RackScene.cpp b/src/app/RackScene.cpp index 3338a23a..d4c734bb 100644 --- a/src/app/RackScene.cpp +++ b/src/app/RackScene.cpp @@ -1,8 +1,6 @@ -#include "app.hpp" -#include "window.hpp" -#include "osdialog.h" -#include #include +#include "osdialog.h" +#include "rack.hpp" namespace rack { diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 478b6033..4ea61823 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -1,12 +1,8 @@ -#include "app.hpp" -#include "engine.hpp" -#include "plugin.hpp" -#include "window.hpp" -#include "settings.hpp" -#include "asset.hpp" #include #include #include "osdialog.h" +#include "rack.hpp" +#include "settings.hpp" namespace rack { diff --git a/src/asset.cpp b/src/asset.cpp index 3b709435..acc89ed0 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -1,7 +1,3 @@ -#include "asset.hpp" -#include "util/common.hpp" -#include "osdialog.h" - #if ARCH_MAC #include #include @@ -19,6 +15,9 @@ #include #endif +#include "rack.hpp" +#include "osdialog.h" + namespace rack { namespace asset { diff --git a/src/audio.cpp b/src/audio.cpp index 85cdfef1..15efc558 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -1,5 +1,5 @@ +#include "rack.hpp" #include "audio.hpp" -#include "util/common.hpp" #include "bridge.hpp" diff --git a/src/bridge.cpp b/src/bridge.cpp index cc348ed9..72328c68 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -1,7 +1,3 @@ -#include "bridge.hpp" -#include "util/common.hpp" -#include "dsp/ringbuffer.hpp" - #include #if ARCH_WIN #include @@ -13,9 +9,12 @@ #include #endif - #include +#include "rack.hpp" +#include "bridge.hpp" +#include "dsp/ringbuffer.hpp" + namespace rack { diff --git a/src/engine.cpp b/src/engine.cpp index 6bc3a941..08526aea 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,14 +1,14 @@ -#include -#include -#include -#include #include #include #include #include +#include +#include + #include #include +#include "rack.hpp" #include "engine.hpp" diff --git a/src/gamepad.cpp b/src/gamepad.cpp index 7d9436ec..831bdefe 100644 --- a/src/gamepad.cpp +++ b/src/gamepad.cpp @@ -1,4 +1,4 @@ -#include +#include "rack.hpp" #include "gamepad.hpp" diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 89a452f6..4f68f164 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -1,5 +1,5 @@ +#include "rack.hpp" #include "keyboard.hpp" -#include namespace rack { diff --git a/src/logger.cpp b/src/logger.cpp index b6427f1e..d7e20a6e 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,4 +1,3 @@ -#include #include #include "logger.hpp" #include "asset.hpp" diff --git a/src/main.cpp b/src/main.cpp index 60310cd3..26b32363 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,11 @@ -#include "util/common.hpp" -#include "engine.hpp" -#include "window.hpp" -#include "app.hpp" -#include "plugin.hpp" -#include "settings.hpp" -#include "asset.hpp" -#include "bridge.hpp" -#include "midi.hpp" +#include +#include "osdialog.h" +#include "rack.hpp" #include "rtmidi.hpp" #include "keyboard.hpp" #include "gamepad.hpp" -#include "color.hpp" - -#include "osdialog.h" -#include +#include "bridge.hpp" +#include "settings.hpp" #ifdef ARCH_WIN #include @@ -59,7 +51,7 @@ int main(int argc, char* argv[]) { #endif // Initialize environment - randomInit(); + random::init(); asset::init(devMode); logger::init(devMode); diff --git a/src/midi.cpp b/src/midi.cpp index 1f64f181..565a42a9 100644 --- a/src/midi.cpp +++ b/src/midi.cpp @@ -1,3 +1,4 @@ +#include "rack.hpp" #include "midi.hpp" #include "rtmidi.hpp" #include "bridge.hpp" diff --git a/src/network.cpp b/src/network.cpp index 2943ec15..326fce9c 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -2,7 +2,6 @@ #include #include -#include "util/common.hpp" #include "network.hpp" diff --git a/src/plugin.cpp b/src/plugin.cpp index 5652588b..46485846 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -1,12 +1,3 @@ -#include "plugin.hpp" -#include "app.hpp" -#include "asset.hpp" -#include "network.hpp" -#include "osdialog.h" - -#include -#include -#include #include #include #include @@ -28,6 +19,9 @@ #endif #include +#include "rack.hpp" +#include "osdialog.h" + namespace rack { diff --git a/src/util/random.cpp b/src/random.cpp similarity index 84% rename from src/util/random.cpp rename to src/random.cpp index 04d065fc..423c6145 100644 --- a/src/util/random.cpp +++ b/src/random.cpp @@ -1,9 +1,11 @@ -#include "util/common.hpp" #include #include +#include "random.hpp" +#include "math.hpp" namespace rack { +namespace random { // xoroshiro128+ @@ -27,7 +29,7 @@ static uint64_t xoroshiro128plus_next(void) { return result; } -void randomInit() { +void init() { // Only allow the seed to be initialized once during the lifetime of the program. assert(xoroshiro128plus_state[0] == 0 && xoroshiro128plus_state[1] == 0); struct timeval tv; @@ -40,33 +42,34 @@ void randomInit() { } } -uint32_t randomu32() { +uint32_t u32() { return xoroshiro128plus_next() >> 32; } -uint64_t randomu64() { +uint64_t u64() { return xoroshiro128plus_next(); } -float randomUniform() { +float uniform() { // 24 bits of granularity is the best that can be done with floats while ensuring that the return value lies in [0.0, 1.0). return (xoroshiro128plus_next() >> (64 - 24)) / powf(2, 24); } -float randomNormal() { +float normal() { // Box-Muller transform - float radius = sqrtf(-2.f * logf(1.f - randomUniform())); - float theta = 2.f * M_PI * randomUniform(); + float radius = sqrtf(-2.f * logf(1.f - uniform())); + float theta = 2.f * M_PI * uniform(); return radius * sinf(theta); // // Central Limit Theorem // const int n = 8; // float sum = 0.0; // for (int i = 0; i < n; i++) { - // sum += randomUniform(); + // sum += uniform(); // } // return (sum - n / 2.f) / sqrtf(n / 12.f); } +} // namespace random } // namespace rack diff --git a/src/rtmidi.cpp b/src/rtmidi.cpp index d0c65d71..fca7f052 100644 --- a/src/rtmidi.cpp +++ b/src/rtmidi.cpp @@ -1,5 +1,6 @@ -#include "rtmidi.hpp" #include +#include "rack.hpp" +#include "rtmidi.hpp" namespace rack { diff --git a/src/settings.cpp b/src/settings.cpp index 6392f867..5e4b239b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,9 +1,6 @@ -#include "settings.hpp" -#include "app.hpp" -#include "window.hpp" -#include "engine.hpp" -#include "plugin.hpp" #include +#include "rack.hpp" +#include "settings.hpp" namespace rack { diff --git a/src/system.cpp b/src/system.cpp index 99a2a61c..f2e01d63 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -7,7 +7,6 @@ #endif #include "system.hpp" -#include "util/common.hpp" namespace rack { diff --git a/src/window.cpp b/src/window.cpp index 1ce1ac69..76cfcfbc 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,16 +1,20 @@ -#include "window.hpp" -#include "app.hpp" -#include "asset.hpp" -#include "gamepad.hpp" -#include "keyboard.hpp" -#include "color.hpp" - #include #include #include +#ifdef ARCH_MAC + // For CGAssociateMouseAndMouseCursorPosition + #include +#endif + + #include "osdialog.h" +#include "rack.hpp" +#include "window.hpp" +#include "keyboard.hpp" +#include "gamepad.hpp" + #define NANOVG_GL2_IMPLEMENTATION 1 // #define NANOVG_GL3_IMPLEMENTATION 1 // #define NANOVG_GLES2_IMPLEMENTATION 1 @@ -25,11 +29,6 @@ #define NANOSVG_ALL_COLOR_KEYWORDS #include "nanosvg.h" -#ifdef ARCH_MAC - // For CGAssociateMouseAndMouseCursorPosition - #include -#endif - namespace rack {