Click to download a .zip file containing the command-line executable, example outputs and user guide.
If you're an experienced C++ programmer, the following ritual will be very familiar to you: creating .h file; declaring the class; adding a header guard; declaring methods; adding private members; adding getters and setters to access those private members; creating a .cpp file; defining methods; and so on and so forth. The goal of the ClassGen language and compiler is to automate the monotonous aspects of the class creation process as much as possible, freeing the programmer to focus on more important design and implementation details.
The current features of ClassGen include:
- Multiple class declarations per ClassGen script. Each class has separate .h and .cpp files generated for it.
- Copyright and author comments at the tops of all files.
- Descriptive comments at the top of .h files.
- Descriptive comments immediately preceding the class declaration.
- Appropriately named header guards.
- #include statements in the .h file. Supports both "file" and <file> syntaxes.
- Single base class inheritance.
- Constructors, destructors, methods and fields. Default constructors are always generated.
- Hierarchical namespaces (e.g. Engine::Graphics::Sprite).
- Access levels (public/protected/private). Access levels are supported both for inheritance and class members.
- Descriptive comments immediately preceding a field declaration in the .h file.
- Descriptive comments immediately preceding a constructor, destructor or method definition in the .cpp file.
- Assigning a default value to a member field in the default constructor.
- Assigning an argument's value to a member field in a user-defined constructor.
- Constant, pointer and reference types.
- Generating inline Getter and/or Setter functions for a field.
A public default constructor was automatically added to the class declaration, with an accompanying stub in the body file (if I continue development of ClassGen I will likely remove this or make it optional).
A more fully featured script would be something along the lines of:
The first class member is a field with a pointer type, default value, getter and setter. The second is a method with a comment and an argument with a reference type. The third member is another method, this one with a specified access level. Following on are more field declarations (with a namespaced type) and a destructor. Finally there are two more members; a method with multiple comments and a constant argument type; and lastly another field, with multiple comments and a specified access level.
The second class declaration has a specified access level for the inheritance and a more complex default value for one of the fields. It also demonstrates a constructor with arguments which will be used to initialize specified fields.
Processing this script with ClassGen produces 4 files: game_object.h, game_object.cpp, text.h and text.cpp. These are shown below. game_object.h:
Within the class declaration are the members, but they are not in the order they were found in the script. Members are sorted first by access level (public first, then protected, then private), and within an access level they follow the order of constructors, destructors, methods, getters, setters and finally fields. The order of members relative to each other is preserved; for example the method CheckForCollisions was declared before the method Initialize in the script and so it comes first in the set of public method declarations.
Starting with the public members: a default constructor has automatically been added along with the destructor declared in the script; then come the public methods and finally the generated getter and setter functions. The names of these getters and setters are based on the fields they relate to; for example the field m_name had the functions GetName and SetName generated for it. ClassGen supports starting member names with m_ and ignores it when naming getters and setters. The getter for the boolean member m_dead has been named IsDead, rather than GetDead.
The protected members consist of a single field with multiple accompanying comments. The private members comprise a private method followed by the remaining field declarations, again matching the relative order they were declared in in the input script.
With the class declaration complete, the definitions of the getter and setter functions come next. These match the order they were declared in. Finally the file ends with closing braces for the namespaces and a closing #endif for the header guard.
text.h is as follows:
Other features such as automatically deleting and nullifying pointer members in a class' destructor, being able to #include files in the body file, and using namespaces would also improve the program.
But by far the biggest limitation of ClassGen as it stands is coding style. The program only supports one coding style, and it's one I myself already have problems with (getters and setters after the class declaration?). If I were to continue development of ClassGen, the first thing I would add would be a style sheet that can be passed as an optional parameter to the executable and which would dictate the coding style of the generated artifacts. This could include:
- The tab size to use, and whether to use tabs or spaces.
- The directory to save generated artifacts to.
- The file extensions to use (.h/.hh/.hpp/.hxx/.cc/.cpp/.cxx and so on).
- Where to define inline getters and setters.
- The naming convention to use for files, getters and setters.
- What style of comments to use (this could even be extended to include support for third party tools such as Doxygen).
- The placement of opening and closing braces, brackets and parentheses.
- The order of access levels in the class declaration, and whether to have separate access levels for methods and fields.
- The order that member types appear within an access level.
- A maximum line length.