From 05c24222d8eda11008837b4046c0ea411fe0d0cf Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 5 Sep 2016 20:02:35 -0400 Subject: [PATCH] Liberated the tape parser template. --- .../Clock Signal.xcodeproj/project.pbxproj | 2 + StaticAnalyser/Acorn/Tape.cpp | 56 +-------------- StaticAnalyser/TapeParser.hpp | 69 +++++++++++++++++++ 3 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 StaticAnalyser/TapeParser.hpp diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 9775d8e94..fdbe15019 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -776,6 +776,7 @@ 4BCA98C21D065CA20062F44C /* 6522.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 6522.hpp; sourceTree = ""; }; 4BD14B0F1D74627C0088EAD6 /* AcornAnalyser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AcornAnalyser.cpp; path = ../../StaticAnalyser/Acorn/AcornAnalyser.cpp; sourceTree = ""; }; 4BD14B101D74627C0088EAD6 /* AcornAnalyser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = AcornAnalyser.hpp; path = ../../StaticAnalyser/Acorn/AcornAnalyser.hpp; sourceTree = ""; }; + 4BD328FD1D7E3EB5003B8C44 /* TapeParser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = TapeParser.hpp; path = ../../StaticAnalyser/TapeParser.hpp; sourceTree = ""; }; 4BD5F1931D13528900631CD1 /* CSBestEffortUpdater.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSBestEffortUpdater.h; path = Updater/CSBestEffortUpdater.h; sourceTree = ""; }; 4BD5F1941D13528900631CD1 /* CSBestEffortUpdater.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CSBestEffortUpdater.m; path = Updater/CSBestEffortUpdater.m; sourceTree = ""; }; 4BEE0A6A1D72496600532C7B /* Cartridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cartridge.cpp; sourceTree = ""; }; @@ -1604,6 +1605,7 @@ 4BF1354B1D6D2C300054B2EA /* StaticAnalyser.hpp */, 4BC830D21D6E7C6D0000A26F /* Commodore */, 4BD14B121D7462810088EAD6 /* Acorn */, + 4BD328FD1D7E3EB5003B8C44 /* TapeParser.hpp */, ); name = StaticAnalyser; sourceTree = ""; diff --git a/StaticAnalyser/Acorn/Tape.cpp b/StaticAnalyser/Acorn/Tape.cpp index 0e5fc5890..cf6104025 100644 --- a/StaticAnalyser/Acorn/Tape.cpp +++ b/StaticAnalyser/Acorn/Tape.cpp @@ -9,62 +9,10 @@ #include "Tape.hpp" #include +#include "../TapeParser.hpp" using namespace StaticAnalyser::Acorn; -/*! - A partly-abstract base class to help in the authorship of tape format parsers; - provides hooks for a -*/ -template class TapeParser { - public: - TapeParser(const std::shared_ptr &tape) : _tape(tape), _has_next_symbol(false) {} - - void reset_error_flag() { _error_flag = false; } - bool get_error_flag() { return _error_flag; } - bool is_at_end() { return _tape->is_at_end(); } - - protected: - bool _error_flag; - void push_wave(WaveType wave) - { - _wave_queue.push_back(wave); - inspect_waves(_wave_queue); - } - - void remove_waves(int number_of_waves) - { - _wave_queue.erase(_wave_queue.begin(), _wave_queue.begin()+number_of_waves); - } - - void push_symbol(SymbolType symbol, int number_of_waves) - { - _has_next_symbol = true; - _next_symbol = symbol; - remove_waves(number_of_waves); - } - - SymbolType get_next_symbol() - { - while(!_has_next_symbol && !is_at_end()) - { - process_pulse(_tape->get_next_pulse()); - } - _has_next_symbol = false; - return _next_symbol; - } - - private: - virtual void process_pulse(Storage::Tape::Tape::Pulse pulse) = 0; - virtual void inspect_waves(const std::vector &waves) = 0; - - std::vector _wave_queue; - SymbolType _next_symbol; - bool _has_next_symbol; - - std::shared_ptr _tape; -}; - enum class WaveType { Short, Long, Unrecognised }; @@ -73,7 +21,7 @@ enum class SymbolType { One, Zero }; -class Acorn1200BaudTapeParser: public TapeParser { +class Acorn1200BaudTapeParser: public StaticAnalyer::TapeParser { public: Acorn1200BaudTapeParser(const std::shared_ptr &tape) : TapeParser(tape) {} diff --git a/StaticAnalyser/TapeParser.hpp b/StaticAnalyser/TapeParser.hpp new file mode 100644 index 000000000..581e2d2fc --- /dev/null +++ b/StaticAnalyser/TapeParser.hpp @@ -0,0 +1,69 @@ +// +// TapeParser.hpp +// Clock Signal +// +// Created by Thomas Harte on 05/09/2016. +// Copyright © 2016 Thomas Harte. All rights reserved. +// + +#ifndef TapeParser_hpp +#define TapeParser_hpp + +namespace StaticAnalyer { + +/*! + A partly-abstract base class to help in the authorship of tape format parsers; + provides hooks for a +*/ +template class TapeParser { + public: + TapeParser(const std::shared_ptr &tape) : _tape(tape), _has_next_symbol(false) {} + + void reset_error_flag() { _error_flag = false; } + bool get_error_flag() { return _error_flag; } + bool is_at_end() { return _tape->is_at_end(); } + + protected: + bool _error_flag; + void push_wave(WaveType wave) + { + _wave_queue.push_back(wave); + inspect_waves(_wave_queue); + } + + void remove_waves(int number_of_waves) + { + _wave_queue.erase(_wave_queue.begin(), _wave_queue.begin()+number_of_waves); + } + + void push_symbol(SymbolType symbol, int number_of_waves) + { + _has_next_symbol = true; + _next_symbol = symbol; + remove_waves(number_of_waves); + } + + SymbolType get_next_symbol() + { + while(!_has_next_symbol && !is_at_end()) + { + process_pulse(_tape->get_next_pulse()); + } + _has_next_symbol = false; + return _next_symbol; + } + + private: + virtual void process_pulse(Storage::Tape::Tape::Pulse pulse) = 0; + virtual void inspect_waves(const std::vector &waves) = 0; + + std::vector _wave_queue; + SymbolType _next_symbol; + bool _has_next_symbol; + + std::shared_ptr _tape; +}; + +} + +#endif /* TapeParser_hpp */