Loading...
Searching...
No Matches
juce_SingleThreadedAbstractFifo.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 SingleThreadedAbstractFifo
{
public:
SingleThreadedAbstractFifo() = default;
explicit SingleThreadedAbstractFifo (int sizeIn)
: size (sizeIn)
{
// This class only works properly when the size is a power of two.
// Use nextPowerOfTwo() to find a good size, and ensure that your
// backing storage is the same size.
jassert (isPowerOfTwo (sizeIn));
}
int getRemainingSpace() const { return size - numReadable; }
int getNumReadable() const { return numReadable; }
int getSize() const { return size; }
std::array<Range<int>, 2> write (int num)
{
const auto startPos = (readPos + numReadable) & (size - 1);
const auto maxToWrite = jmin (getRemainingSpace(), num);
const auto firstBlockSize = jmin (maxToWrite, size - startPos);
numReadable += maxToWrite;
return { { { startPos, startPos + firstBlockSize }, { 0, maxToWrite - firstBlockSize } } };
}
std::array<Range<int>, 2> read (int num)
{
const auto startPos = readPos;
const auto maxToRead = jmin (numReadable, num);
const auto firstBlockSize = jmin (maxToRead, size - startPos);
readPos = (startPos + maxToRead) & (size - 1);
numReadable -= maxToRead;
return { { { startPos, startPos + firstBlockSize }, { 0, maxToRead - firstBlockSize } } };
}
private:
int size = 0, readPos = 0, numReadable = 0;
};
} // namespace juce
juce::SingleThreadedAbstractFifo::SingleThreadedAbstractFifo
SingleThreadedAbstractFifo(int sizeIn)
Definition juce_SingleThreadedAbstractFifo.h:70
juce::SingleThreadedAbstractFifo::SingleThreadedAbstractFifo
SingleThreadedAbstractFifo()=default
juce::SingleThreadedAbstractFifo::getRemainingSpace
int getRemainingSpace() const
Definition juce_SingleThreadedAbstractFifo.h:80
juce::SingleThreadedAbstractFifo::read
std::array< Range< int >, 2 > read(int num)
Definition juce_SingleThreadedAbstractFifo.h:109
juce::SingleThreadedAbstractFifo::getSize
int getSize() const
Definition juce_SingleThreadedAbstractFifo.h:86
juce::SingleThreadedAbstractFifo::write
std::array< Range< int >, 2 > write(int num)
Definition juce_SingleThreadedAbstractFifo.h:93
juce::SingleThreadedAbstractFifo::getNumReadable
int getNumReadable() const
Definition juce_SingleThreadedAbstractFifo.h:83