Every once in a while when I'm programming, I complain to myself, "Do
I really need to type so much?". I'm sure many programmers make the
statement occasionally and even try to write macros to avoid the extra
typing. I even had one Mac programmer tell me once that he never typed
a single letter on the keyboard, but was able to construct all his programs
with just a mouse. This of course seems to be a bit of an exaggeration,
but exaggerating sometimes commands higher rates
The example I chose to use is a sound class. The sound class will be able to play a sound from the wave file is used to construct it. To make the object a little more interesting, we'll put a duration attribute in the object to allow you to determine how long the wave file will be played. The first step is to draw the class diagram in With Class. This is accomplished by using With Class's simple drawing tools:
We made Sound a subclass of CWnd so that we could take advantage of the timer feature of CWnd to cut out the duration of sound. Each of the attributes and operations of With Class are initially created by simply typing them into the class. Then the details of each are filled in by double-clicking on the class and filling the appropriate attribute and operation dialogs:
After all the details of With Class are filled in, we can show these by going to the View | Show Class menu and selecting Show Type, Show Parameters, etc.
Now we are ready to use the With Class Scripting language to generate some of the framework. The following is a minimal script for the header file:
Listing 1.
#ifndef __$CAPITALIZE_ALL$CLASS_NAME$_H
#define __$CAPITALIZE_ALL$CLASS_NAME$_H
> [
#ifndef __$CAPITALIZE_ALL$BASE_CLASS$_H
#include "$BASE_CLASS$.h"
#endif
]
[#include
]
class CLASS_NAME[NO_RETURN NO_REPEAT: NO_REPEAT public BASE_CLASS ,DELETE_LAST_SYMBOL]
{
[ATTRIBUTE_TYPE ATTRIBUTE_NAME;]
public:
[CPP_OPERATION_VIRTUAL CPP_OPERATION_STATIC OPERATION_RETURN_TYPE OPERATION_NAME
(CPP_OPERATION_PARAMETERS) CPP_OPERATION_CONSTANT CPP_OPERATION_PURE_VIRTUAL;]
};
#endif
The capitalized words are the keywords for with classes scripting languages.
Brackets around keywords indicate repeat for each value. For example:
[ATTRIBUTE_TYPE ATTRIBUTE_NAME;]
This tells with class to write the attribute's type and attribute;s
The resulting code from the diagram four sound.h is:
Listing 2.
#ifndef __SOUND_H
#define __SOUND_H
#ifndef __CWND_H
#include "CWnd.h"
#endif
class Sound : public CWnd
{
int m_strFile;
int m_nDuration;
public:
Sound (CString& strFile, int nDuration = 0) ;
void Play () ;
void SetDuration (int nDuration) ;
void SetWaveFile (CString& strFile) ;
};
#endif
The user can also generate his CPP shell as well. For the CPP code, we
will use the following script:
Listing 3.
#include "stdafx.h"
#include "$CLASS_NAME$.h"
[
// Function: OPERATION_NAME
// Purpose: OPERATION_COMMENT1
// Parameters: CPP_OPERATION_PARAMETERS
// Comments: OPERATION_COMMENT2
OPERATION_RETURN_TYPE CLASS_NAME::OPERATION_NAME(CPP_OPERATION_PARAMETERS)
CPP_OPERATION_CONSTANT
{
OPERATION_CODE
}
]
The script will repeat for each method in With Class. OPERATION_COMMENT1
and OPERATION_COMMENT2 are used here for filling in the purpose and additional
operation comments, but the user can use the fields for whatever they like.
Note that the user can use the Code button in With Class to enter code
so that the parameter OPERATION_CODE is filled in with any added code:
After the code is generated for the CPP file, the Import and Export C++ buttons can be used to import and export code to the same CPP file.
Well what do we get when we run the script? Here is the output generated by With Class:
Listing 4.
#include "stdafx.h"
#include "Sound.h"
// Function: Sound
// Purpose: Constructor for sound object
// Parameters: CString& strFile, int nDuration = 0
// Comments:
Sound::Sound(CString& strFile, int nDuration = 0)
{
}
// Function: Play
// Purpose: Play the Wave File Sound
// Parameters:
// Comments: This is limited to the m_nDuration variable
void Sound::Play()
{
}
// Function: SetDuration
// Purpose: Sets the duration of the sound
// Parameters: int nDuration
// Comments:
void Sound::SetDuration(int nDuration)
{
// code for set duration
m_nDuration = nDuration;
}
// Function: SetWaveFile
// Purpose: Set the wave file to be played
// Parameters: CString& strFile
// Comments:
void Sound::SetWaveFile(CString& strFile)
{
}
The SetDuration code was filled into our OPERATION_CODE keyword since we
entered the code into With Class. Code can now be typed directly into the
file sound.cpp and imported into With Class, or typed into With Class and
exported into sound.cpp.
Additional code can be added to With Class to support the OnTimer function so that the sound object will jump into this function when the duration of the sound is up, and turn off the sound.
Conclusion: With Class's scripting language is flexible enough to allow the user to generate code however they wish. Additional script variables such as TAB_COLUMN allow the user to format code so that the code lines up properly. Look for future articles on With Class to give you more examples on using this powerful tool.
Michael Gold is a Software Developer at MicroGold Software.
He has been programming Visual C++ in the Financial and
Engineering industries for 4 ½ years. Feel free to write him
at 71543.1172@compuserve.com
for any comments.