How to add a macro as a member of a type ?

New to FreeBASIC? Post your questions here.
Post Reply
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to add a macro as a member of a type ?

Post by kcvinu »

Hi all,
Forgive me if this is a stupid question.
I need some freedom from this strict typing of FB. My only relief is macros. So out of curiosity, i am asking that, is there any possible way to add a macro as member of a type ?
like this --

Code: Select all

#Macro aMacro(parameter)
	'Do whatever you want with parameter.
	'May be you need a type check of parameter... 
	'in order to do the task.
#Endmacro

Type aType	 
	Declare Constructor()
	Declare #Macro aMacro(parameter)	 
End Type
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: How to add a macro as a member of a type ?

Post by marcov »

Atype exists on compiler level. A macro exists on preprocessor level. Therefore, this is not possible.
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: How to add a macro as a member of a type ?

Post by caseih »

Not as such, no.

So really you are asking for what other languages call "generics", or "template classes." FB doesn't support those concepts, but within the last year I saw someone post a very complex and complete macro system that emulated template classes, which I think would be close to what you want. Unfortunately I can't find it. hopefully someone can chime in with it.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: How to add a macro as a member of a type ?

Post by sancho3 »

You have to get creative.
In this code the value passed to the macro is used as the type for myvar.
It is also used to create a distinct name for the type. Without that distinct name, type test would always only be the first type you passed to the macro.

Code: Select all

#Macro __make_it_a(T)
Type test##T
	As T myvar
End Type
#Endmacro

__Make_it_a(string)
Dim As teststring t1
t1.myvar = "hello"
? t1.myvar
__make_it_a(integer)
Dim As testinteger t2
t2.myvar = 12
? t2.myvar
Sleep 
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to add a macro as a member of a type ?

Post by D.J.Peters »

pseudo code:

Code: Select all

#define BEGIN_CLASS(_class_name_) type _class_name_
#define END_CLASS end type

#MACRO DECLARE_MEMBER(_member_name_,_data_type_, and what ever)
...
#endmacro

#MACRO IMPLEMENT_MEMBER(_class_name,_member_name_, and what ever)
...
#endmacro

BEGIN_CLASS(MY_SUPER_CLASS)
  declare As_Before ...
  as integer Normal
  DECLARE_MEMBER(BLA_BLA_BLA)
  ...
END_CLASS

IMPLEMENT_MEMBER(MY_SUPER_CLASS,BLA_BLA_BLA) 
Post Reply