I'm trying to abstract away hardware dependencies for an embedded application.
I have an interface (frameWriterI.h)
template<typename Message)
class FrameWriterI
{
public:
virtual void writeMessage(Message m)=0;
}
With multiple implementations like this one
(writerA.h)
class WriterA : public FrameWriterI<MessageA>
{
public:
void writeMessage(MessageA m)
}
(writerA.cpp)
void WriterA::writeMessage(MessageA m)
{
/*use a DAC to write m with a format specific to A*/
somethingReallyHardwareSpecific();
}
In the layer above, I have 1 state machine per implementation (A, B, C) that use the related Writers
class StateMachineA:
{
public:
void doSomethingThatUseWriterA();
private:
WriterA writer;
}
problem is that now, my state machines are hardware dependent. So in order to mock the writer for unit testing, I should use the interface instead:
class StateMachineA:
{
public:
void doSomethingThatUseWriterA();
private:
FrameWriterI<MessageA>& writer;
}
but where do I initialize the real WriterA?
in the constructor I cannot reference a temporary
(StateMachineA.cpp)
StateMachineA::StateMachineA(): writer(WriterA()){} //not valid
I try to avoid pointers as much as possible (limited heap space and coding preference) but if it's the only solution I could live with it but it can't be dynamically allocated.
The only solution that I found so far is the have a file scoped variable
(StateMachineA.cpp)
WriterA realWriter;
StateMachineA::StateMachineA(): writer(realWriter){}
Any better/cleaner solution?