Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this is a perfect use case for <a href="http://nedbatchelder.com/code/cog/" rel="nofollow noreferrer">python cog</a></p> <p>see also <a href="https://stackoverflow.com/questions/294520/what-are-some-techniques-for-code-generation">this answer</a>.</p> <p>I've used cog to generate handlers for a list of events, the handler code is very generic and i don't need to do special cases but i still have to write all the functions, so what i did is keep the events in a list in a .py file and the code to generate the boilerplate of the handlers in a python function. so i'm able to be faithful to the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow noreferrer">DRY principle</a></p> <p>obviously you'll had to add cog to the pre-build of your makefile in order to work with your toolchain</p> <p><strong>Edit</strong> as an example of the code generation design required for adding boilerplate to your classes, i would do something like:</p> <h2>myCodeGeneration.py</h2> <pre><code>import cog ClassesToMock = [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] , [ 'IfaceB', 'classB' , 'mockB' , ['static classA&amp; getInstance()'] ] def addInterfaces( myStructure ): for classItem in myStructure: cog.outl('class %s { ' % classItem[0] ) for methodDecl in classItem[3]: cog.outl(' virtual %s = 0;' %methodDecl ) cog.outl(' } ') #implement your real classes normally def addMocks( myStructure ): for classItem in myStructure: cog.outl('class %s : public %s { ' % classItem[2] % classItem[0] ) for methodDecl in classItem[3]: cog.outl(' %s {' %methodDecl ) cog.outl(' MOCK_STUFF_MACRO ') cog.outl(' } ') cog.outl(' } ') </code></pre> <p>then in your header:</p> <h2>IfaceA.h</h2> <pre><code>/*[[[cog import cog import myCodeGeneration myCodeGeneration.addInterfaces( [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] ] ) ]]]*/ //your code will be generated here //[[[end]]] </code></pre> <h2>mockA.h</h2> <pre><code>/*[[[cog import cog import myCodeGeneration myCodeGeneration.addMocks( [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] ] ) ]]]*/ //your code will be generated here //[[[end]]] </code></pre> <p>Also, the issue if considering adding python to your c++ source is 'polluting' it or 'beautifying' it is largely a matter of taste and style. I consider that cog provides a complement to template style metaprogramming that is lacking in c++, giving the tools to the programmer to provide code tidiness and readability. But i don't expect everyone to agree</p> <p>For me the whole architectural design principle behind this approach is <strong>Don't Repeat Yourself</strong>. errors happen when we have to encode a method in several places manually. Let the computer automate what its automatable and encode things that cannot <em>just once</em>. As a side-effect, it will make your coding more enjoyable, both for writing it and reading it later.</p> <p>hope this helps</p>
 

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