News:

Simutrans Sites
Know our official sites. Find tools and resources for Simutrans.

Global variable for the world?

Started by neroden, June 24, 2013, 03:32:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

prissi

Sorry, I am a little at loss of the many stuff here.

So why npot make  a static member of karte_t called w. karte_t::w will be either NULL (bei definition in the header file) or will be set in init (the first point karte_t contains useful content) to this. Simworld.h is anyway include everywhere where the karte_t this is needed.


in simworld.h

#define WORLD (karte_t::w)

class karte_t
{
static karte_t *w;
...
};

in simworld.cc

karte_t karte_t::w = NULL;
....

void karte_t::init()
{
...
karte_t::w = this;
}

void karte_t::destroy()
{
....
karte_t::w = NULL;
}

everywhere else
welt -> karte_t::w
or
WORLD->

Using a define would allow for a different funtion for debug levels>3 ...

This is threadsafe by desgin, would also allow for testing karte_t::w for NULL (map may exist but not properly no initialized or is already destroyed), and you need to include no new header. Accessing a static is a fast as it could be. I fail to see the advantage of a special pointer class, when there should be no test on accessing it anyway. But I admid I may be thick, and happily hear more explaination.

Dwachs

The idea was to implement something that needs as less modification of other files as possible. This means that the static members 'karte_t* welt' in the code still are static members (now of a different type).

Macros may or may not lead to different problems if 'welt' is replaced by 'karte_t::w' and the word 'welt' is used in a different context, for instance as name of a function parameter. I will try.
Parsley, sage, rosemary, and maggikraut.

Markohs

I'd stay away from macros, they are allways a source of problems.

prissi

#38
Ok, I can understand to stay away from macros.

karte_prt_t could achieve the same if it returns karte_t::welt and still karte_t  decides when it is time for initialisation.


class karte_ptr_t
{
karte_t* operator->() { return karte_t::world; }
...
}

and init then in karte_t::init() and NULL in destroy.

The extra list of all instances is not needed, respective only needed if there ever going to be more than one world (even then a static access would work). Also the list seems not very thread save. Or am I overlooking something again?

EDIT: forgot the patch

Dwachs

Parsley, sage, rosemary, and maggikraut.

Dwachs

Here is a follow-up patch that removes the 'karte_t*' parameters in functions of fabrik_t, some related gui windows lost karte_t* parameters as well. Should we proceed in this direction? Ie transform the code to not pass any karte_t parameters around?
Parsley, sage, rosemary, and maggikraut.

kierongreen

Well if there's no need to then seems a bit pointless passing them around.

prissi

I agree not passing around. However, MSVC complains that struct cannot be freind, class can. So I declare karte_ptr_t a class then ...

Dwachs

Parsley, sage, rosemary, and maggikraut.