Statically Typed

because Hindley-Milner rocks

Pragmatic Programmer: Auto-code generation


I’m going to start putting some of my Python C++ generation scripts onto the website bit by bit.  This is a pseudo learning exercise for me as I’m really trying to figure out the mark-up language WordPress uses (is it straight HTML? I don’t know yet.)  Once I have that I can write a simple script to post properly colored code.

The first bit of code is a script to write a C++ header file representing a table from a DB.  Missing are the details of its base class but they are not as important.

def CreateHeader( _tableName, _columns):
   print "#include \"IDBTable.h\"\n"
   print "class " + _className + " : public DBTable"
   print "{"
   print "public:"
   print "\t" + _tableName + "(auto_ptr<ITable> _table);"
   for varName in _columns:
      print "\tstring\tGet" + varName.capitalize() + "Name() { return m_" + varName.capitalize() + "Name; }"
      print "\t int\tGet" + varName.capitalize() + "Column() { return m_" + varName.capitalize() + "Column; }"
   print "protected:"
   print "private:"
   for varName in _columns:
      print "\tstring\tm_" + varName.capitalize() + "Name;"
      print "\tint\tm_" + varName.capitalize() + "Column;"
   print "};"

Yes, I know I could have refactored a few things in there but I’m not lazy enough motivated to do it.  Onward to the CPP file:

def CreateFooter( _tableName, _columns ):
   print "#include \"stdafx.h\""
   print "#include \"" + _tableName.capitalize() + ".h\"\n"
   print _tableName + "::" + _tableName + "(auto_ptr<ITable> *_table)"
   print " : m_Name(L\"" + _tableName + "\"),"
   for varName in _columns:
      print "   m_" + varName.capitalize() + "Name(L\"" + varName.capitalize() + "\"),"
   print "   DBTable(_table)"
   print "{"
   print "\tIFields *fields = m_Table->GetFields();"
   print "\tif( fields == NULL )"
   print "\t{"
   print "\t\tthrow exception(\"Unable to access data fields on " + _tableName + ".\");"
   print "\t}\n"
   for varName in _columns:
      print "\tm_" + varName.capitalize() + "Column = FindColumn( fields, m_ " + varName.capitalize() + "Name );"
   print "}"

Now some people are going to quickly point out that a smart pointer might make more sense if I’d ever like to pass my “ITable” object outside of the class but I won’t.  Trust me.  The actual “m_Table” object is initialized within the base class and it too throws an exception if NULL.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on December 30, 2009 by in Pragmatic Programmer.
%d bloggers like this: