JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ScopedContentSharerInterface.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::detail
27{
28
29/*
30 Instances of this type can show and dismiss a content sharer.
31
32 This is an interface rather than a concrete type so that platforms can pick an implementation at
33 runtime if necessary.
34*/
36{
37 virtual ~ScopedContentSharerInterface() = default;
38
39 /* Shows the content sharer.
40
41 When the content sharer exits normally, it should send the result to the passed-in function.
42 The passed-in function is safe to call from any thread at any time.
43 */
44 virtual void runAsync (ContentSharer::Callback callback)
45 {
47 NullCheckedInvocation::invoke (callback, false, "Content sharing not available on this platform!");
48 }
49
50 /* Forcefully closes the content sharer.
51
52 This will be called when the content sharer handle has fallen out of scope.
53 If the content sharer has already been closed by the user, this shouldn't do anything.
54 */
55 virtual void close() {}
56
57 /* Implemented differently for each platform. */
60
61 /* Implemented below. */
64};
65
67 private AsyncUpdater
68{
69public:
71 : parent (parentIn) {}
72
73 void runAsync (ContentSharer::Callback cb) override
74 {
75 callback = std::move (cb);
76
77 task = std::async (std::launch::async, [this]
78 {
79 std::tie (temporaryFiles, error) = prepareTemporaryFiles();
81 });
82 }
83
84 void close() override
85 {
86 if (inner != nullptr)
87 inner->close();
88 }
89
90private:
91 virtual std::tuple<Array<URL>, String> prepareTemporaryFiles() const = 0;
92
93 void handleAsyncUpdate() override
94 {
95 if (error.isNotEmpty())
96 {
97 NullCheckedInvocation::invoke (callback, false, error);
98 return;
99 }
100
101 inner = shareFiles (temporaryFiles, parent);
102
103 if (inner == nullptr)
104 {
105 NullCheckedInvocation::invoke (callback, false, TRANS ("Failed to create file sharer"));
106 return;
107 }
108
109 inner->runAsync (callback);
110 }
111
112 Array<URL> temporaryFiles;
113 String error;
117 Component* parent = nullptr;
118};
119
120std::unique_ptr<ScopedContentSharerInterface> ScopedContentSharerInterface::shareImages (const Array<Image>& images,
122 Component* parent)
123{
125 {
126 public:
127 Decorator (Array<Image> imagesIn, std::unique_ptr<ImageFileFormat> formatIn, Component* parentIn)
128 : TemporaryFilesDecorator (parentIn), images (std::move (imagesIn)), format (std::move (formatIn)) {}
129
130 private:
131 std::tuple<Array<URL>, String> prepareTemporaryFiles() const override
132 {
133 const auto extension = format->getFormatName().toLowerCase();
134
135 Array<URL> result;
136
137 for (const auto& image : images)
138 {
139 File tempFile = File::createTempFile (extension);
140
141 if (! tempFile.create().wasOk())
142 return { Array<URL>{}, TRANS ("Failed to create temporary file") };
143
145
146 if (outputStream == nullptr)
147 return { Array<URL>{}, TRANS ("Failed to open temporary file for writing") };
148
149 if (format->writeImageToStream (image, *outputStream))
150 result.add (URL (tempFile));
151 }
152
153 jassert (std::all_of (result.begin(),
154 result.end(),
155 [] (const auto& url)
156 {
157 return url.isLocalFile() && url.getLocalFile().existsAsFile();
158 }));
159
160 return { std::move (result), String{} };
161 }
162
163 Array<Image> images;
165 };
166
167 return std::make_unique<Decorator> (images,
168 format == nullptr ? std::make_unique<PNGImageFormat>() : std::move (format),
169 parent);
170}
171
172std::unique_ptr<ScopedContentSharerInterface> ScopedContentSharerInterface::shareData (MemoryBlock mb, Component* parent)
173{
174 class Decorator : public TemporaryFilesDecorator
175 {
176 public:
177 Decorator (MemoryBlock mbIn, Component* parentIn)
178 : TemporaryFilesDecorator (parentIn), mb (std::move (mbIn)) {}
179
180 private:
181 std::tuple<Array<URL>, String> prepareTemporaryFiles() const override
182 {
183 File tempFile = File::createTempFile ("data");
184
185 if (! tempFile.create().wasOk())
186 return { Array<URL>{}, TRANS ("Failed to create temporary file") };
187
188 std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream());
189
190 if (outputStream == nullptr)
191 return { Array<URL>{}, TRANS ("Failed to open temporary file for writing") };
192
193 size_t pos = 0;
194 size_t totalSize = mb.getSize();
195
196 while (pos < totalSize)
197 {
198 size_t numToWrite = std::min ((size_t) 8192, totalSize - pos);
199
200 if (! outputStream->write (mb.begin() + pos, numToWrite))
201 return { Array<URL>{}, TRANS ("Failed to write to temporary file") };
202
203 pos += numToWrite;
204 }
205
206 return { Array<URL> { URL (tempFile) }, String{} };
207 }
208
209 MemoryBlock mb;
210 };
211
212 return std::make_unique<Decorator> (std::move (mb), parent);
213}
214
215} // namespace juce::detail
T all_of(T... args)
T async(T... args)
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
ElementType * begin() noexcept
Returns a pointer to the first element in the array.
Definition juce_Array.h:328
ElementType * end() noexcept
Returns a pointer to the element which follows the last element in the array.
Definition juce_Array.h:344
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
Has a callback method that is triggered asynchronously.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
The base class for all JUCE user-interface objects.
Represents a local file or directory.
Definition juce_File.h:45
std::unique_ptr< FileOutputStream > createOutputStream(size_t bufferSize=0x8000) const
Creates a stream to write to this file.
Result create() const
Creates an empty file if it doesn't already exist.
A class to hold a resizable block of raw data.
bool wasOk() const noexcept
Returns true if this result indicates a success.
The JUCE String class!
Definition juce_String.h:53
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
T format(T... args)
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
#define jassert(expression)
Platform-independent assertion macro.
#define jassertfalse
This will always cause an assertion failure.
T min(T... args)
T move(T... args)
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T tie(T... args)