Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatic registration of static object factories inside dll modules via metaprogramming or other
    primarykey
    data
    text
    <p>In a project I am working on I want to put classes into a dll that will be initialized by an abstract factory and included via composition within objects in the main program. The purpose is to allow users to create their own dll modules and thus add their own functionality. The dll module itself should be as simple as possible; ideally it would just contain the functionality classes and their base class. The user only has access to the source for the dll and not the main program or factory.</p> <p>I found some code for an abstract factory on codeproject.com that seems pretty good to me. Here's the example code:</p> <p>Code:</p> <pre><code>// Templates for the generic factory and factory plant template &lt;class BT&gt; class FactoryPlant { public: FactoryPlant() {} virtual ~FactoryPlant() {} virtual BT *createInstance() = 0; }; template &lt;class BT,class ST&gt; class Factory : public FactoryPlant&lt;BT&gt; { public: Factory() {} virtual ~Factory() {} virtual BT *createInstance() {return new ST;} }; // Our sample classes class SimpleBaseClass { public: virtual int getValue() = 0; virtual void setValue(int value) = 0; typedef FactoryPlant&lt;SimpleBaseClass&gt; SimpleBaseClassFactory; }; class SimpleClass1 : public SimpleBaseClass { public: int getValue() {return m_value;} void setValue(int value) {m_value = value;} static Factory&lt;SimpleBaseClass,SimpleClass1&gt; myFactory; private: int m_value; }; Factory&lt;SimpleBaseClass,SimpleClass1&gt; SimpleClass1::myFactory; class SimpleClass2 : public SimpleBaseClass { public: int getValue() {return m_value*100;} void setValue(int value) {m_value = value;} static Factory&lt;SimpleBaseClass,SimpleClass2&gt; myFactory; private: int m_value; }; Factory&lt;SimpleBaseClass,SimpleClass2&gt; SimpleClass2::myFactory; // Some method that might need to create an instance SimpleBaseClass *someMethod (std::map&lt;int,SimpleBaseClass::SimpleBaseClassFactory *&gt; factories, int type) { return factories[type]-&gt;createInstance(); } void main() { SimpleBaseClass *simpleInstance = NULL; std::map&lt;int,SimpleBaseClass::SimpleBaseClassFactory *&gt; factories; factories[1] = &amp;SimpleClass1::myFactory; factories[2] = &amp;SimpleClass2::myFactory; simpleInstance = someMethod (factories,1); if (simpleInstance) { simpleInstance-&gt;setValue (123); std::cout &lt;&lt; simpleInstance-&gt;getValue() &lt;&lt; std::endl; delete simpleInstance; } else { std::cout &lt;&lt; "Instance creation failed" &lt;&lt; std::endl; } simpleInstance = someMethod (factories,2); if (simpleInstance) { simpleInstance-&gt;setValue (123); std::cout &lt;&lt; simpleInstance-&gt;getValue() &lt;&lt; std::endl; delete simpleInstance; } else { std::cout &lt;&lt; "Instance creation failed" &lt;&lt; std::endl; } } </code></pre> <p>The obvious way to do this is to have a registration function inside each dll that gets called to build up the map. My question: is it possible to have my program register the factories in the map automatically somehow eg. through some metaprogramming technique so that the user doesn't need to make this function, which could potentially have a very long list of map assignments? What I want to do would be something like make a list or script of the dll classes at compile time and then perform registration from the list at runtime.</p> <p>Thanks a lot for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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