Palm-Studios / sh3redux

SILENT HILL 3 Engine Remake in OpenGL and C++
GNU General Public License v3.0
162 stars 16 forks source link

Singleton Template class #139

Closed Quaker762 closed 5 years ago

Quaker762 commented 5 years ago

Basic templated singleton template class. This is used in instances where we need one and ONLY one instance of a class, such as the engine itself, or it's managers. Due to the nature of the original SH3 engine, I'm going out on a limb (and especially from my research) and assuming we'regoing to need a fair few "global" variables such as these to satisfy the original data structures.

It's currently not thread safe. If two seperate execution threads attempt to access the instance of a class inheriting this, it will result in undefined behaviour. This could probably be fixed by using std::unique_ptr<T> or std::shared_ptr<T>, however I want to keep it simple unless we actually need more than one thread going at a time. From memory, audio in the original game is on a seperate thread, probably due to memory constraints of ~2003. We could probably just load all the audio into memory anyways (via some kind of singleton 'manager' or lookup table) and be done with it anyways, but I digress.

Quaker762 commented 5 years ago

It seems my IDE has used tabs instead of spaces... I'll fix that up shortly.

z33ky commented 5 years ago

Do we need the Destroy-method? If not we can easily make this thread-safe by using a static local non-pointer variable. static variables are guaranteed to be initialized by at most one thread. Methods defined in class-definitions are implicitly inline, thus the qualifier is not needed.

Quaker762 commented 5 years ago

Do we need the Destroy-method?

No we don't! I honestly doubt we'd need that much fine grained control.

§6.7 [stmt.dcl] p4 If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

I was unaware of this. I've fixed it up now. I should probably give the standard a read some time.

Quaker762 commented 5 years ago

That should do it. Let me know if I've forgotten anything else.