Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Curiously Recurring Template Pattern for Bridge Pattern?
    primarykey
    data
    text
    <p>I have been researching the Curiously Recurring Template Pattern to determine how I could use it to implement a Bridge Design Pattern.</p> <p>My problem is connecting, (wiring up), the IBridgeConnector::GetBridgeImpl method to the Bridge::GetBridgeImpl method since the overriding method is actually templated.</p> <p>Since virtual dispatch won't work in this case, what is the best way to point these methods to each other? Function Delegates? Is there a better pattern for this?</p> <p>How should this be done?</p> <p>Thanks for the help!</p> <p>Made the best code simplification I can without shared_ptrs everwhere and OpenGL and DirectX calls. :) Hopefully this will be useful to someone in the future!</p> <pre><code>#include &lt;string&gt; /********************************************************************/ class BridgePart { public: BridgePart * OtherPart; }; /********************************************************************/ // Connects a BridgeSource and a BridgeImplementation class BridgeConnector { public: static BridgeConnector * implementor; // Need a way, (Function Delegates?) to point this method // This method will loop until stack overflow. template &lt;typename ValueTemplateType&gt; BridgePart * GetBridgeImpl(ValueTemplateType * source) { return implementor-&gt;GetBridgeImpl&lt;ValueTemplateType&gt;(source); } }; BridgeConnector * BridgeConnector::implementor = nullptr; /********************************************************************/ // Where the Magic is At, (CRTP) template &lt;typename BridgeImplementationTemplateType&gt; class Bridge : public BridgeConnector { public: template &lt;typename ValueTemplateType&gt; IBridgePart * GetBridgeImpl(IBridgePart * source) { // NOTE: This method never gets called. // CRTP Magic Here to Semi-Specify Pure Virtual Methods return static_cast&lt;BridgeImplementationTemplateType&gt;(this) -&gt;GetBridgeImpl( (ValueTemplateType) source); } }; /********************************************************************/ class BridgeImplementation1 : public Bridge&lt;BridgeImplementation1&gt;, public BridgePart { public: class CustomImpl : public BridgePart { public: template &lt;typename SourceTemplateType&gt; BridgePart(SourceTemplateType source){} /* Does proprietary stuff. */ }; template &lt;typename ValueTemplateType&gt; BridgePart * GetBridgeImpl(ValueTemplateType &amp; source) { return new CustomImpl&lt;ValueTemplateType&gt;(source); } // Constructor BridgeImplementation1() { } }; /********************************************************************/ class BridgeSource1 : public BridgePart {}; class BridgeSource2 : public BridgePart {}; class Client { BridgeSource1 source1; BridgeSource2 source2; BridgeConnector * connector; bool usingImpl1; Client() { usingImpl1 = true; // from config file. connector = new BridgeConnector(); connector-&gt;implementor = usingImpl1 ? (BridgeConnector *) new BridgeImplementation1() : nullptr; // (BridgeConnector *) new BridgeImplementation2(); // removed to shorten code. } void Init() { source1.OtherPart = connector-&gt;GetBridgeImpl&lt;BridgeSource1&gt;(&amp; source1); source2.OtherPart = connector-&gt;GetBridgeImpl&lt;BridgeSource2&gt;(&amp; source2); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload