This is an emulator I am currently re-writing for my Operating Systems course. It is a simple emulator that is supposed to represent hardware, OS, etc. It is strictly for learning purposes and I want to propose my solution to the teacher as a method of modern C++ (since his solution is more "C with classes").
However, before I do that, I would like to make sure I don't embarrass myself with bad code.
Right now I'm debating on my association between hardware and OS. They are currently written like so:
// Hardware.h
class OS;
class Hardware : boost::noncopyable
{
friend class OS;
private:
Hardware(OS *os);
void load_job(boost::shared_ptr<Job> job);
void run_job();
private:
void execute_instruction(Instruction const& instruction);
void handle_interrupt(Instruction const& trap);
private:
enum { NumberOfRegisters = 32 };
enum { InstructionMemorySize = 1024 };
enum { DataMemorySize = 1024 };
OS *m_pOS;
std::vector<int> m_Registers;
boost::ptr_vector<Instruction> m_InstructionMemory;
std::vector<int> m_DataMemory;
boost::optional<int> m_Counter;
};
// OS.h
class Hardware;
class OS : boost::noncopyable
{
friend class Hardware;
public:
OS();
// Load jobs
void boot();
// Execute jobs
void run();
private:
// "Interrupts"
void trap_halt(int status);
void trap_getw(int& receiver);
void trap_putw(int value);
void trap_dump();
void time_elapsed();
private:
void get_next_job();
private:
boost::scoped_ptr<Hardware> m_pHardware;
boost::scoped_ptr<Parser> m_pParser;
boost::ptr_deque<Job> m_Jobs;
boost::ptr_deque<Job> m_JobsInProgress;
};
The question is: I don't want the Hardware
to be accessible by any other class other than OS. Declaring Hardware private and friending OS is a step in the right direction, but it bothers me that OS now has access to all functions. Is there a way I can restrict OS
?
Also, I did some researching on callbacks, but the C++ FAQ suggests that you shouldn't make callbacks to member functions of a class. Are there other solutions such that my Hardware
class does not need a pointer to OS
in order to call its functions?
Secondly, I'm wondering, at what point does the pImpl
idiom become a viable solution? I mean, how large does a class have to be, or how many includes must it have, to seriously consider moving the class into a struct
?