bsnes/nall/queue.hpp
Tim Allen 212da0a966 Update to 20180730 release.
byuu says:

These WIPs-within-WIPs are getting more and more broken ... this isn't
going the way I wanted.

But ... this time around, I've revamped the entire ruby API again, to
solve a bunch of tough problems that have always made using ruby really
clunky.

But there are *so many* ruby drivers that it's going to take a long
time to work through them all. This WIP is only going to run bsnes, and
only on FreeBSD, and only with some drivers.

hiro's Application::initialize() now calls hiro::initialize(), which you
define inside of your hiro apps. This lets you call
Application::setName(...) before anything else in hiro runs. This is
essential on Xorg to set program icons, for instance.

With the ruby rewrite and the change to hiro, I can get away from the
need to make everything in bsnes/higan pointers to objects, and can now
just declare them as regular objects.
2018-07-31 12:23:12 +10:00

96 lines
2.0 KiB
C++

#pragma once
//simple circular ring buffer
#include <nall/range.hpp>
#include <nall/serializer.hpp>
namespace nall {
template<typename T>
struct queue {
queue() = default;
queue(const queue& source) { operator=(source); }
queue(queue&& source) { operator=(move(source)); }
auto operator=(const queue& source) -> queue& {
if(this == &source) return *this;
reset();
_size = source._size;
_data = new T[_size];
for(uint n : range(_size)) _data[n] = source._data[n];
_read = source._read;
_write = source._write;
return *this;
}
auto operator=(queue&& source) -> queue& {
if(this == &source) return *this;
_data = source._data;
_size = source._size;
_read = source._read;
_write = source._write;
source._data = nullptr;
source.reset();
return *this;
}
~queue() {
reset();
}
explicit operator bool() const { return _size; }
template<typename U = T> auto size() const -> uint { return _size * sizeof(T) / sizeof(U); }
auto empty() const -> bool { return _read == _write; }
auto pending() const -> bool { return _read != _write; }
auto full() const -> bool { return _write == _size; }
auto data() -> T* { return _data; }
auto data() const -> const T* { return _data; }
auto reset() {
delete[] _data;
_data = nullptr;
_size = 0;
_read = 0;
_write = 0;
}
auto resize(uint size, const T& value = {}) -> void {
reset();
_size = size;
_data = new T[_size];
for(uint n : range(_size)) _data[n] = value;
}
auto flush() -> void {
_read = 0;
_write = 0;
}
auto read() -> T {
if(_read >= _size) _read = 0;
return _data[_read++];
}
auto write(const T& value) -> void {
if(_write >= _size) _write = 0;
_data[_write++] = value;
}
auto serialize(serializer& s) -> void {
s.array(_data, _size);
s.integer(_size);
s.integer(_read);
s.integer(_write);
}
private:
T* _data = nullptr;
uint _size = 0;
uint _read = 0;
uint _write = 0;
};
}