Loading...
Searching...
No Matches
juce_AbstractFifo.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. 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 permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API AbstractFifo
{
public:
//==============================================================================
AbstractFifo (int capacity) noexcept;
//==============================================================================
int getTotalSize() const noexcept;
int getFreeSpace() const noexcept;
int getNumReady() const noexcept;
void reset() noexcept;
void setTotalSize (int newSize) noexcept;
//==============================================================================
void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
void finishedWrite (int numWritten) noexcept;
void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
void finishedRead (int numRead) noexcept;
//==============================================================================
private:
enum class ReadOrWrite
{
read,
write
};
public:
template <ReadOrWrite mode>
class ScopedReadWrite final
{
public:
ScopedReadWrite() = default;
ScopedReadWrite (AbstractFifo& f, int num) noexcept : fifo (&f)
{
prepare (*fifo, num);
}
ScopedReadWrite (const ScopedReadWrite&) = delete;
ScopedReadWrite (ScopedReadWrite&&) noexcept;
ScopedReadWrite& operator= (const ScopedReadWrite&) = delete;
ScopedReadWrite& operator= (ScopedReadWrite&&) noexcept;
~ScopedReadWrite() noexcept
{
if (fifo != nullptr)
finish (*fifo, blockSize1 + blockSize2);
}
template <typename FunctionToApply>
void forEach (FunctionToApply&& func) const
{
for (auto i = startIndex1, e = startIndex1 + blockSize1; i != e; ++i) func (i);
for (auto i = startIndex2, e = startIndex2 + blockSize2; i != e; ++i) func (i);
}
int startIndex1, blockSize1, startIndex2, blockSize2;
private:
void prepare (AbstractFifo&, int) noexcept;
static void finish (AbstractFifo&, int) noexcept;
void swap (ScopedReadWrite&) noexcept;
AbstractFifo* fifo = nullptr;
};
using ScopedRead = ScopedReadWrite<ReadOrWrite::read>;
using ScopedWrite = ScopedReadWrite<ReadOrWrite::write>;
ScopedRead read (int numToRead) noexcept;
ScopedWrite write (int numToWrite) noexcept;
private:
//==============================================================================
int bufferSize;
Atomic<int> validStart, validEnd;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
};
template <>
inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::finish (AbstractFifo& f, int num) noexcept
{
f.finishedRead (num);
}
template <>
inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::finish (AbstractFifo& f, int num) noexcept
{
f.finishedWrite (num);
}
template <>
inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::prepare (AbstractFifo& f, int num) noexcept
{
f.prepareToRead (num, startIndex1, blockSize1, startIndex2, blockSize2);
}
template <>
inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::prepare (AbstractFifo& f, int num) noexcept
{
f.prepareToWrite (num, startIndex1, blockSize1, startIndex2, blockSize2);
}
} // namespace juce
juce::AbstractFifo::ScopedReadWrite
Definition juce_AbstractFifo.h:203
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite(AbstractFifo &f, int num) noexcept
Definition juce_AbstractFifo.h:213
juce::AbstractFifo::ScopedReadWrite::forEach
void forEach(FunctionToApply &&func) const
Definition juce_AbstractFifo.h:237
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite()=default
juce::AbstractFifo
Definition juce_AbstractFifo.h:73
juce::AbstractFifo::reset
void reset() noexcept
Definition juce_AbstractFifo.cpp:41
juce::AbstractFifo::prepareToWrite
void prepareToWrite(int numToWrite, int &startIndex1, int &blockSize1, int &startIndex2, int &blockSize2) const noexcept
Definition juce_AbstractFifo.cpp:55
juce::AbstractFifo::getTotalSize
int getTotalSize() const noexcept
Definition juce_AbstractFifo.cpp:31
juce::AbstractFifo::read
ScopedRead read(int numToRead) noexcept
Definition juce_AbstractFifo.cpp:163
juce::AbstractFifo::prepareToRead
void prepareToRead(int numWanted, int &startIndex1, int &blockSize1, int &startIndex2, int &blockSize2) const noexcept
Definition juce_AbstractFifo.cpp:93
juce::AbstractFifo::AbstractFifo
AbstractFifo(int capacity) noexcept
Definition juce_AbstractFifo.cpp:26
juce::AbstractFifo::finishedRead
void finishedRead(int numRead) noexcept
Definition juce_AbstractFifo.cpp:119
juce::AbstractFifo::getFreeSpace
int getFreeSpace() const noexcept
Definition juce_AbstractFifo.cpp:32
juce::AbstractFifo::finishedWrite
void finishedWrite(int numWritten) noexcept
Definition juce_AbstractFifo.cpp:81
juce::AbstractFifo::write
ScopedWrite write(int numToWrite) noexcept
Definition juce_AbstractFifo.cpp:164
juce::AbstractFifo::getNumReady
int getNumReady() const noexcept
Definition juce_AbstractFifo.cpp:34
juce::AbstractFifo::setTotalSize
void setTotalSize(int newSize) noexcept
Definition juce_AbstractFifo.cpp:47
juce::Atomic
Definition juce_Atomic.h:42