title: Dynamic switching of implementation in hardware abstraction layer (HAL)
body: <p>I'm trying to implement a hardware abstraction layer (HAL) to access several similar devices using all the same interface.
  The question is, how should I implement the dynamic switching of the implementation for one device class vs. another.<br />
  The following code shows how I do that actually.
  However, I am not sure if this implementation is safe enough to stay. Especially I'm unsure if it is the right way to use 'shared_ptr' for switching the implementation or if it would be better to use a factory pattern or even more abstraction.
  What would you recommend to implement such a dynamic HAL?</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  
  using namespace std;
  
  // Common HAL base class
  struct Base {
    virtual string Identify() {return string(&quot;Baseclass&quot;);}
    Base() {cout &lt;&lt; &quot;Base Constructor&quot; &lt;&lt; '\n';}
    virtual ~Base() {cout &lt;&lt; &quot;Base Destructor&quot; &lt;&lt; '\n';} 
    // Important to make destructor virtual so that destructor of derived classes is invoked, too
  };
  
  // HAL Device 1 Implementation
  struct Derived1: public Base {
    string Identify() override {return string(&quot;Derived 1 class&quot;);}
    Derived1() {cout &lt;&lt; &quot;Derived1 Constructor&quot; &lt;&lt; '\n';}
    ~Derived1() {cout &lt;&lt; &quot;Derived1 Destructor&quot; &lt;&lt; '\n';}
  };
  
  // HAL Device 2 Implementation
  struct Derived2: public Base {
    string Identify() override {return string(&quot;Derived 2 class&quot;);}
    Derived2() {cout &lt;&lt; &quot;Derived2 Constructor&quot; &lt;&lt; '\n';}
    ~Derived2() {cout &lt;&lt; &quot;Derived2 Destructor&quot; &lt;&lt; '\n';}
  };
  
  // Switches implementation via setting a pointer to a class instance
  struct ImplementationDispatcher {
    shared_ptr&lt;Base&gt; Impl = make_shared&lt;Base&gt;();
  
    Base&amp; Implementation() {return *Impl;}
    void SetImplementation(shared_ptr&lt;Base&gt; impl) {Impl = impl;}
  };
  
  // Top level class which calls HAL device functions
  struct Actor {
    ImplementationDispatcher HAL;
  
    void Work() {cout &lt;&lt; HAL.Implementation().Identify() &lt;&lt; '\n';}
    void SetActor(shared_ptr&lt;Base&gt; newImpl) {HAL.SetImplementation(newImpl);}
  };
  
  int main()
  {
    Actor actor;
  
    actor.Work();
    actor.SetActor(make_shared&lt;Derived1&gt;());
    actor.Work();
    actor.SetActor(make_shared&lt;Derived2&gt;());
    actor.Work();
  }
  </code></pre>
  <p>I define a base class with common functionality for all devices (In the case of this example this is only the <code>Identify()</code> method).
  Then i derive classes for device implementations <code>Derived1</code> and <code>Derived2</code>.
  I want to switch between those implementations in the main code (e.g. device selector or sth similar. In the example this is just done by calling a setter from the main).<br />
  To implement the switching i added a class <code>ImplementationDispatcher</code> which holds a <code>shared_ptr</code> to the current implementation for isolation purposes, similar to the pimpl-idiom.
  The switching is done by feeding the switcher class with new instances of subclasses from base.</p>