1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-04-06 10:38:16 +00:00

Eliminate std::shared_ptr.

This commit is contained in:
Thomas Harte 2025-01-18 23:25:08 -05:00
parent 00b1865fc8
commit beb9f38514
6 changed files with 42 additions and 38 deletions

View File

@ -19,9 +19,9 @@ static std::vector<Storage::Data::ZX8081::File> GetFiles(Storage::Tape::TapeSeri
Storage::Tape::ZX8081::Parser parser;
while(!serialiser.is_at_end()) {
std::shared_ptr<Storage::Data::ZX8081::File> next_file = parser.get_next_file(serialiser);
if(next_file != nullptr) {
files.push_back(*next_file);
const auto next_file = parser.get_next_file(serialiser);
if(next_file) {
files.push_back(std::move(*next_file));
}
}

View File

@ -10,14 +10,16 @@
using namespace Storage::Data::ZX8081;
static uint16_t short_at(std::size_t address, const std::vector<uint8_t> &data) {
namespace {
uint16_t short_at(std::size_t address, const std::vector<uint8_t> &data) {
return uint16_t(data[address] | (data[address + 1] << 8));
}
static std::shared_ptr<File> ZX80FileFromData(const std::vector<uint8_t> &data) {
static std::optional<File> ZX80FileFromData(const std::vector<uint8_t> &data) {
// Does this look like a ZX80 file?
if(data.size() < 0x28) return nullptr;
if(data.size() < 0x28) return std::nullopt;
// uint16_t next_line_number = short_at(0x2, data);
// uint16_t first_visible_line = short_at(0x13, data);
@ -27,11 +29,11 @@ static std::shared_ptr<File> ZX80FileFromData(const std::vector<uint8_t> &data)
uint16_t display_address = short_at(0xc, data);
// check that the end of file is contained within the supplied data
if(size_t(end_of_file - 0x4000) > data.size()) return nullptr;
if(size_t(end_of_file - 0x4000) > data.size()) return std::nullopt;
// check for the proper ordering of buffers
if(vars > end_of_file) return nullptr;
if(end_of_file > display_address) return nullptr;
if(vars > end_of_file) return std::nullopt;
if(end_of_file > display_address) return std::nullopt;
// TODO: does it make sense to inspect the tokenised BASIC?
// It starts at 0x4028 and proceeds as [16-bit line number] [tokens] [0x76],
@ -39,13 +41,13 @@ static std::shared_ptr<File> ZX80FileFromData(const std::vector<uint8_t> &data)
// TODO: check that the line numbers declared above exist (?)
auto file = std::make_shared<File>();
file->data = data;
file->isZX81 = false;
return file;
File file;
file.data = data;
file.isZX81 = false;
return std::move(file);
}
static std::shared_ptr<File> ZX81FileFromData(const std::vector<uint8_t> &data) {
std::optional<File> ZX81FileFromData(const std::vector<uint8_t> &data) {
// Does this look like a ZX81 file?
// Look for a file name.
@ -57,10 +59,10 @@ static std::shared_ptr<File> ZX81FileFromData(const std::vector<uint8_t> &data)
if(data[data_pointer] & 0x80) break;
data_pointer++;
}
if(!c) return nullptr;
if(!c) return std::nullopt;
data_pointer++;
if(data.size() < data_pointer + 0x405e - 0x4009) return nullptr;
if(data.size() < data_pointer + 0x405e - 0x4009) return std::nullopt;
// if(data[data_pointer]) return nullptr;
@ -69,7 +71,7 @@ static std::shared_ptr<File> ZX81FileFromData(const std::vector<uint8_t> &data)
// uint16_t display_address = short_at(0x400c - 0x4009, data);
// check that the end of file is contained within the supplied data
if(data_pointer + end_of_file - 0x4009 > data.size()) return nullptr;
if(data_pointer + end_of_file - 0x4009 > data.size()) return std::nullopt;
// check for the proper ordering of buffers
// if(vars > end_of_file) return nullptr;
@ -81,15 +83,17 @@ static std::shared_ptr<File> ZX81FileFromData(const std::vector<uint8_t> &data)
// TODO: check that the line numbers declared above exist (?)
auto file = std::make_shared<File>();
file->name = StringFromData(name_data, true);
file->data = data;
file->isZX81 = true;
return file;
File file;
file.name = StringFromData(name_data, true);
file.data = data;
file.isZX81 = true;
return std::move(file);
}
std::shared_ptr<File> Storage::Data::ZX8081::FileFromData(const std::vector<uint8_t> &data) {
std::shared_ptr<Storage::Data::ZX8081::File> result = ZX81FileFromData(data);
}
std::optional<File> Storage::Data::ZX8081::FileFromData(const std::vector<uint8_t> &data) {
const auto result = ZX81FileFromData(data);
if(result) return result;
return ZX80FileFromData(data);
}

View File

@ -9,7 +9,7 @@
#pragma once
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>
@ -21,7 +21,7 @@ struct File {
bool isZX81;
};
std::shared_ptr<File> FileFromData(const std::vector<uint8_t> &data);
std::optional<File> FileFromData(const std::vector<uint8_t> &data);
std::wstring StringFromData(const std::vector<uint8_t> &data, bool is_zx81);
std::vector<uint8_t> DataFromString(const std::wstring &string, bool is_zx81);

View File

@ -27,7 +27,7 @@ ZX80O81P::ZX80O81P(const std::string &file_name) {
target_platforms_ = TargetPlatform::ZX81;
}
std::shared_ptr<::Storage::Data::ZX8081::File> zx_file = Storage::Data::ZX8081::FileFromData(data_);
const auto zx_file = Storage::Data::ZX8081::FileFromData(data_);
if(!zx_file) throw ErrorNotZX80O81P;
}

View File

@ -108,32 +108,32 @@ int Parser::get_next_byte(Storage::Tape::TapeSerialiser &serialiser) {
return result;
}
std::shared_ptr<std::vector<uint8_t>> Parser::get_next_file_data(Storage::Tape::TapeSerialiser &serialiser) {
if(is_at_end(serialiser)) return nullptr;
std::optional<std::vector<uint8_t>> Parser::get_next_file_data(Storage::Tape::TapeSerialiser &serialiser) {
if(is_at_end(serialiser)) return std::nullopt;
SymbolType symbol = get_next_symbol(serialiser);
if(symbol != SymbolType::FileGap) {
return nullptr;
return std::nullopt;
}
while((symbol == SymbolType::FileGap || symbol == SymbolType::Unrecognised) && !is_at_end(serialiser)) {
symbol = get_next_symbol(serialiser);
}
if(is_at_end(serialiser)) return nullptr;
if(is_at_end(serialiser)) return std::nullopt;
return_symbol(symbol);
auto result = std::make_shared<std::vector<uint8_t>>();
std::vector<uint8_t> result;
int byte;
while(!is_at_end(serialiser)) {
byte = get_next_byte(serialiser);
if(byte == -1) return result;
result->push_back(uint8_t(byte));
result.push_back(uint8_t(byte));
}
return result;
}
std::shared_ptr<Storage::Data::ZX8081::File> Parser::get_next_file(Storage::Tape::TapeSerialiser &serialiser) {
std::shared_ptr<std::vector<uint8_t>> file_data = get_next_file_data(serialiser);
std::optional<Storage::Data::ZX8081::File> Parser::get_next_file(Storage::Tape::TapeSerialiser &serialiser) {
const auto file_data = get_next_file_data(serialiser);
if(!file_data) {
return nullptr;
return std::nullopt;
}
return Storage::Data::ZX8081::FileFromData(*file_data);
}

View File

@ -40,7 +40,7 @@ public:
attempts to parse those as a valid ZX80 or ZX81 file. If no file is found,
returns nullptr.
*/
std::shared_ptr<Storage::Data::ZX8081::File> get_next_file(Storage::Tape::TapeSerialiser &);
std::optional<Storage::Data::ZX8081::File> get_next_file(Storage::Tape::TapeSerialiser &);
private:
bool pulse_was_high_;
@ -51,7 +51,7 @@ private:
void mark_end() override;
void inspect_waves(const std::vector<WaveType> &waves) override;
std::shared_ptr<std::vector<uint8_t>> get_next_file_data(Storage::Tape::TapeSerialiser &);
std::optional<std::vector<uint8_t>> get_next_file_data(Storage::Tape::TapeSerialiser &);
};
}