From 17f43a079be5fdbb38d7992c21acda89ad91bbbd Mon Sep 17 00:00:00 2001 From: jatin Date: Sun, 7 Jul 2024 11:15:24 -0700 Subject: [PATCH] Adding arena helpers --- .../DataStructures/chowdsp_StringHelpers.h | 5 ++++ .../Helpers/chowdsp_ArenaHelpers.h | 26 +++++++++++++++++++ .../chowdsp_data_structures.h | 1 + 3 files changed, 32 insertions(+) create mode 100644 modules/common/chowdsp_data_structures/Helpers/chowdsp_ArenaHelpers.h diff --git a/modules/common/chowdsp_core/DataStructures/chowdsp_StringHelpers.h b/modules/common/chowdsp_core/DataStructures/chowdsp_StringHelpers.h index fa6662bae..5725ae15e 100644 --- a/modules/common/chowdsp_core/DataStructures/chowdsp_StringHelpers.h +++ b/modules/common/chowdsp_core/DataStructures/chowdsp_StringHelpers.h @@ -8,5 +8,10 @@ inline juce::String toString (const std::string_view& sv) noexcept { return juce::String::fromUTF8 (sv.data(), (int) sv.size()); } + +inline std::string_view toStringView (const juce::String& str) noexcept +{ + return { str.toRawUTF8(), str.getNumBytesAsUTF8() }; +} #endif } // namespace chowdsp diff --git a/modules/common/chowdsp_data_structures/Helpers/chowdsp_ArenaHelpers.h b/modules/common/chowdsp_data_structures/Helpers/chowdsp_ArenaHelpers.h new file mode 100644 index 000000000..d10371872 --- /dev/null +++ b/modules/common/chowdsp_data_structures/Helpers/chowdsp_ArenaHelpers.h @@ -0,0 +1,26 @@ +#pragma once + +namespace chowdsp::arena +{ +template +std::span make_span (Arena& arena, I size, size_t alignment = alignof (T)) +{ + return { arena.template allocate (size, alignment), static_cast (size) }; +} + +template +std::string_view alloc_string (Arena& arena, const std::string_view& str) +{ + auto* data = arena.template allocate (str.size()); + std::copy (str.begin(), str.end(), data); + return std::string_view { data, str.size() }; // NOLINT +}; + +#if CHOWDSP_USING_JUCE +template +std::string_view alloc_string (Arena& arena, const juce::String& str) +{ + return alloc_string (arena, toStringView (str)); +} +#endif +} diff --git a/modules/common/chowdsp_data_structures/chowdsp_data_structures.h b/modules/common/chowdsp_data_structures/chowdsp_data_structures.h index 17bcfba03..0ee7a2429 100644 --- a/modules/common/chowdsp_data_structures/chowdsp_data_structures.h +++ b/modules/common/chowdsp_data_structures/chowdsp_data_structures.h @@ -46,6 +46,7 @@ BEGIN_JUCE_MODULE_DECLARATION #include "Allocators/chowdsp_ArenaAllocator.h" #include "Allocators/chowdsp_ChainedArenaAllocator.h" #include "Allocators/chowdsp_STLArenaAllocator.h" +#include "Helpers/chowdsp_ArenaHelpers.h" #include "Structures/chowdsp_BucketArray.h" #include "Structures/chowdsp_AbstractTree.h"