#pragma once #include #include "printer.h" #include "inputter.h" namespace lc3 { namespace shims { class PrinterShim: public lc3::utils::IPrinter { public: PrinterShim( std::function setColor, std::function print, std::function newline ): setColorFunc(setColor), printFunc(print), newlineFunc(newline) {} virtual void setColor(lc3::utils::PrintColor color) override; virtual void print(std::string const & string) override; virtual void newline(void) override; private: std::function setColorFunc; std::function printFunc; std::function newlineFunc; }; PrinterShim noOpPrintShim(void); void setColorNoOp(lc3::utils::PrintColor color); void printNoOp(std::string const & string); void newlineNoOp(void); class InputterShim: public lc3::utils::IInputter { public: InputterShim( std::function beginInput, std::function getChar, std::function endInput ): beginInputFunc(beginInput), getCharFunc(getChar), endInputFunc(endInput) {} virtual void beginInput(void) override; virtual bool getChar(char & c) override; virtual void endInput(void) override; private: std::function beginInputFunc; std::function getCharFunc; std::function endInputFunc; }; InputterShim noOpInputShim(void); void beginInputNoOp(void); bool getCharNoOp(char & c); void endInputNoOp(void); /// Prints to a buffer. class BufferPrinter: public lc3::utils::IPrinter { public: BufferPrinter(size_t len, unsigned char buffer[/*len*/]): len(len), buffer(buffer) {} virtual void setColor(lc3::utils::PrintColor color) override; virtual void print(std::string const & string) override; virtual void newline(void) override; private: bool put(unsigned char c); size_t pos = 0; size_t len; unsigned char* buffer; }; /// Gets inputs from a buffer. class BufferInputter: public lc3::utils::IInputter { public: BufferInputter(size_t len, unsigned char const buffer[/*len*/]): len(len), buffer(buffer) {} virtual void beginInput(void) override; virtual bool getChar(char & c) override; virtual void endInput(void) override; private: size_t pos = 0; size_t len; unsigned char const* buffer; }; /// Calls a function on output. class CallbackPrinter: public lc3::utils::IPrinter { public: CallbackPrinter(void(*func)(unsigned char)): func(func) {} virtual void setColor(lc3::utils::PrintColor color) override; virtual void print(std::string const & string) override; virtual void newline(void) override; private: void (*func)(unsigned char); }; /// Calls a function to get an input. /// /// Note: the function must ultimately produce a character but *can* block. class CallbackInputter: public lc3::utils::IInputter { public: CallbackInputter(unsigned char (*func)(void)): func(func) {} virtual void beginInput(void) override; virtual bool getChar(char & c) override; virtual void endInput(void) override; private: unsigned char (*func)(void); }; }; };