We use many macros to hide the details of our OOP classes. This requires that we specify the class name within macros in order to create unique names in the expanded code.
We can get around the problem with the following code structure. The class name is used in other macros to create unique labels, procedure names, etc.
Code: Select all
Macro BeginClass
EndMacro
Macro EndClass
UndefineMacro QQClass
EndMacro
Macro BeginData
Structure cls#QQClass
EndMacro
Macro EndData
EndStructure
EndMacro
Macro QQClass :Application: EndMacro
BeginClass
BeginData
a.i
EndData
EndClass
Macro QQClass :Container: EndMacro
BeginClass
BeginData
a.i
EndData
EndClass
Code: Select all
Macro BeginClass(ClassName)
Macro QQClass
ClassName
EndMacro
EndMacro
Macro EndClass
UndefineMacro QQClass
EndMacro
Macro BeginData
Structure cls#QQClass
EndMacro
Macro EndData
EndStructure
EndMacro
BeginClass(Application)
BeginData
a.i
EndData
EndClass
BeginClass(Container)
BeginData
a.i
EndData
EndClass
FreeBasic has two ways of creating macros, a multi-line Macro/EndMacro feature, and a single-line #Define feature. An #UnDef statement undefines the macro.
In FreeBasic we use the single-line #Define statement for creating all our macros, as shown in the following example. The first parameter is the macro name and its parameters, and the rest of the line is the body of the macro which itself can be a #Define statement. This allows for nested macro definitions.
Code: Select all
#Define BeginClass(ClassName) #Define QQClass ClassName
#Define EndClass #UnDef QQClass
#Define BeginData Type cls##QQClass
#Define EndData End Type
BeginClass(Application)
BeginData
a as integer
EndData
EndClass
BeginClass(Container)
BeginData
a as integer
EndData
EndClass
Code: Select all
Type clsApplication
a as integer
End Type
Type clsContainer
a as integer
End Type