Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch AUDIO_SAMPLE and RES_TYPE/RES_DATA structures to std::list #3608

Merged

Conversation

ManManson
Copy link
Member

@ManManson ManManson commented Jan 23, 2024

The changeset does the following things:

  1. Move mutating_list_iterate utility function to a separate header object_list_iteration.h and move it up the project hierarchy, so that it's accessible in lib/framework and lib/sound.
  2. Enhance mutating_list_iterate to be able to accept lambdas which take an iterator, this is used in some places to efficiently erase from list inside of the handler itself.
  3. Remove psNext and psPrev pointers from AUDIO_SAMPLE, RES_TYPE and RES_DATA structures, also convert global lists g_psSampleList and g_psSampleQueue to std::list<AUDIO_SAMPLE*>.

After these changes, there are still some places in WZ code base, which use C-style lists, but it's not much anymore and should be easy to clear it up in a follow up PR a bit later.

So that the utility is accessible in base libraries

Signed-off-by: Pavel Solodovnikov <[email protected]>
Replace `malloc/free` by `new/delete` to allocate
`RES_TYPE` instances, otherwise we'll have
problems when replacing `psRes` pointer by a
C++ container (e.g. `std::list`), because
`malloc` doesn't know anything about C++ constructors.

Signed-off-by: Pavel Solodovnikov <[email protected]>
…iterate`

Use a compile-time helper with some SFINAE tricks
to allow passing callables which take an iterator
instead of a pointer type as the argument.

This can be quite convenient when one needs to
erase from or insert into the list being iterated
directly inside the handler's body, avoiding
additional lookups to obtain an iterator to
the current element.

The SFINAE function signature checks are performed
using the following trick:

	std::is_convertible<Callable, std::function<Sig>::value

This is based on the ability of raw function
pointers and lambda expressions to be converted
to the `std::function` type with a matching
signature.

Paired with a `std::enable_if_t`, this allows
to easily compile-time-choose the correct overload
of the `Invoke` helper function, which would call
the handler and pass the correct argument to it.

Signed-off-by: Pavel Solodovnikov <[email protected]>
Change `RES_TYPE::psRes` to use `std::list`
instead of a C-style intrusive list.

Also, remove `RES_DATA::psNext` pointer as it's
not needed anymore.

Signed-off-by: Pavel Solodovnikov <[email protected]>
Also, remove now unused `psNext` member field from
`RES_TYPE`.

Signed-off-by: Pavel Solodovnikov <[email protected]>
Also, remove `psPrev` and `psNext` pointers from `AUDIO_SAMPLE`.

Signed-off-by: Pavel Solodovnikov <[email protected]>
This is sort of a hack/workaround to prevent crashes related to
`TargetMissing_` static instance of `BASE_OBJECT` defined
in the `multibot.cpp`.

By the time this object gets destroyed, audio-related global
vars are already destroyed and this object references one of
them in its destructor via `audio_RemoveObj(this)` call.

This should be addressed later, of course. But still,
not crashing is better than crashing and early return
is never bad.

Signed-off-by: Pavel Solodovnikov <[email protected]>
Signed-off-by: Pavel Solodovnikov <[email protected]>
These compilers are more strict than MSVC,
so fix the compilation errors coming from
the linux build.

Signed-off-by: Pavel Solodovnikov <[email protected]>
@past-due past-due merged commit 0cdf530 into Warzone2100:master Jan 24, 2024
35 checks passed
@past-due past-due added this to the 4.5.0-beta1 milestone Jan 28, 2024
@ManManson ManManson deleted the psnext_remove_resources_and_sound branch March 16, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants