Raised This Month: $12 Target: $400
 3% 

Macro Laziness


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Scag
AlliedModders Donor
Join Date: May 2017
Location: Crashing Hale
Old 03-25-2018 , 16:08   Macro Laziness
Reply With Quote #1

So I have a large methodmap which inherits from StringMap. Getting and setting properties are fairly copy-paste but I wanted to make it stupidly simpler with macros.
I got these:
PHP Code:
#define IntGet(%1)         public get()\
{\
    
int ithis.GetValue(%1i);\
    return 
i;\
}
#define IntSet(%1)         public set( const int i )\
{\
    
this.SetValue(%1i);\
}
// And so on with bool and float 
Moving to a property...
PHP Code:
property int iRoundState
{
    
IntGet("iRoundState")
    
IntSet("iRoundState")

Which compiles just fine, but I wanna make it even simpler with just a single macro. So I tried this:
PHP Code:
#define DoInt(%1)         IntGet(%1)IntSet(%1) 
Which spews "error 155: expected newline, but found 'public'" when used. So after a bunch of variations of the such I was never able to get it to work. Is this "one line wonder" even possible?

And yes I know a lot of people hate macros
__________________
Over-engineering is underrated.

GitHub
BTC
ETH

Retired
Scag is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-25-2018 , 18:05   Re: Macro Laziness
Reply With Quote #2

Your problem is somewhere else. This complete example code compiles without error:

PHP Code:
#include <sourcemod>

#define IntGet(%1)         public get()\
{\
            
int ithis.GetValue(%1i);\
            return 
i;\
}
#define IntSet(%1)         public set( const int i )\
{\
            
this.SetValue(%1i);\
}

#define DoInt(%1)         IntGet(%1)IntSet(%1)

methodmap Foo StringMap
{
        public 
Foo() { return view_as<Foo>(new StringMap()); }
        
property int iRoundState
        
{
                
IntGet("iRoundState")
                
IntSet("iRoundState")
        }
}

public 
void OnPluginStart() {
        
Foo f = new Foo();
        
f.iRoundState 1;

You can use the -l compiler option to get the preprocessed output without actually compiling. You can use it to see what your macros are actually outputting. It is unfortunately annoying since it'll have substituted all the included files.

Last edited by Fyren; 03-25-2018 at 18:12. Reason: actual complete example
Fyren is offline
Scag
AlliedModders Donor
Join Date: May 2017
Location: Crashing Hale
Old 03-25-2018 , 18:53   Re: Macro Laziness
Reply With Quote #3

Whoops, I guess I didn't explain myself very well with how I used the latter macro. Bouncing off of the code snippet you placed down:
PHP Code:
#include <sourcemod>

#define IntGet(%1)         public get()\
{\
            
int ithis.GetValue(%1i);\
            return 
i;\
}
#define IntSet(%1)         public set( const int i )\
{\
            
this.SetValue(%1i);\
}

#define DoInt(%1)         IntGet(%1)IntSet(%1)

methodmap Foo StringMap
{
        public 
Foo() { return view_as<Foo>(new StringMap()); }
        
property int iRoundState
        
{
                
DoInt("iRoundState"// Error line
        
}
}

public 
void OnPluginStart() {
        
Foo f = new Foo();
        
f.iRoundState 1;

Throwing this code snippet down...
PHP Code:
public void function1() { /*Code*/ } public void function2() { /*Code*/ // Errors

void function1() { /*Code*/ void function2() { /*Code*/ // Does not error 
My guess is that the compiler doesn't like more than 1 public function on the same line.
__________________
Over-engineering is underrated.

GitHub
BTC
ETH

Retired
Scag is offline
SZOKOZ
Member
Join Date: Jan 2014
Old 03-25-2018 , 19:56   Re: Macro Laziness
Reply With Quote #4

Didn't know you could make macros like that... Defining a function as a macro and calling it by the macro. Doesn't make sense to me lol. Could you help me understand please.
__________________
May still be available for SM scripting. Just look at my Steam profile regarding my availability.
My Steam

Last edited by SZOKOZ; 03-25-2018 at 20:06. Reason: Never mind didn't realise it the macro was being used in a property. Really confused me there for a good minute.
SZOKOZ is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-25-2018 , 20:28   Re: Macro Laziness
Reply With Quote #5

It looks like it's specific to methodmaps. You can't have non-public functions inside one and closing braces (for properties, functions, and the methodmap itself) have to have a newline after them. Free functions can be declared on the same line whether public or not.
Fyren is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:31.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode