Sunday, November 4, 2012

Single Class Instance (Singleton Class)

There is only going to be one instance of the Table class in the program.  I recalled from when learning C++ that there was a way to allow only one instance of a class to be created.  After a little research, it was found that this is known as a singleton class.  There are several ways to implement this.  The solution used for the Table class is described below.  The goals were (click Continue... for complete details on the implementation):
  1. Allow only one instance of the table to be created
  2. Detect table entry errors when the instance is created
  3. Return a list of any error messages of errors found in the table entries
  4. Get a reference to the table instance created

Qt Transition – Table Class

The Table class contains two internal structures, one for a table entry and one for expression information within the table entry.  Only the Table class should have access to these, so their definitions were moved from the table header file to the table source file along with two constants.  Only a forward reference to the table entry was left behind.  Other code has no need to access these.

As a side effect, the code for all the table access functions also had to be moved to the table source file since the structure definitions were no long available in the header file.  This would appear to cause an inefficiency since these functions are no longer in-line functions since they are not defined directly in the class definition.  However, the GCC compiler (and probably other C++ compilers) as part of their optimization, will still in-line these small functions.

All of the table variables and functions were renamed to Qt style naming.  The remaining C character definitions were changed to QString and the remaining standard library calls (and their include statements) were replaced with Qt equivalents.  All the various initialized arrays used for the table entries were made static since they are only used in the table source file.

[commit e8ddccc029]