Jump to content

Allocate event Ids at compile time


Josh
 Share

Recommended Posts

This will assign new event Ids at compile time. This avoids the unpleasant situation where you need to assign the value of an event Id at startup, while still ensuring each ID is unique:

#define ALLOCEVENTID EventId(10001 + __COUNTER__)
#define EVENT_TEST1 ALLOCEVENTID
#define EVENT_TEST2 ALLOCEVENTID

You can use ALLOCEVENTID anywhere, across different files, so you don't need to worry about making sure your custom event IDs are all different.

The Event Ids above can be used in switch statements since they are constant.

  • Like 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Yeah, the problem is that only enums and defines can be used in switch statements, and even if you use if/else if you also an ugly situation before event IDs are initialized.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Ha...this actually increments the value each time it appears in the code. This needs modification to work right...

#define EVENT_TEST ALLOCEVENTID

void main(int argc, const char* argv[])
{
    Print(String(EVENT_TEST));
    Print(String(EVENT_TEST));
    return;
}

Prints:

10001
10002

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

You can use an enum like this. The only problem is you have to cast your own custom events to the EventId enum. I don't know how to make it so CustomEventId gets automatically cast to EventId:

	enum CustomEventId
	{
		EVENT_RENDER = ALLOCEVENTID,
		EVENT_PROGRAMSTART = ALLOCEVENTID,
		EVENT_CONSOLEEXECUTE = ALLOCEVENTID,
		EVENT_THUMBNAILLOADED = ALLOCEVENTID,
		EVENT_OBJECTTEMPLATESELECT = ALLOCEVENTID,
		EVENT_RESETLAYOUT = ALLOCEVENTID
	};

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

#define ALLOCEVENTID EventId(10001 + __COUNTER__) 
#define RegisterEventEnum(T) \
bool operator==(const T& t, const EventId& g) { return static_cast<EventId>(t) == g; }     \
bool operator==(const EventId& g, const T& t) { return static_cast<EventId>(t) == g; }     \
                                                                                           \
bool operator!=(const T& t, const EventId& g) { return static_cast<EventId>(t) != g; }     \
bool operator!=(const EventId& g, const T& t) { return static_cast<EventId>(t) != g; }     \
\
EventId& operator<<=(EventId& g, T t) { g = static_cast<EventId>(t); return g; } \
T& operator<<=(T& t, EventId g) { t = static_cast<T>(g); return t; }; \


enum CustomEvent
{
    TEST_1 = ALLOCEVENTID,
    TEST_2 = ALLOCEVENTID,
};

RegisterEventEnum(CustomEvent);

Try this.

[Edit] Forget it, it is not working. switch statements work out of the box, but functions will not work without proper casting

  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

This is da wey

const EventId EVENT_TEST1 = ALLOCEVENTID;

void main(int argc, const char* argv[])
{
    int i = 3;
    switch (i)
    {
    case EVENT_TEST1:
        break;
    }

    EmitEvent(EVENT_TEST1);

    Print(EVENT_TEST1);
    Print(EVENT_TEST1);
}

Sonic The Hedgehog Tails GIF by Kinda Funny

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 3 weeks later...

I think this approach does not work with partial builds. I am seeing some values that appears twice and make no sense.

The only thing you can do is declare all your custom event IDs in a single file.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...