mirror of
https://github.com/libretro/bsnes-libretro.git
synced 2024-11-23 17:09:44 +00:00
82293c95ae
byuu says: Changelog: - (u)int(max,ptr) abbreviations removed; use _t suffix now [didn't feel like they were contributing enough to be worth it] - cleaned up nall::integer,natural,real functionality - toInteger, toNatural, toReal for parsing strings to numbers - fromInteger, fromNatural, fromReal for creating strings from numbers - (string,Markup::Node,SQL-based-classes)::(integer,natural,real) left unchanged - template<typename T> numeral(T value, long padding, char padchar) -> string for print() formatting - deduces integer,natural,real based on T ... cast the value if you want to override - there still exists binary,octal,hex,pointer for explicit print() formatting - lstring -> string_vector [but using lstring = string_vector; is declared] - would be nice to remove the using lstring eventually ... but that'd probably require 10,000 lines of changes >_> - format -> string_format [no using here; format was too ambiguous] - using integer = Integer<sizeof(int)*8>; and using natural = Natural<sizeof(uint)*8>; declared - for consistency with boolean. These three are meant for creating zero-initialized values implicitly (various uses) - R65816::io() -> idle() and SPC700::io() -> idle() [more clear; frees up struct IO {} io; naming] - SFC CPU, PPU, SMP use struct IO {} io; over struct (Status,Registers) {} (status,registers); now - still some CPU::Status status values ... they didn't really fit into IO functionality ... will have to think about this more - SFC CPU, PPU, SMP now use step() exclusively instead of addClocks() calling into step() - SFC CPU joypad1_bits, joypad2_bits were unused; killed them - SFC PPU CGRAM moved into PPU::Screen; since nothing else uses it - SFC PPU OAM moved into PPU::Object; since nothing else uses it - the raw uint8[544] array is gone. OAM::read() constructs values from the OAM::Object[512] table now - this avoids having to determine how we want to sub-divide the two OAM memory sections - this also eliminates the OAM::synchronize() functionality - probably more I'm forgetting The FPS fluctuations are driving me insane. This WIP went from 128fps to 137fps. Settled on 133.5fps for the final build. But nothing I changed should have affected performance at all. This level of fluctuation makes it damn near impossible to know whether I'm speeding things up or slowing things down with changes.
147 lines
4.2 KiB
C++
147 lines
4.2 KiB
C++
#pragma once
|
|
|
|
//serializer: a class designed to save and restore the state of classes.
|
|
//
|
|
//benefits:
|
|
//- data() will be portable in size (it is not necessary to specify type sizes.)
|
|
//- data() will be portable in endianness (always stored internally as little-endian.)
|
|
//- one serialize function can both save and restore class states.
|
|
//
|
|
//caveats:
|
|
//- only plain-old-data can be stored. complex classes must provide serialize(serializer&);
|
|
//- floating-point usage is not portable across different implementations
|
|
|
|
#include <nall/stdint.hpp>
|
|
#include <nall/traits.hpp>
|
|
#include <nall/utility.hpp>
|
|
|
|
namespace nall {
|
|
|
|
struct serializer;
|
|
|
|
template<typename T>
|
|
struct has_serialize {
|
|
template<typename C> static auto test(decltype(std::declval<C>().serialize(std::declval<serializer&>()))*) -> char;
|
|
template<typename C> static auto test(...) -> long;
|
|
static const bool value = sizeof(test<T>(0)) == sizeof(char);
|
|
};
|
|
|
|
struct serializer {
|
|
enum Mode : uint { Load, Save, Size };
|
|
|
|
auto mode() const -> Mode {
|
|
return _mode;
|
|
}
|
|
|
|
auto data() const -> const uint8_t* {
|
|
return _data;
|
|
}
|
|
|
|
auto size() const -> uint {
|
|
return _size;
|
|
}
|
|
|
|
auto capacity() const -> uint {
|
|
return _capacity;
|
|
}
|
|
|
|
template<typename T> auto floatingpoint(T& value) -> serializer& {
|
|
enum { size = sizeof(T) };
|
|
//this is rather dangerous, and not cross-platform safe;
|
|
//but there is no standardized way to export FP-values
|
|
auto p = (uint8_t*)&value;
|
|
if(_mode == Save) {
|
|
for(uint n = 0; n < size; n++) _data[_size++] = p[n];
|
|
} else if(_mode == Load) {
|
|
for(uint n = 0; n < size; n++) p[n] = _data[_size++];
|
|
} else {
|
|
_size += size;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template<typename T> auto integer(T& value) -> serializer& {
|
|
enum { size = std::is_same<bool, T>::value ? 1 : sizeof(T) };
|
|
if(_mode == Save) {
|
|
for(uint n = 0; n < size; n++) _data[_size++] = (uintmax_t)value >> (n << 3);
|
|
} else if(_mode == Load) {
|
|
value = 0;
|
|
for(uint n = 0; n < size; n++) value |= (uintmax_t)_data[_size++] << (n << 3);
|
|
} else if(_mode == Size) {
|
|
_size += size;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template<typename T, int N> auto array(T (&array)[N]) -> serializer& {
|
|
for(uint n = 0; n < N; n++) operator()(array[n]);
|
|
return *this;
|
|
}
|
|
|
|
template<typename T> auto array(T array, uint size) -> serializer& {
|
|
for(uint n = 0; n < size; n++) operator()(array[n]);
|
|
return *this;
|
|
}
|
|
|
|
template<typename T> auto operator()(T& value, typename std::enable_if<has_serialize<T>::value>::type* = 0) -> serializer& { value.serialize(*this); return *this; }
|
|
template<typename T> auto operator()(T& value, typename std::enable_if<std::is_integral<T>::value>::type* = 0) -> serializer& { return integer(value); }
|
|
template<typename T> auto operator()(T& value, typename std::enable_if<std::is_floating_point<T>::value>::type* = 0) -> serializer& { return floatingpoint(value); }
|
|
template<typename T> auto operator()(T& value, typename std::enable_if<std::is_array<T>::value>::type* = 0) -> serializer& { return array(value); }
|
|
template<typename T> auto operator()(T& value, uint size, typename std::enable_if<std::is_pointer<T>::value>::type* = 0) -> serializer& { return array(value, size); }
|
|
|
|
auto operator=(const serializer& s) -> serializer& {
|
|
if(_data) delete[] _data;
|
|
|
|
_mode = s._mode;
|
|
_data = new uint8_t[s._capacity];
|
|
_size = s._size;
|
|
_capacity = s._capacity;
|
|
|
|
memcpy(_data, s._data, s._capacity);
|
|
return *this;
|
|
}
|
|
|
|
auto operator=(serializer&& s) -> serializer& {
|
|
if(_data) delete[] _data;
|
|
|
|
_mode = s._mode;
|
|
_data = s._data;
|
|
_size = s._size;
|
|
_capacity = s._capacity;
|
|
|
|
s._data = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
serializer() = default;
|
|
serializer(const serializer& s) { operator=(s); }
|
|
serializer(serializer&& s) { operator=(move(s)); }
|
|
|
|
serializer(uint capacity) {
|
|
_mode = Save;
|
|
_data = new uint8_t[capacity]();
|
|
_size = 0;
|
|
_capacity = capacity;
|
|
}
|
|
|
|
serializer(const uint8_t* data, uint capacity) {
|
|
_mode = Load;
|
|
_data = new uint8_t[capacity];
|
|
_size = 0;
|
|
_capacity = capacity;
|
|
memcpy(_data, data, capacity);
|
|
}
|
|
|
|
~serializer() {
|
|
if(_data) delete[] _data;
|
|
}
|
|
|
|
private:
|
|
Mode _mode = Size;
|
|
uint8_t* _data = nullptr;
|
|
uint _size = 0;
|
|
uint _capacity = 0;
|
|
};
|
|
|
|
};
|